0

Is it possible to use the javascript regex engine instead of built-in one for search and substitute?

  • why would you want to? – Herb Wolfe Sep 22 '17 at 4:24
  • @herbwolfe because i'm js developer and want use () capture group without escaping \(\). I used to use js regexp, because i use it every day. It seems to me that js regexpes more human-readable than vim's – Herrgott Sep 22 '17 at 5:09
  • 2
    Have you tried prepending search pattern with \v - which is very magic? This makes Vim treat () special without escaping. Read more here:h \v – grodzik Sep 22 '17 at 5:54
  • @grodzik not only because of escaping i want to use another engine. It concerns a lot of aspects, like ? instead of \=, so i didn't found \d working in vim engine – Herrgott Sep 22 '17 at 6:10
  • 2
    not possible. Because Vim regexp likely allows regex atoms that cannot be expressed by the JS regex enging (e.g. cursor position, regexp engine, etc) – Christian Brabandt Sep 22 '17 at 6:44
4

I can understand the frustration in having to learn a new1 regular expression dialect when you already know one well, but it's not possible to change which dialect Vim uses.

Vim's default regular expressions make it quicker to search for the textual elements frequently found in code. Thus you can search for for e.g. foo() simply with the expression foo(). Personally, this isn't to my taste, so I have the following in my .vimrc:

nnoremap / /\v
nnoremap ? ?\v

This means my searches begin in "very magic" mode, so much less escaping is required to get to regular expression features such as grouping. If I need a normal "magic" expression I can simply delete the \v by pressing Backspace.

However, some of the other differences (e.g. the syntax for lookaround) you're just going to have to learn afresh, I'm afraid. The best place if you're already well versed in regular expressions is probably the reference documentation (:help regexp), but there's also info in the user guide (:help usr_27.txt).

This previous answer by tommcdo explains some of the benefits you will get from doing so.

1: New to you, that is. Vim's regular expressions pre-date most other current dialects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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