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
qa— start recordingI<li>— insert opening tag at line start<Esc>— back to normal modeA</li>— append closing tag at line end<Esc>j— back to normal, move downq— stop recording
Example
Apple
Banana
Cherry
After 3@a:
<li>Apple</li>
<li>Banana</li>
<li>Cherry</li>
Tips
- Change
lito any tag (p,div,span, etc.) - Add
O<ul><Esc>before ando</ul><Esc>after for a complete list - Use visual block mode for simpler wrapping
- The vim-surround plugin does this with
ysst<li>