tags:

views:

1475

answers:

1

I'm not sure it's possible or not, but one of the things I find I need to do often is to replace the contents of the file, but I want to use the current line number as a replacement option. So if I had a file like:

This is text to replace XX 
This is text to replace XX 
This is text to replace XX

I would want to be able to run a regex that would turn that file into:

This is text to replace 1
This is text to replace 2
This is text to replace 3

I've looked around, but I can't seem to find any way of doing this. Is this possible? Does anyone know of a simple way to approach this?

Thanks.

+13  A: 
:help sub-replace-expression

You can construct a replace expression like:

:s@ xx$@\=" " . line(".")@

... Which works for me.

greyfade
what is that magic with '@'. I moved through help and this is still isn't clear.
Mykola Golubyev
Thanks, that's just about the exact thing I was looking for. In my case, the expression I was looking for was::%s/XX/\=line(".")/gWorks like a charm.
Ryan Smith
Whatever non-alphanumeric character follows s is used as the delimiter of expressions. That @ could as easily be a comma, period, #, ", !, or ¥ as it could be /
greyfade