Vim can increment and decrement numbers while in normal mode:
Ctrl-a - Increment the next number on the line
Ctrl-x - Decrement the next number on the line
In older versions of Vim, these required placing your cursor on the number. But modern Vim will automatically jump your cursor to the next occurrence of a number and perform the increment or decrement in one operation. Since any action in Vim can be prepended with a number x to perform that action x times, you can, for example, add 5
to the next number with 5<c-x>
.
If you’ve already internalized Ctrl-a
and Ctrl-x
for incrementing numbers, awesome! You might want to power up your efficiency even further with a few plugins:
Tim Pope’s speeddating.vim plugin allows incrementing of dates. Instead of Vim seeing 2020-09-30
as one positive and two negative numbers, Vim with Speed Dating will understand this is a date and will properly increment the date when your cursor is on the 30
, even rolling over to the next month when needed. Speed Dating also includes a number of other increment and decrement improvements, some of which we’ll dive into in future newsletter editions.
Since Vim version 7.4.754, incrementing and decrementing numbers has also worked with visual block selections, making quick work of numerical lists, such as in markdown files. But another useful plugin called visual-increment.vim allows converting a list of identical numbers into a list of sequential numbers. Just visually select the list and press Ctrl-a and the numbers are splayed out to an incremental list.
Here’s a quick gif showing these two plugins in action:
In case you missed our presale announcement last week, on November 1st we’re launching an email-based Vim course called Git Better with Vim. We will deliver 24 extraordinarily helpful lessons on how to use Git inside Vim and we’re offering the first 20 VimTricks subscribers 50% off with the promo code “subscriber”.
To the 14 of you who purchased already — thank you for your support! ♥️
~ Colin & Andy
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote:
You actually don't need a plugin to turn a list of identical numbers into a list of sequential ones. It can be done with vanilla Vim using `g<c-a>`/`g<c-x>`. See `:h v_g_CTRL-A` :)