Monday, April 4, 2011

Substitute via expression on matched text

I have a file that contains several lines that start out similar to
these:

PP 124,152
PP 83,938
PP 877,994

In other words, these lines match the regex:

/^PP \d\+,\d\+/

I wanted to decrement the second number in each pair by 120, and I ended
up with something like this:

:g/^PP /norm! f,l120^X

where the "^X" is a literal C-x character.

This worked just fine, but I wondered if there was a way to do the same
thing with a substitute command instead of a global. I tried this:

:%s/^PP \d\+,\zs\d\+/\=\& - 120/

but it didn't work, nor did I really expect it to. The "\&",
representing the current matched text, is not valid within an
expression.

So my question is, how can I write a replacement expression for a
substitute command that performs some operation (such as arithmetic) on
the matched text itself? Is there a special register that holds the
current match?

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: