5

After years of using Vim and then Neovim, I still find myself forgetting to escape parenthesis and the | character when I intend to use their special behaviour:

If I want to search for "this" or "that", this is incorrect:

/(this|that)/

Instead, I should have typed one of these two alternatives:

/\(this\|that\)/
/\v(this|that)/

I either have to backslash the (, |, and ) characters, or I have to use \v to enter "very magic" mode. From the docs:

\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'

How do I make \v the default mode, without having to type \v at the beginning of the regex?

3
  • 4
    nnoremap / /\v is a very common mapping.
    – romainl
    Commented Jan 11, 2023 at 7:29
  • @romainl That looks useful. Is there something similar for substitutions?
    – Flimm
    Commented Jan 11, 2023 at 7:59
  • github.com/vim/vim/issues/13098
    – balki
    Commented Dec 19, 2023 at 19:03

1 Answer 1

0

Since search is done with / and search backwards is done with ?, you can add these lines in your .vimrc in order to automatically type \ v when those buttons are pressed:

nnoremap / /\v
nnoremap ? ?\v

This doesn't affect the substitute command :s, I don't know how to automatically turn on "very magic" mode for those regexes.

2
  • I found this solution interfered with nvim-cmp, so I stopped using it eventually.
    – Flimm
    Commented Dec 15, 2023 at 4:07
  • This breaks / + Return to search the last pattern because /\v followed by nothing matches everything.
    – ynn
    Commented Jan 8 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.