What does “n.n.n.n.” do in Vim?
n repeats the last search.
. repeats the last change.
So, let’s say we have this file and we want to extract out just the names.
We are going to move the cursor to the first
: by typing
/:<RETURN>, then press
D to delete to the end of line. Then we want to repeat that over and over.
To repeat those two steps we could type
/:<RETURN> again, then
D again, 6 more times. However, we could use
n to repeat the search and
. to repeat the change. That is, we type
n. over and over until the task is done. i.e.
As a result we end up with this list cleaned up as
There are other ways to do it, but pressing
. is kind of fun.
We might also want to remove the whitespace at the front of each line. In that case we do something similar. We move to the first line and type
0 to get to the first column (the 0th column), then
2x to delete the 2 chars (the 2 whitespaces).
FYI: You can prefix most vim commands with a number to repeat it that many times. In this case
2x will do
x (delete the char under the cursor) twice.
Now we want to repeat this sequence of
j2x. We can type
j to move to the next line, then
. to repeat the change (delete 2 chars).
The result should look like this:
In this case we didn’t use
j is a perfectly fine way to get to the next line.
. you could have typed
x twice. However I find it useful to get into a rhythm of “move dot move do move dot”. Whether the move is
j, if you do this often enough you’ll develop muscle memory and it will become second nature.
Two keystrokes (
j.) is a 33% savings over three (
j2x) but most importantly it is less brainwork because it ends in
. which is always in the same place on the keyboard.
As an exercise, try removing the double-quotes from each line. There are multiple ways to do it. Try using some combination of a move to the next double-quote and
. to repeat the change of moving it.