Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 19 down vote accepted

\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.

share|improve this answer
This answer is great, but I think the replace part should be \1, without {}around it. – yunfeng Mar 31 at 4:03
@yunfeng I wanted to replace (10) with {10}, that was the reason to add them. – Bernhard Mar 31 at 6:51
@yunfeng, it should with curly braces, cause this is exactly what OP whants. please read the question more carefully. – rush Mar 31 at 6:55

Your Answer


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.