grokking vim

wednesday, 16 december 2015

Vim is a modal editor – with insert (writing) and command modes – that persists despite the evolution of graphical editors, many of which are equally powerful and have become de facto standards on their respective platforms. TextMate comes to mind for OSX and Sublime Text another for that and other platforms. Sublime Text even has a vi modal emulation which would probably entice me to use it more, except for two important addons – amongst the many I use – that Vim has: vimwiki, which this site is built upon and, Goyo (and before that, VimRoom), which presents a clutter free display environment for writing.

The modal aspect of Vim is the most difficult aspect to grasp for users who have been weaned on windowing environments. Graphical interface standards allow users to grope applications right out of the box, editors included, using the mouse to navigate the various menus and visual text area. A modal editor doesn’t really lend itself to that sort of exploration – though, there is a GUI version of Vim (but that would sabotage thinking in terms of Vim actions versus tedious massaging with mouse gestures).

A modal keyboard driven editor may go against the grain but there are advantages: agility, power and negligible mouse fumbling. This requires practice and exercising one’s memory – not necessarily a bad thing. The added benefit is that many other modal applications like the Luakit browser, map their keys similarly, just as menus tend to be similar.

Much like making a decision to switch to the Colemak keyboard layout for greater typing efficiency and ease, going modal begins to make sense once you use it for awhile. But you have to give it a chance – and then you will likely never turn back. The keyboard shortcuts for the most part are easy enough to remember – d for delete, i for insert, etc.

There are numerous Vim cheat sheets and tutorials available on the web, as well as, a built in :help tutor within the editor itself. The following keystroke samples illustrate just some of the power of Vim – using a mouse to emulate many of the complex actions is just not possible. Actions prefixed with an ellipsis (in the tables below) can accept a numeric multiplier before the keystroke to further extend the reach of the command. The key sequences look daunting at first, but being able to operate on words, sentences, paragraphs and strings is much more nimble than highlighting cautiously with a mouse.

[modifier] action

{number} repeat number of times
h … left
j … down
k … up
l … right
w … next word
e … end of word
b … previous word
0 start of line
  first non-blank character of line
$ end of line
( start of sentence
) end of sentence
{ start of paragragh
} end of paragraph
fx … next character x
Fx … previous character x
tx … before next character x
Tx … before previous character x
; … repeat f F t T
, … repeat f F t T in opposite direction
1g or gg start of file
0g or G end of file
ng go to line n
ma mark current position with letter a

:marks to list marks


[modifier] action

{number} repeat number of times
/re … search to string expression re
?re … search backwards to string expression re
n … next result
N … previous result
* … search for word under cursor
# … search backwards for word under cursor
gd go to 1st occurence of word under cursor
% jump to end of matching brackets (or meta-tags – requires match-it plugin)


[modifier] action

{number} repeat number of times
a insert after
A append to end of line
i insert before
I insert at beginning of line
o insert at new line below
O insert at new line above
r … replace character
R replace mode
u … undo last action
U … redo undone action
. … repeat last action
qa begin recording macro labelled letter m
@a … playback macro labelled m
v visual mode

copying, cutting and pasting

[modifier] action [scope]

{number} repeat number of times
“r paste from or hold copy of yanked or replaced text in register letter r
y copy selection
yy … copy line
ym … copy scope m
d delete selection
dd … delete line
D delete to end of line
dm … delete scope m
x … delete character
X … backspace
C change to end of line
cm … change scope m
p … paste after
P … paste before
tx to character x
aw a word
w to end of word
aW a word including punctuation
W to end of word including punctuation
as a sentence
is inner part of sentence
ditto p paragraph
ditto t tag
ditto b block
a( or a) a parenthesis
i( or i) inner part of parenthesis
ditto […] square brackets
ditto <…> angle brackets
ditto {…} curly brackets
a” or a’ or a` a quote
i” or i’ or i` inner part of quote
/re to string expression re
?re backwards to string expression re
`m to mark position m
‘m to start of line at mark position m

:registers to list registers


: [position [,position]] s/match/replace/ [options]

{number} absolute line number
. or {nil} current line
$ last line in file
% equal to 1,$ (entire file)
* equal to ‘<,’> (Visual area)
‘m line at mark position m
/ {pattern} [/] next line where {pattern} matches
? {pattern} [?] previous line where {pattern} matches
\/ next line where previously used search pattern matches
\? previous line where previously used search pattern matches
\& next line where previously used substitute pattern matches
c confirm before substitution
e no error messages if pattern not found
g replace all occurrences in the line
i ignore case
I do not ignore case
n do not substitute

The pattern matching and replacement possibilities are well beyond what casual writers require (but not programmers). Regular expressions are well worth becoming familiar with for facilitating broad changes to documents beyond simple corrections.


zO open all nested folds
zr open highest level fold
zR open all folds of all levels
zc close all folds
zm close all highest level of open folds
zM close all folds of all levels
space toggle current fold level


zt move current line to top of screen
zz move current line to middle of screen
zb move current line to bottom of screen
ctrl-d scroll half screen down
ctrl-e scroll screen up
ctrl-u scroll half screen up
ctrl-y scroll screen down

No write up on vim would be complete without reference to perhaps the most eloquent explanation of the power of vi, vim’s ancestor – which is not to say that vi itself is outdated or has outlived its use. vi is still by default available on most every *nix install, hence, extremely useful to know.

Any aspiring vim user should read and reread “Your problem with Vim is that you don’t grok vi” until you gain a fluency with vi’s language.

I do not know of any editor that can match vi and vim’s text manipulation power with just a few keystrokes (save those with vi emulation modes and emacs, and even the venerable emacs has a vi emulation mode). And that is just it: writing power at your fingertips.

»»  colemak colours

comment ?