Increment numbers

3.6
(185)

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 video showing these two plugins in action:

How useful was this tip?

Average rating 3.6 / 5. Vote count: 185

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Andy Libby

36 Posts

Rider of bicycles. Writer of code. User of Vim.
View all posts