Practical Vim, Second Edition: Extracted From
Practical Vim, Second Edition: Extracted From
This PDF file contains pages extracted from Practical Vim, Second Edition, published
by the Pragmatic Bookshelf. For more information or to purchase a paperback or
PDF copy, please visit http://www.pragprog.com.
Note: This extract contains some colored text (particularly in code listing). This
is available only in online versions of the books. The printed versions are black
and white. Pagination might vary between the online and printed versions; the
content is otherwise identical.
Copyright 2015 The Pragmatic Programmers, LLC.
Drew Neil
The dot command lets us repeat the last change. It is the most powerful and
versatile command in Vim.
Vims documentation simply states that the dot command repeats the last
change (see :h . ). It doesnt sound like much, but in that simple definition
well find the kernel of what makes Vims modal editing model so effective.
First we have to ask, What is a change?
To understand the power of the dot command, we have to realize that the
last change could be one of many things. A change could act at the level of
individual characters, entire lines, or even the whole file.
The x command deletes the character under the cursor. When we use the
dot command in this context, repeat last change tells Vim to delete the
character under the cursor:
We can restore the file to its original state by pressing the u key a few times
to undo the changes.
The dd command also performs a deletion, but this one acts on the current
line as a whole. If we use the dot command after dd , then repeat last change
instructs Vim to delete the current line:
Finally, the >G command increases the indentation from the current line until
the end of the file. If we follow this command with the dot command, then
repeat last change tells Vim to increase the indentation level from the current
position to the end of the file. In this example, well start with the cursor on
the second line to highlight the difference
The x , dd , and > commands are all executed from Normal mode, but we also
create a change each time we dip into Insert mode. From the moment we
enter Insert mode (by pressing i , for example) until we return to Normal mode
(by pressing <Esc> ), Vim records every keystroke. After making a change such
as this, the dot command will replay our keystrokes (see Moving Around in
Insert Mode Resets the Change, on page ?, for a caveat).
Well see a few applications of the dot command throughout this chapter.
Well also learn a couple of best practices for working with the dot command
in Tip 9,Compose Repeatable Changes, on page ?, and Tip 23,Prefer Operators
to Visual Commands Where Possible, on page ?.
Tip 2
For such a common use case as appending a semicolon at the end of a series
of lines, Vim provides a dedicated command that combines two steps into one.
To finish the job, we could run the exact same sequence of keystrokes on the
next two lines, but that would be missing a trick. The dot command will repeat
that last change, so instead of duplicating our efforts, we could just run j$.
twice. One keystroke ( . ) buys us three ( a ;<Esc>). Its a small saving, but these
efficiencies accumulate when repeated.
But lets take a closer look at this pattern: j$. . The j command moves the
cursor down one line, and then the $ command moves it to the end of the
line. Weve used two keystrokes just to maneuver our cursor into position so
that we can use the dot command. Do you sense that theres room for
improvement here?
Although this formula looks terrific for our short example, its not a universal
solution. Imagine if we had to append a semicolon to fifty consecutive lines.
Pressing j. for each change starts to look like a lot of work. For an alternative
approach, skip ahead to Tip 30,Run Normal Mode Commands Across a Range,
on page ?.
If you catch yourself running ko (or worse, k$a<CR> ), stop! Think about what youre
doing. Then recognize that you could have used the O command instead.
Did you identify the other property that these commands share? They all switch from
Normal to Insert mode. Think about that and how it might affect the dot command.
Tip 3
We can pad a single character with two spaces (one in front, the other behind)
by using an idiomatic Vim solution. At first it might look slightly odd, but the
solution has the benefit of being repeatable, which allows us to complete the
task effortlessly.
The s command compounds two steps into one: it deletes the character under
the cursor and then enters Insert mode. Having deleted the + sign, we then
type + and leave Insert mode.
One step back and then three steps forward. Its a strange little dance that
might seem unintuitive, but we get a big win by doing it this way: we can
repeat the change with the dot command; all we need to do is position our
cursor on the next + symbol, and the dot command will repeat that little
dance.
Having made our first change, we could jump to the next occurrence by
repeating the f+ command, but theres a better way. The ; command will
repeat the last search that the f command performed. So instead of typing
f+ four times, we can use that command once and then follow up by using
the ; command three times.
Instead of fighting Vims modal input model, were working with it, and look
how much easier it makes this particular task.