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.

But you may not need it.

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.

  1. Go to the first line you want to edit, start at position 0.
  2. In normal mode, hit q and a letter to start recording the macro.
  3. Now, do your edits. Use motions intelligently, for example, use w to move forward to the beginning of the word.
  4. Switch back to normal mode, then hit q to exit recording the macro.
  5. Go to the other lines you’ll like to edit and select them with V.
  6. Hit @ and the letter you used to record the macro in step 2.

Further Reading