开发者

VIM Search/Replace spaces between 2 brackets or columns

开发者 https://www.devze.com 2022-12-24 05:27 出处:网络
Given the following line: [aaaa bbbb cccc dddd] [decimal](18, 0) NULL, How would you replace the spaces only between the firs开发者_开发百科t set of brackets in Vim? What would the /s command look

Given the following line:

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

How would you replace the spaces only between the firs开发者_开发百科t 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.


You can always go for interactive find and replace, which can be done with flag c

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号