Today I Learned

hashrocket A Hashrocket project

Increment All The Numbers

Vim's substitution feature can be used for more than simple static text replacement. Each replacement can be the result of some operation on the original text. For instance, what if we'd like to increment all numbers in the buffer? We can achieve this by searching for all numbers and then using \= with submatch. Whenever the replacement string of a substitution starts with \=, the remainder of the string is evaluated as an expression.

Given the following text in our buffer:

1 2 a b c 45 123 1982

We can run the following substitution command:


This will transform all digits in the buffer, resulting in:

2 3 a b c 46 124 1983

Want to decrement all the numbers instead?


See :h sub-replace-expression for more details.

See More #vim TILs
Every developer at Hashrocket is a Vim expert. Check out our development environment, Dotmatrix, and if you are in Chicago, come to the Vim Chicago Meetup hosted at our Chicago office.