Skip to content

Instantly share code, notes, and snippets.

@weshouman
Created December 2, 2023 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weshouman/1e8d485ea5cf020a4b9c113e3f84a09f to your computer and use it in GitHub Desktop.
Save weshouman/1e8d485ea5cf020a4b9c113e3f84a09f to your computer and use it in GitHub Desktop.
vim tips and tricks ep001

Using a macro to add double quotations over multiple lines in vim

  1. Select the Lines of Interest:

    • Move the cursor to the start of the first line where you want to perform the operation.
    • Enter Visual Line mode by pressing Shift+V.
    • Use the up or down arrow keys to extend the selection to cover all lines of interest.
  2. Start Recording the Macro:

    • Press q followed by a letter (like a) to start recording the macro into register a.
  3. Append the Character to the Word:

    • Move to the end of the Word by pressing E. This takes the cursor to the end of the current Word.
    • Enter insert mode at the current cursor position by pressing a.
    • Type the character you want to append.
    • Press Esc to return to normal mode.
    • To ensure the cursor is positioned correctly for multiple runs, press B to go back to the beginning of the Word.
  4. Move to the Next Line and Stop Recording:

    • Move to the next line with j.
    • Press q again to stop recording the macro.
  5. Apply the Macro to the Selected Lines:

    • With the lines still selected in Visual Line mode, apply the macro by typing : which will bring up :'<,'>. Then type norm @a to apply the macro to each line in the selection. The full command will be :'<,'>norm @a.
    • To apply the macro to a range of lines, use :[range]norm @a, where [range] is the line range (e.g., 5,10 for lines 5 to 10).
    • To apply the macro to every line in the file, you can use :%norm @a.
  6. Save Your Changes:

    • Once you have finished, save your changes with :w.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment