In vim I have a line of text like this:
abcdef
Now I want to add an underscore or something else between every letter, so this would be the result:
a_b_c_d_e_f
The only way I know of doing this wold be to record a macro like this:
qqa_<esc>lq4@q
Is there a better, easier way to do this?
:%s/\(\p\)\p\@=/\1_/g
: starts a command.% searches the whole document.\(\p\) will match and capture a printable symbol. You could replace \p with \w if you only wanted to match characters, for example.\p\@= does a lookahead check to make sure that the matched (first) \p is followed by another \p. This second one, i.e., \p\@= does not form part of the match. This is important.\1 fills in the matched (first) \p value, and the _ is a literal.g is the standard do them all flag.If you want to add _ only between letters you can do it like this:
:%s/\a\zs\ze\a/_/g
Replace \a with some other pattern if you want more than ASCII letters.
To understand how this is supposed to work: :help \a, :help \zs, :help \ze.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With