2

I want to replace a number n, with the number n+1. Here's what's working:

:%s/\([0-9]\+\)/\=(submatch(0)+1)/

But this one doesn't work:

:%s/-c \([0-9]\+\)/-c \=(submatch(0)+1)/

Why?

5

To use \= in substitute it needs to start the replacement string (see :h sub-replace-expression). Also substitue(0) means whole matched string, so also including -c prefix and that will always produce 0 when used in a arithmetic context. To use it correctly only on a number, you should use submatch(1).

And now, to make it work, you need to include -c inside the expression:

s/-c \([0-9]\+\)/\=("-c ".(submatch(1)+1))/

or:

s/\(-c \)\([0-9]\+\)/\=((submatch(1)).(submatch(2)+1))/

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.