I’ve been using Vim for a long time and I’m familiar with many of its commands but I’ve never had a good understanding of the underlying components of commands and their composition. Although there are many good Vim references, I felt that I was still not seeing the full picture. So, to try and dig a little deeper and educate myself, I’ve put together this reference, which details a subset of the command language. The approach I’ve taken was inspired by the grammar rules outlined in this blog post, and should be useful for anyone who is familiar with Vim.

A Vim command can be constructed from different rules, and below each rule is specified with a EBNF-style syntax, where <...> is another rule. {...} represents some key presses, | is an alternative and [...] is an optional component. Be warned that since there are many special cases to Vim commands and I’ve not checked all the combinations, some of the rules below may yield invalid compositions! As such, these rules are better viewed as a way of remembering commands. For an exhaustive treatment of the command language, please check the Vim documentation, or use the :help tags that I’ve given.

Motions

Motions change the position of the cursor and can be combined with a count to repeat the motion.

count := {digits}
motion := [<count>] <motion-keys>
command := <motion>

Examples:

Text objects

Text objects are a collection of characters relative to the position of the cursor. Compared to motions, text objects on a whole object, regardless of the specific cursor position. The a and i modifiers augment text objects, specifying whether the surrounding context should be included. The context may be whitespace or delimiting characters.

text-object := <text-object-keys>
             | <modifier> <text-object-keys>

Examples:

Operators on text objects

Operators can be applied to text objects. In some circumstances, a count can be applied to the text object to repeat its effect.

command := [<count>] <operator> <text-object>
text-object := <count> <modifier> <text-object-keys>

Examples:

Operators on motions

A motion can be applied after an operator to apply the operator on the text that was moved over.

command := [<count>] <operator> <motion>

Examples:

Duplicate operators

Operators applied twice affect the entire line, a synonym for <operator>_,

command := [<count>] <operator> <operator>

Examples:

Note: duplication does not apply to ~ or two-character operators.

Aliases

Some commonly-used commands have aliases.

Filtering

Text lines can be filtered through an external program (see :help filter).

command := ! <motion> <filter>

Examples, using some basic utilities found on Unix platforms:

Visual selection

Visual selection, character-wise v, line-wise V or block-wise Ctrl+v (all referred to below by {visual}, see :help visual-start), followed by a motion or text object can be used to specify a character range. An operator can then be used to transform the text. Note the operators gu, gU and g@ can’t be used in visual mode and text objects in visual mode, however, there are additional ones that can (see :help visual-operators). A visual block is created by entering a visual mode, then providing a motion or text object to set the selection, or alternatively by any sequence of movement commands (referred to by {move-around}).

visual-block := {visual} {move-around}
              | {visual} <motion>
              | {visual} <text-object>
command := <visual-block> <operator>
         | <visual-block> <visual-operator>
         | <visual-block> ! <filter>

Examples:

Other command combinations

Beyond the above rules, there are further command keys and more restricted combination with motions, operators and text objects.

The . command repeats the last change that was made.

command := [<count>] .

The gn and gN motions can be used with operators to move between matching search patterns.

command := [<count>] <operator> gn
         | [<count>] <operator> gN

Here are some other interesting and potentially useful commands that I’ve found in the Vim help:


Summary of rules

Motions

<motion-keys>

References: :help motion :help various-motions

Action Effect
l,h,j,k right, left, down, up
w / W start of next word / start of next WORD
b / B start of pevious word / start of previous WORD
e / E end of next word / end of next WORD
0 / $ start / end of line
^ / g_ first / last non-blank character in line
{ / } start / end of paragraph
t{char} till - move to next character (inclusive)
f{char} find - move to next character (exclusive)
T{char} till - move to previous character (inclusive)
F{char} find - move to previous character (exclusive)
% move to matching character (default pairs: (), {}, [])
[(, [) move to matching parenthesis
[{, [} move to matching curly brace
/{pattern} search forward (must be followed by {CR})
?{pattern} search backwards (must be followed by {CR})
gg / G move to first / last line in buffer

Operators

<operators>

Reference: :help operator

Key Operator
c change
d delete
y yank
~ swap case (or g~)
> shift right (always linewise)
< shift left (always linewise)
gu make lowercase
gU make uppercase
gq format text
g@ apply function (set by operatorfunc)

Visual operators

<visual-operators>

References: :help visual-operators

Key Operator
r{char} replace every character in selection with {char}
s, c substitute (inserted text replicated on each line for blockwise)
J / gJ join / join (remove whitespace)
p put (replace selection with contents of register)
U make uppercase
u make lowercase
I insert (prepend, only on blockwise selection)
A insert (append, only on blockwise selection)

Text objects

<text-object-keys>

References: :help text-objects, :help objects

Key Text object
w word (delimited by non-keyword characters, see :help iskeyword)
W WORD (delimited by whitespace, includes empy line)
s sentence
p paragraph
t tag
[, ] text block between square brackets
{, } text block between curly braces
(, ) text block between parethesises
', ' text block between single quotes
", " text block between double quotes
<, > text block between angle braces
`, ` text block between back ticks
b text block between ( and )
B text block between { and }

Modifiers

<modifiers>

References: :help text-objects

Key Movement
a a or around
i inner

count := {digits}

text-object := <text-object-keys>
             | <modifier> <text-object-keys>
             | <count> <modifier> <text-object-keys>

motion := [<count>] <motion-keys>

visual-block := {visual} {move-around}
              | {visual} <motion>
              | {visual} <text-object>

command := <motion>
         | [<count>] <operator> <text-object>
         | [<count>] <operator> <motion>
         | [<count>] <operator> <operator>
         | ! <motion> <filter>
         | <visual-block> <operator>
         | <visual-block> <visual-operator>
         | <visual-block> ! <filter>
         | [<count>] .
         | [<count>] <operator> gn
         | [<count>] <operator> gN

Some random commands

Just for fun, here are some random commands using the above rules to show the variety of actions you can perform. Some might not make sense or not work, but they might give you some ideas.

Motions

Text objects

Operators on text objects

Operators on motions

[)
T{ch}
^
g_
g_
g_
j
h
T{ch}
4^
%
}
l
B
5[{
j
F{ch}
0
F{ch}
b
j
F{ch}
8%
5}
8e
{
0
[}
t{ch}
3f{ch}
4gg
[(
g_
8j
[{
4h
3%
3w
4gg
B
7g_
j
[(
8F{ch}
?{pat}
[)
k
4w
T{ch}
1l
t{ch}
G
^
?{pat}
0
w
B
e
g_
%
w
G
2l
[(
aw
aW
i"
i>
it
iB
iW
a]
aW
a{
ib
i`
a>
a[
i`
i]
it
i(
i`
aW
iW
i<
aw
a]
i"
a(
i"
iW
a'
iw
ab
aW
a}
i]
is
a{
aB
a]
iW
a]
a]
a'
ap
i'
iW
iw
a'
a<
i}
i"
a"
a(
iw
iB
a`
a(
a[
is
a'
a(
i{
i{
i"
at
3gUi`
<5iw
g@iB
dis
g@9aW
ci{
gui{
>i`
guit
g@a`
g@aB
di)
>2a<
~9aw
gqa"
guib
3gUi<
<1iB
9guaW
gqiW
9di}
g@i(
g~8i<
gUa)
6g@8i'
g~it
g~2iw
gUa'
g@ip
2~i[
guaw
ci"
2g@a{
2dit
dit
yat
yi`
g~a[
gqi)
~i(
~iB
g~ab
~aW
d2aW
5~i>
gUi[
ci(
guit
5g@1a'
g@i>
<at
di[
7>0iW
2<iW
d0ib
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