Vim Quick Reference
Contents
This article needs polish, do not truely trust it!
Vim is so-called the god of editors, but not so friendly to new users. Today we will cover some techniques and trick of vim, for further reference.
General Pattern
A vim operation consists of three parts, namely
[OPERATOR][NUMBER][MOTION]
where
OPERATOR- what you want to do? This mainly covers copy, cut, paste, etc.NUMBER- how many times do you want? It’s nothing but repeating the operationNUMBERtimes, and it’s optional.MOTION- where do you want to go? This point out the scope where theOPERATORapplies.
Note: order does not matter sometimes.
Operators
Copy, Cut, Paste
- v - visual mode, now you can select what you want
- V - visual in line, this is extremely useful when you want to copy a line, just
V+y! - y - yank, like Ctrl+C, copy the selected text to clipboard (
")yy: copy current line5yy: copy 5 lines belowy+MOTION: copy the motion scopey0: copy from here to BOL (beginning of line)y$: copy from here to EOLy4G: copy from here to line 4y?bar: copy from here to previous occurrence ofbar
- d - delete & yank, not only delete, but also yank
- p - paste after the cursor
- P - paste before the cursor
Edit
- i - insert
- a - insert after the cursor
- o - insert a line below and insert
- O - insert a line above and insert
- r - replace, replace the character inplace
- x - delete current character
- s - delete the character and insert
- u - recall the last command
^r- recall the last recall, namely redo.- repeat the last command
Note: all deleted things were automatically yanked in buffers, i.e., register
"
Motions
Basic
- h - move left
- l - move right
- j - move down
- k - move up
Note: view
(h, l)and(j, k)as pairs - G - jump to EOF (end of file)
- gg - jump to BOF
x+ (gg | G) - jump to linex(must be a valid line number)
Some additional movements:
- w - next word, points to the first letter
- b - back, previous word, points to the first letter
- e - end, jump to the end of the word
%- find the bracket matches (( ),{ },[ ]…)^e- scroll down^y- scroll up^d- half-screen down^u- half-screen up*- jump to next occurrence of current word#- jump to previous occurrence of current word
Note: view
(w, b)as a pair
Inline movement
^: jump to the first character which is not a blank (space, tab,\n,\r)g_: jump to the last character which isn’t a blank0: jump to the beginning$: jump to the end- f
x: find nextxin current lineNote: you can use
;(alongside) and,(reverse side) to repeat this in two directions - F
x: find previousxin current lineNote: same rules can be applied
- t
x: find nextxand move 1 backward - T
x: find previousxand move 1 backward

Commands
Search & Replace
/keyword- searchkeywordafter the cursor?keyword- searchkeywordbefore the cursorNote: use
n(search next) orN(search previous) for quick search:{search_scope}s/{target}/{replace}/{replace_flag}- replace{target}to{replace}sstands for substitute.:%s/a/b/g: global (%) searcha, replace it tobat every (g) occurrence:%s/a/b/gc: interact with every replace
More detail
It has a general pattern:
:[range]s/from/to/[flags]
The default range is current line. See some examples:
:1,10s/from/to- search and replace between line 1 and 10 (included):10s/from/to/- search and replace only in line 10:%s/from/to/- in global scope
flags can be
- g: replace all matches in whole line w/o confirmation
- c: confirm before replace
- i: ignore lower/upper case
- e: ignore error
Note that flags can be combined together, e.g., :%s/from/to/gc means search and replace in global and ask for confirmation before each replacement.
Use regular expression
| Meta character | Explanation |
|---|---|
. | Matches any character |
[abc] | Matches any char from the list |
[^abc] | Matches any char except from the list |
\d | Matches numers == [0-9] |
\D | Opposite to above == [^0-9] |
\x | Matches hex numbers == [0-9A-Fa-f] |
\X | Opposite to above == [^0-9A-Fa-f] |
\l | Matches lower case letters == [a-z] |
\L | opposite to above == [^a-z] |
\u | Matches upper case letters == [A-Z] |
\U | Opposite to above == [^A-Z] |
\w | Matches alphanumeric chars == [0-9A-Za-z_] |
\W | Opposite to above == [^0-9A-Za-z_] |
\t | Matches <TAB> |
\s | Matches space == [\t] |
\S | Opposite to above |
Special characters need to be escaped. Some of them are .[]\*/, if you want to match some of them, put the backslash “" ahead. For example: * -> \*.
There are also some special form to express how much do you expect to match the specific pattern.
| Meta char | Explanation |
|---|---|
* | match >= 0 times |
\+ | match >= 1 times |
\? | match 0 or 1 time |
\{n,m} | match n<=x<=m times |
\{n} | match n times |
\{n,} | match >= n times |
\{,m} | match <= m times |
Also, some postional characters.
| Meta char | Explanation |
|---|---|
$ | end of line |
^ | beginning of line |
\< | beginning of word |
\> | end of word |
Some examples:
- remove the spaces of eol:
%s/\s+$//g - remove spaces of bol:
%s/^\s*//or%s/^ *// - delete empty line:
%s/^$//org/^$/d - delete lines with
<space>or<tab>as beginning:%s/^[ |\t]*$//org/^[ |\t]*$/d
Note that pattern in regex scoped by \(<pattern>\) can be refered as \1, \2, etc. in the latter statement. For example, I want to replace every “abc…xyz” to “xyz…abc”, just write like this
%s/\(abc\)\(.*\)\(xyz\)/\3\2\1/g
Advanced Tricks
Auto Complete
In insert mode, press ^p, vim will give you a list of all words you have typed, kind of auto complete.

Markers
:marks- list of marksmk- mark current position (can use a-z)also known as
:mark k'k- move to mark kd'k- delete from current position to mark k'a-z- same file'A-Z- between files
A straight tick
'refers to the line, use a backtick`to also include the column, see [here][foo].
It seems that the marker . will mark the last edit position, so if you open your last edited file again, `. will take you to that position!
Block Editing
One of the magic of vim is block editing. Just press ^V to enter block mode. Then select some block you are interested, then make some modifications. Finally press Esc, then those modifications you have just made will be applied onto every line of the block.
See this magic:
^jump to BOL^Venter block mode4jmove 4 lines downIenter insert mode and add somethingEscto see the effect

Additionally, you can
^v/v/Venter visual modeJjoint them into one line<or>modify indents=auto indent (extremely powerful?)

or
^venter block mode- select some line
$jump to EOLAappend somethingEscsee the effect

Macro Recording
Press qx, where x is the macro name, will enter macro recording mode, all actions will be recorded, just like a tape recorder. If you don’t want to record anymore, press q to stop recording.
To replay the record, press @x. Moreover, @@ will replay the last recorded macro.
Summarization:
qa - record macro a
q - stop recording macro
@a - run macro a
@@ - rerun last run macro
Clipboard
Vim provides 12 clipboards (registers): 0, 1, 2 .. 9, a, ". If your vim support system clipboard, there will be two additional register: + and *. Use :reg to see what are in your registers.
For X11 systems, things selected or highlighted will be saved in register *, while things yanked or cutted will be saved in register +.
To see whether your vim support system clipboard, type
$ vim --version
In general, all your copy and paste operations are performed at register " by default. To use other register, add a prefix "6 to your yank or paste commands. For example:
| |
Multi-file
:e <file>- edit<file>in new bufferbnext- go to the next bufferbprev- go previousbd- delete a buffer (close a file)ls- list all open buffers
Multi-window
:sp <file>- split horizontally and open<file>:vsp- split verticallyand open filename optionally, same file by default^w+h/j/k/l- focus left/down/up/right window:close- close current window (buffer & file)
Multi-tab
tabnew <file>- open<file>in new tab, empty file by defaultgtor:tabnext- move to the next tabgTor:tabprev- move to previous<num>gt- move to tab number<num>:tabclose- close the current tab (windows & files):tabonly- close all tabs except for the current one:tabdo <cmd>- apply the<cmd>to all tabstabdo qwill close all tabs
Spell Check
:set spell- toggle on spell checker:set nospell- toggle off spell checker]s- move to next mistake[s- move tp previous mistakez=- choose an alternativezg- add to userdictzw- remove from userdict