Vim Tip #27: Repetition on a Range

2020-06-10(Wed)

tags: Vim

Vim Tips

Turning line numbers on and off is useful here: see Toggle Line Numbers on and off.

The title of this entry is somewhat misleading: this Vim Tip will definitely allow you to repeat an action on a section of a file, but it will also allow you to repeat any Normal Mode action. The dot command (.) allows you to repeat your last Normal Mode action. I've spent years doing j.j.j.j.j. to repeat a change on several lines. But there's an easier way: :15,20normal . . That means "execute the Normal Mode dot command on lines 15 through 20." This also works on a visual selection: :'<,'>normal .. And it's not limited to the dot command. If you wanted to add a ';' to the end of every line in a function, just get the line numbers and do: :72,91normal A;. The A; part of that command temporarily enters Insert Mode, but Vim automatically switches back to Normal mode after it's done so we don't have to tell it to hit <ESC>.