Skip to content

Instantly share code, notes, and snippets.

@AirManH
Last active December 11, 2021 08:16
Show Gist options
  • Save AirManH/41a77f61eca53fef3d77dacf1683fd18 to your computer and use it in GitHub Desktop.
Save AirManH/41a77f61eca53fef3d77dacf1683fd18 to your computer and use it in GitHub Desktop.

vim and regex cheatsheet

Search and Replace

See also this fandom vim wiki page.

Basic:

  • :s/find/replace/, only search in current line, and replace the first matched pattern

Search in a certain range

  • Default: only in current line
  • In all lines: :%s/f/r/
  • In selected line: :'<,'>s/f/r/
  • In selected characters: :`<,`>s/f/r/

Repeat

  • Default: only first matched in given range
  • match every one: :s/f/r/g

Other

  • Reuse the matched pattern:
    1. :s/f/&/

      & is the text that matches the search pattern.

      Example: :s/[a-z]/&1/g, add a 1 after each characte

    2. :s/\(a\)/\1/

      \(a\) makes a backreference to a.

      \1 is the first backreference, \2 is the second backreference, \0 is the text matched by the entire pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment