Vim icon indicating copy to clipboard operation
Vim copied to clipboard

Support Vim's flavor of regex

Open J-Fields opened this issue 5 years ago • 9 comments

We currently use javascript's regex library, which is unfamiliar to some coming from vim (see #2043, #3073, #3075, etc.). This ticket is to consolidate those and center discussion around the possibility of supporting Vim's regex flavor.

As a first step, I've created the Pattern class with its own parser - this essentially just translates a Vim-style regex into a JS-style regex.

Current status:

  • [X] \</\> => \b (technically, this is not quite right)
  • [X] Deal with \n on Windows (allow leading \r)
  • [X] Capture groups (\1, etc.)
  • [ ] Character classes (:help /character-classes)
  • [ ] magic (#4018)
  • [ ] \%V (#4936)
  • [ ] \zs and \ze (#3073)
  • [ ] Branches (:help /branch)
  • [ ] Many other weird little quirks

J-Fields avatar Aug 26 '19 02:08 J-Fields

Very interested in this. The whole point of using the VIM extension is so I don't have to learn yet another editor and its quickiness. You should look into VIM implementations in javascript. There are a lot (not many good ones though).

Banger avatar Jul 20 '20 19:07 Banger

If this ever gets integrated, I believe it would be a good to have the option. I've been heavily using a variety of regex engines for more years than I care to say and I always thought Vim's regex syntax was clunky.

FrankCoder avatar Dec 31 '20 12:12 FrankCoder

I think there is another option with less effort than a full regex engine. I am stealing ;-) this idea from vsvim. What the guys there did, is a "tokenwise" translation. Meaning:

\<  -> \b
\l  -> [a-z]

And so on... A lot of vim regex could be translated like so and you can also do a lot with simple "left to right" translation, meaning no full parsing. Limits are where functionality is principally missing in js regex.

sql-koala avatar Feb 03 '21 23:02 sql-koala

Yeah that's a good point, @sql-koala, I've pondered it before. There are definitely limitations, but it'd be a good first solution I think.

J-Fields avatar Feb 04 '21 18:02 J-Fields

Hey guys, I imported a .vimrc which has nnoremap / /\v and learned about the concept of 'very magic' in Vim which makes it so no regex characters need to be escaped in patterns. Is there any plans to add this in addition to magic / no magic? Vim also has \V for 'very no magic' (who named these things?) as well as \m and \M for setting magic and nomagic on the fly.

JohnnyUrosevic avatar Nov 10 '21 18:11 JohnnyUrosevic

Is there any plans to add this in addition to magic / no magic?

Yes, support for magic is planned, though it may be a while before it's implemented

J-Fields avatar Nov 10 '21 19:11 J-Fields

Is there any plans to add this in addition to magic / no magic?

Yes, support for magic is planned, though it may be a while before it's implemented

I can try to work on this if you want. I don't know if there's more pressing issues you would rather have contributors for.

JohnnyUrosevic avatar Nov 11 '21 19:11 JohnnyUrosevic

Just noting here that with the new (fantastic!) inccommand addition, this issue makes it all the more puzzling when it seems like the RegEx matches, the UI updates, but it doesn't actually do anything because the RegEx doesn't match when doing the replace (from what I can tell). See this comment: https://github.com/VSCodeVim/Vim/issues/7499#issuecomment-1039755795

macintacos avatar Feb 15 '22 18:02 macintacos

s:(\S+):"\1":g shows the replacement correctly in the UI, but then it provides E486: Pattern not found when hitting enter.

rdbisme avatar Jul 08 '22 08:07 rdbisme

%s/stroke=".*?"/stroke="currentColor" also highlights the replacement correctly in the editor, but then it shows E486: Pattern not found: stroke=".*?" when I try to replace.

ahmafi avatar Aug 22 '23 07:08 ahmafi