1

I'm trying to use the vim submatch command to put quotes around all strings of characters in a line of text, and it doesnt work. Here's what I'm doing

:s:\a*\a:\(submatch(0)\):g

All it does is replace all strings with the string (submatch(0)). How do I make vim do what I'm trying to do?

6

submatch is not the best solution to do this. I was doing some helpgrep on \\=

And I find this:

                *sub-replace-special* *:s\=*
When the {string} starts with "\=" it is evaluated as an expression, see
|sub-replace-expression|.  You can use that for any special characters.
Otherwise these characters in {string} have a special meaning:

If you insist in using submatch you can use it in this way:

s:\a*\a:\='"'.submatch(0).'"'

However a more readable solution is:

s:\a*\a:"\0"
4

You have to capture each match by surrounding the pattern with a pair of braces \( and \). Having done this you can use the actual matched text in the replacement pattern with \1.

The following command does what you want:

:s:\(\a*\a\):"\1":g

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.