Skip to content

Latest commit

 

History

History
366 lines (304 loc) · 8.58 KB

Vim commands.md

File metadata and controls

366 lines (304 loc) · 8.58 KB

vimrc

Where is my .vimrc file?

It may be located at:

/etc/vim/vimrc

Otherwise create it at:

~/.vimrc

Plugins

[Linux] - 將vim打造成source insight

vim附件:cscope+ctag 使用筆記

Set vim editor to full screen

reset
export TERM=xterm
stty rows 40 cols 120

Convert tab to 4 spaces

vim 把 Tab 轉換為 4 Space

syntax enable
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab 

Merge a .swp file to the original file

How to merge a swap (.swp) file to the original file?

vim .xxx.swp
# in the editor, :recover
rm .xxx.swp

Vim commands in Visual Mode

Get help message(manual) of a vim command

:h <vim-command>

Turn on and off syntax highlight

How to Turn On or Off Color Syntax Highlighting In vi or vim Editor

:syntax on
:syntax off

Show and hide line numbers

How To Show or Hide Line Numbers In vi / vim Text Editor

<:set number> or <:set nu>: show line numbers
<:set nonumber> or <:set nu!>: hide line numbers

Show opened's file's name

How to find out which file is currently opened in vim?

<ctrl+g>: show opened' file's name and its total line number
<1> and then <ctrl+g>: additionally show full path
<2> and then <ctrl+g>: additionally show buffer number

Move cursor

<h>: move left
<j>: move down
<k>: move up
<l>: move right
<0>: go to head of line
<$>: go to end of line
<gg>: go to first line
<:$>: go to last line
<ctrl+f>: page down
<ctrl+d>: half page down
<ctrl+b>: page up
<ctrl+u>: half page up
<%>: move to matching braces

Find and go to that char in current line

<f?>: find the character <?> and move the cursor onto it
<F?>: same as <f?>, but find backward
<t?>: same as <f?>, but stop at the character before <?>
<T?>: same as <t?>, but find backward

They can be preceded with a number <n>, so the command will be repeated n times.

<;>: repeat the previous command
<,>: repeat the previous command in reverse direction

Switch search mode between case-sensitive or case-insensitive

<:set ic> or <:set ignorecase>: case-insensitive mode search
<:set noic>: case-sensitive search

Switch to smartcase search mode

How to do case insensitive search in Vim

<:set ic> and then <:set smartcase>

If you search for something containing uppercase characters, it will do a case-sensitive search; if you search for something purely lowercase, it will do a case-insensitive search.

Change the display of long line

How to stop line breaking in vim Display long line as multiple lines:

<:set wrap>

Display long line as one line:

<:set nowrap>

Search a word

Method 1:

/\<.*the-word-you-want-to-find.*\>
<n> to find next
<N> to find previous

Method 2:

move the cursor to the word you want to find
<SHIFT+*> to find the word the cursor at
<SHIFT+#> to find previous

Paste

<p>: paste after
<P>: paste before

paste, disable autoindent

Turning off auto indent when pasting text into vim

Before pasting:

:set paste

After pasting:

:set nopaste

Copy, Delete a character

<y> and then move left: copy the character the cursor move to
<y> and then move right: copy the character the cursor previously at
<x>: delete the current character under the cursor

Copy, Delete a word

<yaw>: copy the current word(not character) under the cursor
<daw>: delete the current word(not character) under the cursor
<caw>: delete the current word and put you into insert mode

Copy, Delete a line

<yy> or <Y>: copy a line
<dd>: cut a line

Copy, Delete all the file

<:%y>: copy all the file
<:%d>: delete all the file

Delete all lines after this line

How to delete line(s) below current line in vim?

:+,$d

Copy selected range to clipboard(so one can paste it outside vim)

First install vim-gtk:

sudo apt-get install vim-gtk

Make sure clipboard and xterm_clipboard are + now.

vim --version | grep clipboard

And then:

"+y

Insert a newline and go to that line

<o>

Delete the characters until the end of this line:

<D>

Delete the newline character at the end of this line:

<J>

Delete character, word, or lines with <d?>

vim deleting backward tricks

<dh>: delete the previous character
<dl>: delete current character
<db>: delete all previous characters in this word
<dw>: delete all later characters in this word, also the following space
<cw>: change(i.e. delete and go to insert mode) all later characters in this word
<diw>: delete current word
<ciw>: change current word
<daw>: delete the current word(word here is the characters between these specific characters: .,-+()[]/\)
<daW>: delete the current token(token here is the characters between spaces, they may contain .,-+()[]/\)
<d0>: delete all previous characters
<d$>: delete from current character to the end of line

Delete characters after a specific character in all lines

Delete All Characters After “.” In Each Line

:%norm f<?>lD #<?> is that specific character

or

:%s/\<?>.*/<?>/

Delete all lines containing specific word

Efficient way to delete line containing certain text in vim with prompt

:%s/.*<text>.*\n//g

Remove all trailing spaces

How to automatically strip trailing spaces on save in Vi and Vim?

:%s/\s\+$//e

Find and Replace

Vim - Search and replace

Find all 'foo's in the file and replace them with 'bar's.

:%s/foo/bar/g

Interactive search/replace regex in Vim?

To find and replace one by one with confirmation, use:

:%s/foo/bar/gc

And then use y to confirm, n to skip, Esc to quit.

To escape / and \, add \ before them, they becomes: \/ and \\.

To replace trailing spaces:

Remove unwanted spaces

:%s/\s\+$//c

Undo and Redo

<u> or <:undo>: undo
<Ctrl+R> or <:redo>: redo

Select multiple lines

<Shift+V> and then move up or down to select multiple lines

Indent and Unindent

Method 1:

<:left 4> or <:le 4>: indent!!
<:right 4>: unindent!!

Method 2:

:set tabstop=4
:set shiftwidth=4
:set expandtab
and then >(to indent) or <(to unindent)

Method 3: to indent

:'<,'>s!^!    !

To indent the whole file:

How do I fix the indentation of an entire file in Vi?

gg=G''

Comment multiple lines

What's a quick way to comment/uncomment lines in Vim?

Ctrl + v to go into visual mode, select multiple lines, and then:

:'<,'>s/^/#/

Note that '<,'> appears automatically after : is entered.

Dispose of "\ No newline at end of file"

VIM Disable Automatic Newline At End Of File

vim -b file.txt
:set noeol
:wq

or

vim file.txt
:set binary
:set noeol
:wq

To disable automatic newline forever, add the following into .vimrc:

:set nofixendofline

Quit

<:q!>

Save and quit

<:wq>