Vim Regexp Cheat Sheet

A Programming › Regular-expressions cheat sheet in English by fievel
38,415 views 4,729 downloads 19 Dec 2016 History
3.7 avg · 0 ratings

Special char NOT requiring escape

\
Escape next character
^
Start-­of-line
$
End-of­-line
.
Any char
*
0 or more quantifier
~
Match last given substitute string
[...]
Match range
[^...]
Not range
&
In replac­ement: insert whole matched pattern
Interp­reted as regular expression operator without escaping (escape to match literal)

Special char requiring escape

\<
Beginning of a word
\>
End of word
\(...\)
Group
\|
Separate altern­ative
\_.
Any single char or end-of­-line
\+
1 or more quantifier
\=
0 or 1 quantifier
\?
or or 1 quantifier
\{n,m}
n to m quantifier
\{n}
n quantifier
\{n,}
at least n quantifier
\{,m}
0 to m quantifier
Interp­reted as regular expression operators only when escaped (otherwize will be interp­reted as literals).

Useful examples

:g/<pa­tte­rn>/d_
delete all lines matching pattern
s/^.*$\n//
delete empty lines
s/<­pat­ter­n>/new &/
Replace pattern by "new <whole matched patter­n>"
s/<­pat­ter­n>/­\=@a/
Replace pattern by content of register "­a"
s/<­pat­ter­n>//gn
Count nb occurence of pattern

Look around assertions

\@<=
positive look behind
\@<!
negative look behind
\@=
positive look ahead
\@!
negative look ahead
Must follow a group matching what to look behind or ahead (ex: \(<­pat­ter­n>­\)\@­<=)

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 3

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.