Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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"
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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