Is there a simple way to |^a| and |^x| ?

32 views
Skip to first unread message

Erik Christiansen

unread,
Jun 5, 2017, 5:37:31 AM6/5/17
to vim users
Both ^a and ^x have proven very useful in a variety of scenarios. Now,
while editing some postscript, I sometimes have to decrement the
magnitude of a series of literal constants by a common amount, and if
there were mod variants of ^a and ^x, I'd be able to hit '.' on them
all, instead of having to ferret out the positive ones, then go back for
the negative, with all the scope for error which that entails.

Erik

Tim Chase

unread,
Jun 5, 2017, 9:29:50 AM6/5/17
to Erik Christiansen, vim users
While a side-stepping of your literal request, you can do
incrementing/decrementing in search replacements.

For all numbers in a range:

:'<,'>s/-\=\d\+/\=submatch(0)+22/g

to add 22 to all numbers in that range of lines. If you need more
context-aware targeting, you can use "\zs" and "\ze":

:'<,'>s/property=\zs-\=\d\+/\=submatch(0)+22/g

to only increment "property=###" by 22.

You can use any valid vim expression to the right of that
"\=" (though with a caveat regarding division, in which case I
recommend alternate delimiters:

:'<,'>s@-\=\d\+@\=(submatch(0)+22)/17@g

so that the "/" isn't seen as a delimiter for the :s command).

-tim



Erik Christiansen

unread,
Jun 5, 2017, 7:43:26 PM6/5/17
to vim users
On 05.06.17 08:29, Tim Chase wrote:
> While a side-stepping of your literal request, you can do
> incrementing/decrementing in search replacements.
>
> For all numbers in a range:
>
> :'<,'>s/-\=\d\+/\=submatch(0)+22/g
>
> to add 22 to all numbers in that range of lines.
...

Thanks Tim, I'll make a note of that for possible future use, but the
crux remains: inc/dec of the magnitude, irrespective of sign. It looks
like I'd need to cobble together a little bit of Vimscript, and map the
functions to keys, maybe Alt-A and Alt-X.

Erik

Christian Brabandt

unread,
Jun 6, 2017, 2:38:12 AM6/6/17
to vim users

On Di, 06 Jun 2017, Erik Christiansen wrote:

> Thanks Tim, I'll make a note of that for possible future use, but the
> crux remains: inc/dec of the magnitude, irrespective of sign. It looks
> like I'd need to cobble together a little bit of Vimscript, and map the
> functions to keys, maybe Alt-A and Alt-X.

visually select the number without the sign should work.

Best,
Christian
--
Niemand ist so gut oder so schlecht, wie er während seiner Scheidung
gemacht wird.
-- J.J. Disenberg

Tim Chase

unread,
Jun 6, 2017, 10:26:25 AM6/6/17
to Erik Christiansen, vim users
Ah, I'd misunderstood that it was ignoring the sign and you wanted
to respect it. But you *wanted* to ignore the sign. Thus, you can
omit the optional sign:

:'<,'>s/\d\+/\=submatch(0)+22/g

or move the "\zs" after the optional minus-sign
to get the desired result:

:'<,'>s/property=-\=\zs\d\+/\=submatch(0)+22/g

In both cases, submatch(0) becomes just the number without the sign.

Beware the first one may do odd things in the event you have decimal
numbers in your file, so it would change "11.1" to "33.23" since you
don't anchor the left side.

-tim




Erik Christiansen

unread,
Jun 7, 2017, 9:34:17 AM6/7/17
to vim users
On 06.06.17 08:38, Christian Brabandt wrote:
>
> On Di, 06 Jun 2017, Erik Christiansen wrote:
>
> > Thanks Tim, I'll make a note of that for possible future use, but the
> > crux remains: inc/dec of the magnitude, irrespective of sign. It looks
> > like I'd need to cobble together a little bit of Vimscript, and map the
> > functions to keys, maybe Alt-A and Alt-X.
>
> visually select the number without the sign should work.

Many thanks, Christian. I'll update my Vim, as it currently just says
"Bong" when I try that. (Was too busy using it to update today. :)

Erik

Erik Christiansen

unread,
Jun 7, 2017, 9:53:21 AM6/7/17
to vim users
On 06.06.17 09:26, Tim Chase wrote:
> Ah, I'd misunderstood that it was ignoring the sign and you wanted
> to respect it. But you *wanted* to ignore the sign. Thus, you can
> omit the optional sign:
>
> :'<,'>s/\d\+/\=submatch(0)+22/g
>
> or move the "\zs" after the optional minus-sign
> to get the desired result:
>
> :'<,'>s/property=-\=\zs\d\+/\=submatch(0)+22/g
>
> In both cases, submatch(0) becomes just the number without the sign.
>
> Beware the first one may do odd things in the event you have decimal
> numbers in your file, so it would change "11.1" to "33.23" since you
> don't anchor the left side.

Fortunately the dimensions are all integer mm in this case, so that
would be OK. But the problem with Postscript (or generated CNC gcode for
that matter) is that the tendency to an excess of magic numbers risks
that some of them could be unrelated to the desired redimensioning. The
correct solution is to introduce more variables, then the whole problem
goes away. It just all takes time, and a quick dash for the finish line
was tempting.

Erik
Reply all
Reply to author
Forward
0 new messages