decrease number?

42 views
Skip to first unread message

stosss

unread,
Jun 19, 2017, 9:14:10 PM6/19/17
to Vim Users
I have several hundred lines like the one below:

<line x1="20"  y1="45"  x2="820" y2="45"  stroke-width="1" stroke="#c0c0c0" />

I want to change them like this:

<line x1="10"  y1="35"  x2="810" y2="35"  stroke-width="1" stroke="#c0c0c0" />

The "ten" place holder needs to decrease by one.
x1="20" y1="45" x2="820" y2="45"

Can someone point me to something that would show me a fast way to do this?

I am thinking some how I should be able to use CTRL-X in a search and replace but I don't know how to do it. I am guessing '\=' would be used. I really don't know how to put this together.

Thanks

stosss

unread,
Jun 19, 2017, 9:59:37 PM6/19/17
to Vim Users
Placing cursor on the number and typing 10CTRL-X reduces it by the desired amount but how do I do it in a search and replace pattern?

John Passaro

unread,
Jun 19, 2017, 10:29:57 PM6/19/17
to vim...@googlegroups.com
use the expression register:

s/\v\w+\="\zs\d\d+\ze"/\=submatch(0)-10/g

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

stosss

unread,
Jun 19, 2017, 10:35:11 PM6/19/17
to Vim Users
Thank You John! Awesome!

Tim Chase

unread,
Jun 19, 2017, 10:35:48 PM6/19/17
to vim...@googlegroups.com
I'd go with your "\=" suggestion. Something like

:%s/\<[xy][12]="\zs[^"]*\ze"/\=submatch(0)-10/g

This only finds x1, x2, y1, and y2. You can relax the pattern a bit
if there are other variable-names in question.

-tim


Tim Chase

unread,
Jun 19, 2017, 10:37:47 PM6/19/17
to vim...@googlegroups.com
On 2017-06-19 22:29, John Passaro wrote:
> use the expression register:
>
> s/\v\w+\="\zs\d\d+\ze"/\=submatch(0)-10/g

Beware that this catches any variable name (not just [xy][12] which
may or may not be what you want), and also doesn't catch
negative-number values.

But for the general case of the example you (OP) gave, John's works
well too.

-tim


stosss

unread,
Jun 19, 2017, 10:40:45 PM6/19/17
to Vim Users
Thanks Tim,

Johns did work exactly the way I wanted it to.

I like yours too.

Reply all
Reply to author
Forward
0 new messages