This post is a part of a chapter from my book Mastering Vim Quickly: From WTF to OMG in no time.
I believe you have already heard of the principle Don’t Repeat Yourself.
In software engineering, this is a principle of software development where your focus is on reducing repetition of all kinds.
Vim has many ways and commands to automate different kinds of tasks, so you don’t have to repeat your actions.
One of the most powerful Vim command when it comes to avoiding repetition is the . (“the dot”) command.
Hitting . in Normal mode will repeat the last native Vim command you’ve executed.
Let’s say you want to delete 5 words from the cursor forward. As you already know, you could press 5dw and it’s done. However, sometimes it’s not convenient to mentally count the number of words.
An alternative would be to use dw to delete one word. And then press …. to call the dot command four times. In this case, you would repeat the latest, dw command, four more times, and in this way achieve the same effect without counting the words.
If you used dd to delete a line, and you want to delete 4 more lines, you could also execute 4. instead of pressing …. . That also works.
It’s very important to understand what is actually repeatable by the dot command. For example, if you have a sample code like this:
my $i
my $learn
my $quickly
and your cursor is positioned on the first line. You want to append ; to all three lines. You could hit a sequence of keys like: A;Escj
• A — would place your cursor at the end of the first line in Insert mode.
• ; — you press to actually insert it, and then you press Esc to get back to Normal mode.
• j — to move one line down
Now, your cursor is at the second line. If you then press . to repeat the change in next (second) line, this won’t work. Here’s what you’d get:
my $i;
my $learn;
my $quickly
Your cursor will still be at the second line rather than on the third line, but ; will be appended. This brings us to conclusion that only this part of our original command was repeated: A;Esc
Now, why is this the case? It’s important to remember that with the dot command, you can repeat the commands which change the contents of the buffer.
A change is any command which you can use to modify your text. In our example, we had the case that command j wasn’t repeated, and our cursor wasn’t moved to the third line.
Commands like j are called motions (or nouns) — and they don’t affect the text itself. Command j just moves the cursor, but doesn’t change text anyhow, so it can’t be repeated.
Think in terms of the grammar of your native language: Not nouns, but verbs are used to express some sort of action. The same is true in Vim: nouns (or motions) can’t affect the text, so they can’t be repeated with the dot command.
Of course, if you’d like to repeat multiple changes, or a combination of movements and changes, you can easily record those into a macro.
You can learn all you need on macros from Macros chapter of my book Mastering Vim Quickly. To see all the commands which can affect the text in a buffer, take a look at :help change.txt
—
Still reading? Every week I publish awesome tips on Vim in my Mastering Vim Quickly Newsletter to thousands of Vim fans. Join us.
Originally published at jovicailic.org