Vim grammar
Introduction
Below is the vim grammar in Normal Mode, similar to a context-free language. The vim grammar has the following form:
- command -> nrepeats? (cursorto | operation | lineoperation | toinsertmode | replaylastop)
- cursorto -> location
- location -> | line-below | line-above | ….
- operation -> cursorto | verb text-object?
- verb -> change | delete | yank
- lineoperation -> repeatedverb
- repeatedverb -> change-change | delete-delete | yank-yank
- nrepeats -> digit | digit nrepeats
- digit -> 0 | 1 | … | 9
- toinsertmode -> insert | insertbelow | append | …
- text-object -> (inside | around) delimiter
- repeatlastop -> .
In the above grammar, the ? key means that the variable is optional, and can have atmost one, similar to its use in regular expressions.
We start with location commands, and split them into sections below.
Crawling
- line-below ->
j
- line-above ->
k
- char-before ->
h
- char-after ->
l
Walking
- next-word ->
w
- previous-word ->
b
- next-end-of-word ->
e
- toggle-surrounding-(,[,{ ->
%
Driving
- find-next/prev-[c]har-in-line ->
f[c]/F[c]
- to-before-next/prev-[c]har-in-line ->
t[c]/T[c]
- repeat-last-char-in-line-command ->
;
- first-char-in-line ->
^
- last-char-in-line ->
$
- next-occurence-of-word-under-cursor ->
*
Teleporting
- search-forward-regexp ->
/regexp[ENTER]
- search-backward-regexp ->
/regexp[ENTER]
- next-match-of-last-search ->
n
- previous-match-of-last-search ->
N
- first-line-of-file ->
gg
- line-#-of-file ->
[line#]G
- last-line-of-file ->
G
Next, there are operation commands.
- change ->
c
- delete ->
d
- yank ->
y
This is followed by lineoperations which simply perform the commands above for the whole line.
- change-change ->
c c
- delete-delete ->
d d
- yank-yank ->
y y
We can scale the above commands any finite number of times using the
nrepeat expression, for example 3 w
moves forward 3 words, or 4 d j
to delete 4 lines below from current line to below, or 5 p
to paste
5 times what was yanked previously.
Moving to Insert Mode is typically done using the following expressions.
- insert ->
i
(Insert before current cursor entry,I
to do at line start) - insertbelow ->
o
(Insert to new line below current line,O
to do above) - append ->
a
(Insert to after current cursor entry,A
to do at line end)
Text inside all kinds of parentheses, inverted commas, or any surrounding delimiters is regarded in vim as a Text object, and we can apply operations to them. From the grammar we see that this is optionally invoked after a verb.
- inside ->
i
(Perform verb inside the surrounding keys) - around ->
a
(Perform verb including the surrounding keys) - delimeter ->
)
,]
,}
,"
,\
,`
,>
,w
,p
(w -> word, p -> paragraph)
All actions performed above can be repeated using .
, for example if you changed
a word via c w new thing
, typing .
on another word also changes it to "thing".