Replace using VIM, reuse part of the search pattern

regular expressionreplacevivim

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

  .s/([0-9]*)/{\0}/

But, this gives as output {(10)}

Then, I tried

 .s/(\zs[0-9]*\ze)/{\0}/

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?

Best Answer

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

.s/(\([0-9]*\))/{\1}/

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

Related Question