vimtricks.wiki Concise Vim tricks, one at a time.

How do I record a macro to add semicolons at the end of every line?

Answer

qaA;<Esc>jq

Explanation

This macro appends a semicolon to the current line and moves down, ready to repeat.

How it works

  1. qa — start recording to register a
  2. A; — append semicolon at end of line
  3. <Esc> — return to normal mode
  4. j — move to the next line
  5. q — stop recording

Example

let x = 1
let y = 2
let z = 3

After qa A;<Esc>jq then 2@a:

let x = 1;
let y = 2;
let z = 3;

Tips

  • 100@a adds semicolons until end of file (stops at last line)
  • Check first that lines do not already have semicolons
  • A is better than $a because it works on empty lines too
  • Modify the macro for different suffixes (commas, colons, etc.)

Next

How do you yank a single word into a named register?