serhii.net

In the middle of the desert you can say anything you want

18 Jun 2021

Day 899

vim magic / nomagic / verymagic

Finally decided to undertand this part: Vim documentation: pattern

  • \m is magic, \M is nomagic. \m/magic is the default.
  • \v is verymagic, \V is very nomagic

Handy table from the documentation:

Examples:
after:	  \v	   \m	    \M	     \V		matches 
		'magic' 'nomagic'
	  $	   $	    $	     \$		matches end-of-line
	  .	   .	    \.	     \.		matches any character
	  *	   *	    \*	     \*		any number of the previous atom
	  ()	   \(\)     \(\)     \(\)	grouping into an atom
	  |	   \|	    \|	     \|		separating alternatives
	  \a	   \a	    \a	     \a		alphabetic character
	  \\	   \\	    \\	     \\		literal backslash
	  \.	   \.	    .	     .		literal dot
	  \{	   {	    {	     {		literal '{'
	  a	   a	    a	     a		literal 'a'

Practically:

  • \v/verymagic - almost everything has a special meaning (numbers, letters and _ are the only ones parsed as-is)
  • \V/verynomagic - almost nothing has a special meaning, everything interpreted as-is EXCEPT \

A Vim Guide for Adept Users has these nice tips that I’ll stick to:

My advice in this madness: remember that very magic will allow you to use every regex metacharacter without escaping them, and that very nomagic oblige you to escape these metacharacters to use them.

and

I propose this simple rule:

  • When you need a regex, use “very magic” by adding \v before your pattern.
  • When you don’t need a regex, use “very nomagic” by adding \V before your pattern.

It also has this nice list:

\s or [:blank:] - whitespace characters.
[A-Z] or \u or [:upper:] - Uppercase.
[a-z] or \l or [:lower:] - Lowercase.
[0-9] or \d or [:digit:] - Digits.
\_ - Character class with end of line included.
Nel mezzo del deserto posso dire tutto quello che voglio.