简体   繁体   中英

VIM Search/Replace spaces between 2 brackets or columns

Given the following line:

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

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

Update:

This is the intend outcome

    [aaaa_bbbb_cccc_dddd] [decimal](18, 0) NULL,

The easiest way to do it would be to visually select the contents of the [] ed string and perform the replacement just on that selection:

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

This doesn't work very well if you're trying to do things programmatically, though. In that case, you can match the entire [] ed string and use a replacement expression to construct the result.

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

with the normal setting of 'magic' you'd want to do

:s/] \[/][/

or more fancily

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

or with the magic level explicitly set in the regex

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

\\zs and \\ze limit the substitution to the area that they enclose.

\\M turns off magic completely, so every character or character combo not prefixed with \\ is taken literally.

'magic' ( :help 'magic' ) is a global option that determines how potentially-special characters in regexes are interpreted.

您可以随时进行交互式查找和替换,这可以通过标志c完成

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM