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

How do I record a macro to wrap each line in HTML tags?

Answer

qaI<li><Esc>A</li><Esc>jq

Explanation

This macro wraps each line in <li> tags by inserting the opening tag at the start and appending the closing tag at the end.

How it works

  1. qa — start recording
  2. I<li> — insert opening tag at line start
  3. <Esc> — back to normal mode
  4. A</li> — append closing tag at line end
  5. <Esc>j — back to normal, move down
  6. q — stop recording

Example

Apple
Banana
Cherry

After 3@a:

<li>Apple</li>
<li>Banana</li>
<li>Cherry</li>

Tips

  • Change li to any tag (p, div, span, etc.)
  • Add O<ul><Esc> before and o</ul><Esc> after for a complete list
  • Use visual block mode for simpler wrapping
  • The vim-surround plugin does this with ysst<li>

Next

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