Last Updated: May 03, 2017
·
5.304K
· rwos

Add and subtract in vim

Vim has a very handy built-in functionality to increment and decrement numbers: press CTRL-A to increment and CTRL-X to decrement.

This works with positive and negative integers and even C-style hex numbers (pressing CTRL-A on 0xff yields 0x100).

That's all pretty cool, but it gets even better if you combine this with the usual vim functionality:

Want to add 1337 to a number in your file? Type 1337 CTRL-A.

Want to construct a list of numbers? Just include CTRL-A in a macro:

  • On a line with a single 1 on it, do:
  • qa to start recording into a
  • yy yank the line
  • p paste it below
  • CTRL-A increment the pasted line
  • q stop recording

Invoke the macro with a count (for example 98@a - (without the href of course, the markdown translator inserted that one)) and you have a list of numbers. (Though, on a unix system you could of course also do this via :r!seq COUNT).

In the same vein, you can use CTRL-A and CTRL-X to, for example, add a constant to a list of numbers.

More under :help CTRL-A, which is also where the macro is from.