Wikia

Vim Tips Wiki

Changes: Simplifying regular expressions using magic and no-magic

Edit

Back to page

(Standard category names + minor manual clean.)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 13: Line 13:
 
If you love regular expressions, you probably like to use them everywhere. Unfortunately it gets very confusing when you use Vim's regular expressions, Perl and POSIX compliant varieties in PHP, and then command-line grep as well, because they all seem to have different rules as to which characters have special meaning on their own and which do not. Quite often I need several attempts to write a search pattern because I can't remember which characters need to be preceded by a backslash.
 
If you love regular expressions, you probably like to use them everywhere. Unfortunately it gets very confusing when you use Vim's regular expressions, Perl and POSIX compliant varieties in PHP, and then command-line grep as well, because they all seem to have different rules as to which characters have special meaning on their own and which do not. Quite often I need several attempts to write a search pattern because I can't remember which characters need to be preceded by a backslash.
   
Thankfully, any Vim search pattern can include the special sequence <tt>\v</tt> (very magic), and this will make every following character except a-zA-Z0-9 and '_' have special meaning. Using <tt>\V</tt> has the opposite effect: all characters have their literal meaning and must be preceded by <tt>\</tt> to activate their special meaning. So if you have forgotten which characters have special meaning in Vim, start the pattern with <tt>\v</tt> or <tt>\V</tt> to switch them all on or off.
+
Thankfully, any Vim search pattern can include the special sequence <code>\v</code> (very magic), and this will make every following character except a-zA-Z0-9 and '_' have special meaning. Using <code>\V</code> has the opposite effect: all characters have their literal meaning and must be preceded by <code>\</code> to activate their special meaning. So if you have forgotten which characters have special meaning in Vim, start the pattern with <code>\v</code> or <code>\V</code> to switch them all on or off.
   
Vim's default 'magic' setting makes characters have the same meaning as in grep, and <tt>\v</tt> (very magic) makes them the same as the extended regular expressions used by egrep.
+
Vim's default 'magic' setting makes characters have the same meaning as in grep, and <code>\v</code> (very magic) makes them the same as the extended regular expressions used by egrep.
   
Regular expressions in scripts should '''always''' specify one of <tt>\v</tt>, <tt>\m</tt>, <tt>\M</tt>, or <tt>\V</tt>, to make them immune to the user's <tt>'magic'</tt> setting.
+
Regular expressions in scripts should '''always''' specify one of <code>\v</code>, <code>\m</code>, <code>\M</code>, or <code>\V</code>, to make them immune to the user's <code>'magic'</code> setting.
   
The <tt>:substitute</tt> command has the <tt>:smagic</tt> and <tt>:snomagic</tt> alternate forms (the same as <tt>\m</tt> and <tt>\M</tt>), so you can [[search and replace]] with <tt>%sno/regex/new_text/g</tt>. Alternatively, you might find it helpful to refine your regular expression by searching with <tt>/\v</tt> first, then you can insert your regular expression by typing:
+
The <code>:substitute</code> command has the <code>:smagic</code> and <code>:snomagic</code> alternate forms (the same as <code>\m</code> and <code>\M</code>), so you can [[search and replace]] with <code>%sno/regex/new_text/g</code>. Alternatively, you might find it helpful to refine your regular expression by searching with <code>/\v</code> first, then you can insert your regular expression by typing:
   
<tt>:s/<Ctrl-R>/</tt>
+
<code>:s/<Ctrl-R>/</code>
   
 
==References==
 
==References==

Revision as of 06:15, July 13, 2012

Tip 1237 Printable Monobook Previous Next

created 2006 · complexity basic · author Peter Hodge · version 6.0


If you love regular expressions, you probably like to use them everywhere. Unfortunately it gets very confusing when you use Vim's regular expressions, Perl and POSIX compliant varieties in PHP, and then command-line grep as well, because they all seem to have different rules as to which characters have special meaning on their own and which do not. Quite often I need several attempts to write a search pattern because I can't remember which characters need to be preceded by a backslash.

Thankfully, any Vim search pattern can include the special sequence \v (very magic), and this will make every following character except a-zA-Z0-9 and '_' have special meaning. Using \V has the opposite effect: all characters have their literal meaning and must be preceded by \ to activate their special meaning. So if you have forgotten which characters have special meaning in Vim, start the pattern with \v or \V to switch them all on or off.

Vim's default 'magic' setting makes characters have the same meaning as in grep, and \v (very magic) makes them the same as the extended regular expressions used by egrep.

Regular expressions in scripts should always specify one of \v, \m, \M, or \V, to make them immune to the user's 'magic' setting.

The :substitute command has the :smagic and :snomagic alternate forms (the same as \m and \M), so you can search and replace with %sno/regex/new_text/g. Alternatively, you might find it helpful to refine your regular expression by searching with /\v first, then you can insert your regular expression by typing:

:s/<Ctrl-R>/

References

Comments

Around Wikia's network

Random Wiki