1

I have a text that looks like this

" original
  call minpac#add('morhetz / gruvbox')

" desired
  call minpac#add('morhetz/gruvbox')

How can I limit vim pattern search within parentheses?

I tried '<,'>s/ //g in visual selection mode, but the problem is it also removes the indentation at the start as a sideffect. I am looking for a pattern search variation which would only work inside the parenthesis.

I am looking for something in lines of '<,'>s/ //gc where I can tell vim its n for first match and y for all others? I am not even sure if something like this is even possible in vim.

1
  • What about a line like this X Y (WWW 22(X Y [A 90] )) #? Can there be unmatched parenthesis? What about parenthesis inside quotes, should they be considered? Pairs of parenthesis that span multiple lines? Please always think of edge cases.
    – Quasímodo
    Oct 21 '20 at 14:56
1

If you desired output is:

" desired
  call minpac#add('morhetz/gruvbox')

And you are not against doing it a bit interactively...

  1. put your cursor on the line before or inside 'morhets / gruvbox' and do vi' to select everything inside a string.
  2. run the command :s/\%V\s\+//g (it should look like :'<,'>s/\%V\s\+//g) on a command line

:help \%V -- match inside visual area.

Or, alternatively, you can limit your regex with a column:

:s/\%>20c\s*//g

:h \%>c -- match after a specific column.

3
  • Note: \s after \V% means ` ` (SPACE) for vim, so :'<,'>s/\%V\s\+//g is same as :'<,'>s/\%V \+//g, And \+ seems not necessary at all in this case, Tested Vim 8.2, NVim 4.4
    – Mamrezo
    Oct 22 '20 at 19:38
  • Not really. Check help for \s
    – Maxim Kim
    Oct 23 '20 at 6:05
  • yes it removes TABs to
    – Mamrezo
    Oct 23 '20 at 23:33
1

You can process in two times with mixing :substitute with substitute(), thanks to \=

:%s/(\zs.*\ze)/\=substitute(submatch(0), ' ', '', 'g')/

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.