Run Macro On Multiple Lines in Vim
Modern editors like VS Code or Sublime Text can use multiple cursors to edit code on more than one line.
Vim doesn’t offer this functionality out of the box.
Today I learned how to run a macro to edit several lines in Vim.
The method is useful when you have a similar structure on several lines, for example:
<li class="link">About</li>
<li class="link">Blog</li>
<li class="link">Works</li>
Add the script visual-at.vim to your vim configuration file.
- Go to the first line you want to edit, start at position 0.
- In normal mode, hit
q
and a letter to start recording the macro. - Now, do your edits. Use motions intelligently, for example, use
w
to move forward to the beginning of the word. - Switch back to normal mode, then hit
q
to exit recording the macro. - Go to the other lines you’ll like to edit and select them with
V
. - Hit
@
and the letter you used to record the macro in step 2.
Further Reading
- You don’t need more than one cursor in vim by Christoph Hermann
Read other posts