I am working with VIm and trying to set up a search and replace command to do some replacements where I can re-use the regular expression that is part of my search string.

A simple example would be a line where I want to replace (10) to {10}, where 10 can be any number.

I came this far

  .s/([0-9]*)/what here??/

which matches exactly the part that I want.

Now the replacement, I tried


But, this gives as output {(10)}

Then, I tried


However, that gave me ({10}), which I also close, but not what I want.

I think I need some other kind of marking/back-referencing instead of this \0, but I don't know where to look. So the question is, can this be done in vim, and if so, how?

3 Answers 3


\0 is the whole match. To use only part of it you need to set it like this and use \1


More detailed instruction you can find here or in vim help.

  • 8
    Note the parentheses for the capture are backslash-escaped.
    – Randall
    Jun 18, 2019 at 15:04

I recently inherited some legacy code and I wanted to replace all occurrences like:

print "xx"
print x,y
print 'xx'





Building on the previous answer and in hopes that someone will benefit from it I used the following command, that will change all occurrences:

%s/print\( .*\)/logging.info\(\1\)/g

If you substitute % with . and remove /g you will end up with

.s/print\( .*\)/logging.info\(\1\)

that will enable you to go over each match and choose whether you change it or not.


Better way to enable you to go over each match is to add a "c" to the end of the code, rather than go line-by-line

%s/print\( .*\)/logging.info\(\1\)/gc
  • This really doesn't answer the original question in any way
    – Bernhard
    Apr 26, 2018 at 6:52
  • 1
    Would be a good comment. Regardless, it's presence is appreciated.
    – Chris
    Dec 10, 2021 at 19:23

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.