Sep 13, 2019 usage article


Vim is a command and mode based editor, so everything can be done with various commands(operator and motion) in different modes. And for each file opened by Vim, there is a buffer. Vim shows everything to you with windows and tabs.

Operator and Motion

there are two kind of commands in Vim: Operator and Motion.

  • Operator commands are generally used to delete or change text.
  • Motion commands are used to move the cursor position.


Vim is always in a specify mode, e.g. once you open a file with vi file.txt you are in Normal mode by default, then type i you switch to Insert mode, with a ⎋Escape you switch back to Normal mode again.


Cheat sheet for Operator commands.

Operator Describe
d Delete
y Yank(copy)
c Change(delete then insert)
> Indent right
«/kbd> Indent left
~ swap case (only if ‘tildeop’ is set)
g~ Swap case
gU Uppercase
gu Lowercase
! Filter through external program
= Filter through ‘equalprg’ or C-indenting if empty
gq Text formatting
gw Text formatting with no cursor movement
g? ROT13 encoding
zf Define a fold
g@ Call function set with the ‘operatorfunc’ option


Motion commands: move/jump cursor between lines or files, select lines.

Left-Right motions

Left-Right motions Describe
h [count] characters to the left
l [count] characters to the right
0 To the first character of the line
^ To the first non-blank character of the line
$ To the end of the line
g_ To the last non-blank character of the line
g0 To the first character of the screen line
g^ To the first non-blank character of the scren line
g$ To the last character of the screen line
f{char} To [count]‘th occurrence of {char} to the right
F{char} To the [count]‘th occurrence of {char} to the left
t{char} Till before [count]‘th occurrence of {char} to the right
T{char} Till after [count]‘th occurrence of {char} to the left
; Repeat latest f, t, F or T [count] times
, Repeat latest f, t, F or T in opposite direction [count] times

Up-Down motions

Up-Down motions Describe
j [count] lines downward linewise
k [count] lines upward linewise
⌃Controlu page up
⌃Controld page down
gk [count] display(screen) lines upward
gj [count] display(screen) lines downward
G Goto line [count], default last line
gg Goto line [count], default first line
:[range] Set the cursor on the last line number in [range]
{count}% Go to {count} percentage in the file
H Goto first screen line
L Goto last screen line
M Goto to middle screen line

Word motions

Word motions Describe
w [count] words forward
e Forward to the end of word [count]
b [count] words backward
ge Backward to the end of word
* Goto search for the next occurrence

Text object motions

Text object motions Describe
( [count] sentences backward
) [count] sentences forward
{ [count] paragraphs backward
} [count] paragraphs forward
% move to matching character (default supported pairs: () {} []. use :h matchpairs in vim for more info)

Text object selection

Text object selection commands can only be used while in Visual mode or after an operator.

Text object selection Describe
aw “a word”, select [count] words
iw “inner word”, select [count] words
as “a sentence”, select [count] sentences
is “inner sentence”, select [count] sentences
ap “a paragraph”, select [count] paragraphs
ip “inner paragraph”, select [count] paragraphs
a] “a [] block”, select [count] ‘[’ ‘]’ blocks
i] “inner [] block”, select [count] ‘[’ ‘]’ blocks
ab “a () block”, select [count] blocks
ib “inner () block”, select [count] blocks
a> “a <> block”, select [count] <> blocks
i> “inner <> block”, select [count] <> blocks
at “a <tag></tag> block”, select [count] tag blocks
it “inner <tag></tag> block”, select [count] tag blocks
aB “a {} block”, select [count] blocks
iB “inner {} block”, select [count] blocks
a' ‘a quoted string’. Selects the text bettwen quotes
i' Like a’, but exclude the quotes
a" “a quoted string”. Selects the text bettwen quotes
i" Like a”, but exclude the quotes
a` `a quoted string`. Selects the text bettwen quotes
i` Like a`, but exclude the quotes |

Jump motions

Jump between lines and files. Use m to set a mark.

Marks Describe
m{a-z} Set mark with name {a-z} the current file(jump only in current file)
m{A-Z0-9} Set mark with name {A-Z0-9} in global(jump between mutilple files)
gf This is not a mark but you can open file under cursor with it

Jumping to a mark can be done in two ways: `{mark name} or '{mark name}

  • with `: The cursor is positioned at the specified location.
  • with ': The cursor is positioned on the first non-blank character in the line.

:help motion.txt


Key Describe
⌃Controlo jump cursor position history back
⌃Controli jump cursor position history forward


Total 14 modes(7 basic modes, 7 additional modes) in Vim for now, but usually I just use part of them(5 basic modes, 4 additonal modes).

Basic modes

  • Normal mode: run most of Vim commands in the mode
  • Visual mode: like normal mode but extend a highlighted area
  • Command-line mode: run search/filter/external/Ex commands
  • Insert mode: insert text(to buffer), input your contents in this mode
  • Terminal job mode: run terminal with a terminal window

Additional modes

  • Insert Normal mode: Enter ⌃Ctrlo in Insert mode, then it's just like Normal mode but after executing one command it returns to Insert mode
  • Insert Visual mode: Enter ⌃Ctrlo v in Insert mode, then it's just like Visual mode but returns to Insert mode when the Visual selection ends
  • Replace mode: Like Insert mode but for each character you enter, one characte of the existing text is deleted
  • Operator pending mode: Like Normal mode but after an operator command has started, and Vim is waiting for a {motion} to specify text that the operator will work on

:help vim-modes

Diagram is easy to show how to switch bettwen different modes:

Use ⎋Escape switching back to Normal mode in any other modes.

Normal mode Describe
i start Insert mode: insert before the cursor
I start Insert mode: insert at the beginning of the line
a start Insert mode: insert (append) after the cursor
A start Insert mode: insert (append) at the end of the line
s start Insert mode: delete character and substitute text
S start Insert mode: delete line and substitute text (same as cc)
o start Insert mode: append (open) a new line below the current line
O start Insert mode: append (open) a new line above the current line
cc start Insert mode: change (replace) entire line
C start Insert mode: change (replace) to the end of the line
v start Visual mode: mark lines, then do a command (like y-yank)
V start Visual mode: mark with linewise, then do a command
⌃Controlv start Visual mode: mark with block, then do a command
zz center cursor on screen
r replace a single character
gt move to the next tab
⌃ Controla increment the next number
⌃ Controlx decrement the next number
: start Command-line mode: run vim command
/ start Command-line mode: search for pattern
? start Command-line mode: search backward for pattern
! start Command-line mode: run external command
Insert mode Describe
⌃Controlo start Insert Normal mode
⌃Controlo v start Insert Visual mode
Visual mode Describe
"*y copy selected content to external clipboard
Command-line mode Describe
bn switch to next buffer
bp switch to pre buffer
terminal open terminal window
w write buffer to file
q quit vim
wq write buffer to file then quit

complete cheat sheet


Macros in vim is powerful so much. There are some tips to it.

Macros Describe
qa record macro a
q stop recording macro
@a run macro a
@@ rerun last run macro
. it's not a really macros but looks liek a macros. it repeats the last change you made(or repeat the Operator you used) such as deleting a word/adding new string.

save macros

  1. create macros with name, such as a

  2. copy macros from register to vim config with "{register} e.g. "ap at normal mode to paste macros a

  3. name that macros with a new var: let @d = '...'

  4. use it with @d at the normal mode

apply macros to multiple lines

  1. ⇧ Shiftv to select multiple lines in visual mode

  2. : and input normal @a (a is the name of your macros)


Snippets makes life easy. I use ultisnips to manage all snippets.

You can find basic examples for ultisnips with Google (or full doc on github repo) like:

hello $0

But I want to show you some tips which are more interesting in here.

match with regex

snippet /kbd([a-z0-9A-Z])/ "kbd" ri
<kbd>`!p snip.rv =`</kbd>$0

"/.../ means regex string
"r means Regular expression
"i means In-word expansion
"`!p ...` means Python script

this snippet matches the string like kbda kbdb kbd0 kbdS and complete them to <kbd> tag:

kbda: <kbd>a</kbd>
kbdb: <kbd>b</kbd>
kbd0: <kbd>0</kbd>
kbdS: <kbd>S</kbd>


We use parentheses, brackets, quotes, XML tags a lot when we are coding. so it's necessary to do it more efficiently. I use vim-surround for that. It extends new operator commands for surroundings:

Operator Describe
ys add new surroundings
ds delete exist surroundings
cs replace exist surroundings


  • ysiW": add " to surround current WORD
  • ds": remove " of current word/WORD/line
  • cs'": replace current surrond from ' to "