Vim Regular Expression Special Characters: To Escape or Not To Escape

Vim‘s regular expression dialect is distinct from many of the other more popular ones out there today (and actually predates them).
One of the dialect differences that always leaves me fumbling has to do with which special characters need to be escaped.
Vim does have a special “very magic” mode (that is activated by “\v” in the regular expression) that makes thing very clean and simple in this regard: only letters, numbers and underscores are treated as literals without escaping.
But I have never got used to the habit of preceding my expressions with “\v“, though maybe I should.

In the mean time however, I thought I would put up a quick reference that lists all the special regular expression characters in the default “magic” mode Vim dialect, divided into those that do not need to be escaped vs. those that do.

Regular Expression Special Characters Not Requiring Escaping

The following special characters are interpreted as regular expression operators without escaping (escaping will result in them being intepreted as literals):

\ Escape next character (use “\\” for literal backslash).
^ Start-of-line (at start of pattern).
$ End-of-line.
. Matches any character.
* Matches 0 or more occurrences of the previous atom.
~ Matches last given substitute string.
[] Matches any of the characters given within the brackets.
[^] Matches any character not given within the brackets.
& In replacement pattern: insert the whole matched search pattern.

Regular Expression Special Characters Requiring Escaping

The following special characters are interpreted as regular expression operators only when escaped (otherwise they will be interpreted as literals):

\< Matches beginning of a word (left word break/boundary).
\> Matches end of a word (right word break/boundary).
\(...\) Grouping into an atom.
\| Separating alternatives.
\_. Matches any single character or end-of-line.
\+ 1 or more of the previous atom (greedy).
\= 0 or one of the previous atom (greedy).
\? 0 or one of the previous atom (greedy).
\{ Multi-item count match specification (greedy).

\{n,m} n to m occurrences of the preceding atom (as many as possible).
\{n} Exactly n occurrences of the preceding atom.
\{n,} At least n occurrences of the preceding atom (as many as possible).
\{,m} 0 to n occurrences of the preceding atom (as many as possible).
\{} 0 or more occurrences of the preceding atom (as many as possible).
\{- Multi-item count match specification (non-greedy).

\{-n,m} n to m occurrences of the preceding atom (as few as possible).
\{-n} Exactly n occurrences of the preceding atom.
\{-n,} At least n occurrences of the preceding atom (as few as possible).
\{-,m} 0 to n occurrences of the preceding atom (as few as possible).
\{-} 0 or more occurrences of the preceding atom (as few as possible).

One thought on “Vim Regular Expression Special Characters: To Escape or Not To Escape

Leave a Reply

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