Vim: Increment and decrement numbers

In Normal mode, Ctrl+A increments a number by one, and Ctrl+X decrements a number by one. This will work on numbers at or after the cursor. So the cursor does not need to be on the number. But the “after the cursor” thing works only if the cursor is on the same line as the number. 

Like most Normal mode commands, increment and decrement support numeric prefixes. For example, pressing 5Ctrl+A will increment the number by 5.

Where is it useful?

At first glance, this feature might not seem useful but there are situations where you need to tweak a number and see its effects. Consider the following Autohotkey statement.

Gui, Show, % "x" A_ScreenWidth - 180 "y" A_ScreenHeight - 130 AutoSize NoActivate, %title%

Here we are subtracting numbers from variables A_ScreenWidth and A_ScreenHeight to specify the starting position of a GUI window on the screen. We may have to adjust the numbers a few times to arrive at the desired position.

Repeat using the dot command

The increment and decrement commands can be repeated using the dot command. Let us look at an example.

Let us say, we want to increment the number 180 by 10, multiple times.

  1. Place the cursor on the number.
  2. Type 10Ctrl+A. This would increase the number to 190.
  3. See if you are satisfied with the result. If yes, stop.
  4. Else press the dot key. The number will increase by another 10.
  5. Repeat steps 3 and 4.

Leave a Comment

Your email address will not be published. Required fields are marked *