Skip to content

Instantly share code, notes, and snippets.

@trptcolin
Created March 19, 2014 13:52
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 trptcolin/9642062 to your computer and use it in GitHub Desktop.
Save trptcolin/9642062 to your computer and use it in GitHub Desktop.

Vim Regexes

When do I need a backslash?

  • this is all dependent on the "magic" option being set to the default, magic (:set magic? will tell you what you're set on)

  • vim regexes are beyond reason

  • (defn finds a literal paren, and \(defn\) creates a group.

  • | finds a literal pipe and, \| separates alternatives

  • [ring.adapter.jetty finds literal text, but so does \[ring.adapter.jetty

    • however, closing the square bracket makes the stuff inside a character class
    • backslashing or omitting either bracket finds literal text instead
  • . matches any character, \. matches a literal period

  • f\+ matches 1 or more f's, but f* matches 0 or more

  • \d finds a digit

  • \d\{2,} finds 2 or more digits (note: no backslash on closing bracket)

Zero-width matching

  • foo\(bar\)\@= matches any foo that's followed by bar
  • foo\(bar\)\@! matches any foo that's NOT followed by bar
  • \(foo\)\@<=bar matches any bar that's preceded by foo
  • \(foo\)\@<!bar matches any bar that's NOT preceded by foo
  • ^\s*\zsuser matches user at the start of a line, ignoring whitespace
    • ^\s*user matches user at the start of a line, keeping whitespace

Case sensitivity

  • \c (anywhere in the pattern) marks the search as case insensitive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment