0

I need to prepend a backslash \ to each character in a string, excluding any leading whitespace. I believed the following would do the trick.

:s/^\s*\(.\)\+/\\\1/g

but it produces this result for the string shown

   abcde
\e

i.e. it shows only the last such replacement, not each of them.

Vim provides 2 regex engines, which you can access explicitly by prepending the search string with either '\%#=1' or '\%#=2'. Both produce the same result.

Could someone explain what I am doing wrong here?

1 Answer 1

2

Your regexp captures the complete input string abcde captures the 'e' and replaces that by \e.

You probably want something more like this: :s/\S/\\&/g

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .