← back to latest

Repeat the last change anywhere in the file

NORMAL.Repeat the last change anywhere in the file

anatomy

.
Repeat. Executes the last change command at the current cursor position.

In the buffer

Before:

let foo = old_value
let bar = old_value
let baz = old_value

After using ciw to change the first old_value to new_value, then pressing . on the other lines:

let foo = new_value
let bar = new_value
let baz = new_value

When you would reach for it

You just made a change. A good one. Now you want to make the exact same change somewhere else. Maybe it is changing a word, deleting a line, inserting a character, or indenting a block. Whatever it was, . will do it again at the current cursor position.

This is the engine behind most efficient Vim editing. Make the change once, move the cursor, repeat. No macros, no search-and-replace, no highlighting or mousing. Just do it once, then . your way through the rest.

Gotchas

  • The dot command repeats the last change, not the last motion. Moving the cursor does not update what . will do.
  • If your last change was a complex multi-step operation (like c3w followed by typing several words), . replays the whole thing: it will change the next three words and insert the exact same text.
  • Exiting insert mode ends the change. If you enter insert mode, type a sentence, leave insert mode, move around, and then hit ., it replays the entire insertion, not just the last character.
  • Visual mode selections followed by an operator (like vwd) are not repeatable with dot. Use text objects instead (daw) and dot works perfectly.

Variants

Repeat the last Ex command (@:): While . repeats normal mode changes, @: repeats the last command-line command. Useful after running something like :s/old/new/ and wanting to apply it to another line.

Repeat with a count (3.): Repeats the last change three times. If your last change was deleting a word (dw), typing 3. deletes three more words.

The dot command with macros: You can record a macro that includes a change, then use . within the macro to repeat the change. This stacks repeatability: the macro repeats ., which repeats the change.

The dot command is why experienced Vim users structure their edits to be repeatable. Instead of making three small changes in insert mode, they make one change, leave insert mode, move, and dot. It trades a little upfront discipline for a lot of downstream speed.

lineage

The dot command is one of the original vi operators, present since the first release in 1976. Bill Joy called it the most powerful command in vi.

-- INSERT --vim-hacks.com008-008-dot.html
utf-81,1All