Vim Tips Wiki
(Standard category names + minor manual clean.)
 
(4 intermediate revisions by 3 users not shown)
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>
  +
  +
==Permanent "very magic" mode==
  +
Vim does not have an option to use "very magic" by default (whereas <code>:set ignorecase</code> will [[Searching#Case sensitivity|always ignore case]] unless <code>\C</code> is used). The following mappings, however, effectively achieve the same purpose (from [http://stackoverflow.com/a/23021259/1858225 stackoverflow]):
  +
<pre>
  +
nnoremap / /\v
  +
vnoremap / /\v
  +
cnoremap %s/ %smagic/
  +
cnoremap \>s/ \>smagic/
  +
nnoremap :g/ :g/\v
  +
nnoremap :g// :g//
  +
</pre>
  +
  +
This does not work with `:g` and interferes somewhat with Vim's search-history behavior (see the StackOverflow link). An alternative solution is to use this plugin: http://www.vim.org/scripts/script.php?script_id=4849
   
 
==References==
 
==References==

Latest revision as of 17:29, 17 April 2014

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>/

Permanent "very magic" mode[]

Vim does not have an option to use "very magic" by default (whereas :set ignorecase will always ignore case unless \C is used). The following mappings, however, effectively achieve the same purpose (from stackoverflow):

nnoremap / /\v
vnoremap / /\v
cnoremap %s/ %smagic/
cnoremap \>s/ \>smagic/
nnoremap :g/ :g/\v
nnoremap :g// :g//

This does not work with `:g` and interferes somewhat with Vim's search-history behavior (see the StackOverflow link). An alternative solution is to use this plugin: http://www.vim.org/scripts/script.php?script_id=4849

References[]

Comments[]