Tip 74Use the \v Pattern Switch for Regex Searches

Vim’s regular expression syntax is closer in style to POSIX than to Perl. For programmers who already know Perl’s regexes, this can be a source of frustration. Using the very magic pattern switch, we can make Vim adopt a more familiar syntax for regular expressions.

Let’s say that we want to compose a regular expression that matches each of the color codes in this snippet of CSS:

 body { color: #3c3c3c; }
 a { color: #0000EE; }
 strong { color: #000; }

We need to match a # character followed by either three or six hexadecimal characters. That includes all numeric digits, plus the letters A through F in upper- or lowercase.

Find Hex Colors with Magic Search

The following ...

Get Practical Vim, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.