Vim Recipes ‣ Editing ‣ Repeating Commands
You've entered a command and want to repeat it without re-typing it.
The period . repeats the last command entered in Normal mode. For example, dd deletes the current line; dd.. deletes the current line, then deletes the new current line, then deletes the new current line. In other words, it repeats the command twice.
To repeat a command entered in Command mode hit :, then ↑ to scroll back through your command history. If you type the first few letters of the command before using ↑, the history will be limited appropriately.
The period command helps automating repetitive tasks with the fewest
keypresses. It lets you say do that again
, but in only one character.
If you know you want to execute a command n times, you can prefix it with the integer n. The above example rewritten in this way is 3dd.
The second approach requires fewer keystrokes so is clearly preferable if you know in advance how many times you want to repeat a command. However, the period command lets you make that decision incrementally, after executing the command.
You can combine these approaches by prefixing the period command with an
integer to say do that n times again
:
n.. Be aware that having done this, if you
use the period command again it will repeat your previous repetitions, i.e.
command, followed by
n., followed by . will
result in command being executed 2n + 1 times.
The discussion above assumes that the last command needs to be repeated exactly. Instead, you may wish to execute a different command on the same text. Jump to the line of the last change with the Normal mode command '., then make that change.