Vim `.` Command
The .
command in Vim is incredibly powerful for repeating actions. It re-executes the last change you made, whether it was a complex macro, a simple deletion, or an insertion. This can dramatically speed up your workflow, especially for repetitive tasks.
Here’s a breakdown:
Repeating Changes: Imagine you need to add “//” at the beginning of multiple lines. You could manually do it, or you can use the
.
command. Add “//” to the first line, then press.
on each subsequent line to repeat the insertion.Complex Edits: The
.
command isn’t limited to simple insertions or deletions. If you make a complex edit involving multiple commands (e.g., deleting a word, inserting text, changing capitalization), pressing.
will repeat the entire sequence.Using with other commands: Combine
.
with other Vim commands for even greater efficiency. For example, you can delete a word withdw
and then repeat the deletion on other occurrences of the word by pressingn
(to find the next occurrence) and then.
.Macros and the
.
command: While macros are powerful for complex repetitions, sometimes the.
command offers a simpler alternative. If your repetition involves a single change or a small sequence of changes,.
might be quicker than defining and executing a macro.
Example Scenario:
Let’s say you have the following text:
apple
banana
orange
grape
You want to change it to:
fruit: apple
fruit: banana
fruit: orange
fruit: grape
- Position the cursor at the beginning of the first line.
- Type
ifruit: <Esc>
(insert “fruit: ” at the beginning of the line). - Move the cursor to the next line (using
j
or the down arrow). - Press
.
. Vim will repeat the insertion, adding “fruit: ” to the second line. - Repeat steps 3 and 4 for the remaining lines.
This simple example demonstrates the power of the .
command. Instead of manually typing “fruit: ” on each line, you efficiently repeat the initial insertion, saving significant time and effort. This principle applies to much more complex edits as well, making the .
command an essential tool in any Vim user’s arsenal.