Vim: Substituting Cheat Sheet

For each line in [range] replace a match of {pattern} with {string}

:[range]s[ubstitute]/{pattern}/{string}/[c][e][g][p][r][i][I] [count]

Repeat last :substitute with same search pattern and substitute string, but without the same flags

:[range]s[ubstitute] [c][e][g][r][i][I] [count] :[range]&[c][e][g][r][i][I] [count]

You may add extra flags.

The arguments that you can use for the substitute commands:

  • [c] Confirm each substitution. Vim positions the cursor on the matching string. You can type:
    • y to substitute this match
    • n to skip this match
    • a to substitute this and all remaining matches
    • q to quit substituting
    • CTRL-E to scroll the screen up
    • CTRL-Y to scroll the screen down
  • [e] When the search pattern fails, do not issue an error message and, in particular, continue in maps as if no error occurred
  • [g] Replace all occurrences in the line. Without this argument, replacement occurs only for the first occurrence in each line
  • [i] Ignore case for the pattern
  • [I] Don’t ignore case for the pattern
  • [p] Print the line containing the last substitute

Leave a Reply

Your email address will not be published. Required fields are marked *