In vim, I want to find a string and insert spaces between each character of the string

I try to search and replace regular expressions and capture groups in a simple way, but have no idea how.

Let's say I want to work on the previous sentence, capturing “try” and replacing “try i n g”.

:%s/\vtrying{-}/ \1/g
+4
source share
3 answers

You have a solution, but here is another:

 %s/trying/\=join(split(submatch(0),'\zs'), ' ')/g

It should be a little faster than calling substitute (), but it is not so important.

+4
source

Just complete two substitution matches. If the second uses a replacement with an expression ( \=at the beginning of the replacement :h sub-replace-expression)

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

The expression simply inserts a space between each character of the word.

+4

, , .

, "" "t r y n g".

:g~trying~exe "norm /trying^Mve^[" | s/\%V\(.\)/\1 /g

( ^M CTRL-Q Enter, ^[ - CTRL-Q Esc)

, , .

, , "t r y n g" "t r y n g".

NB , , "" .

g "try" , "execute" "normal", , , | , \%V .

It also adds a space at the end.

Overall, this is not as good as @FDinoff.

0
source

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


All Articles