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

How do I create a macro that converts lines to a numbered list?

Answer

qa I1. <Esc>j q

Explanation

This simple macro inserts a list number prefix at the beginning of each line. Combined with visual block increment, it creates proper numbered lists.

How it works

  1. qa — start recording
  2. I1. — insert 1. at the beginning of the line
  3. <Esc> — return to normal mode
  4. j — move to the next line
  5. q — stop recording

Example

apple
banana
cherry

After qa I1. <Esc>jq and 2@a, then fix numbers with visual block and g<C-a>.

Tips

  • Use the counter technique for proper auto-numbering
  • Or apply 1. to all lines then use g<C-a> to fix numbers
  • For markdown lists, use - instead of 1.
  • Combine with :g/pattern/normal @a for selective application

Next

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