VIM Search / Replace spaces between two brackets or columns

Given the following line:

[aaaa bbbb cccc dddd] [decimal](18, 0) NULL, 

How would you replace spaces only between the first set of brackets in Vim? What does the / s command look like?

Update:

This is the expected result.

  [aaaa_bbbb_cccc_dddd] [decimal](18, 0) NULL, 
+4
source share
3 answers

The easiest way to do this is to visually select the contents of the string [] ed and replace only with this selection:

 vi]:s/\%V \%V/_/g 

This does not work well if you are trying to do something programmatically. In this case, you can match the entire string [] ed and use the replacement expression to construct the result.

 :s/\[[^\]]*\]/\=substitute(submatch(0), ' ', '_', 'g')/g 
+7
source

with the usual โ€œmagicโ€ setting you want to do

 :s/] \[/][/ 

or more fancily

 :s/]\zs\s\+\ze\[// 

or with a magic level explicitly set in regular expression

 :s/\V]\zs\s\+\ze[// 

\ zs and \ ze limit the replacement of the region they enclose.

\ M completely disables magic, so every character or combination of characters, not a prefix with \ , is taken literally.

'magic' ( :help 'magic' ) is a global option that defines how potentially special characters are interpreted in regular expressions.

+1
source

You can always find an interactive search and replace function that can be done with the c flag

-1
source

Source: https://habr.com/ru/post/1305040/


All Articles