Learn Practice Daily golf Reference

How to increase or decrease a number in Vim

In Normal mode, Ctrl-a adds one to the number under or after the cursor and Ctrl-x takes one away. Put a count first for more, so 5Ctrl-a adds five. In Visual mode, gCtrl-a numbers the selected lines 1, 2, 3.

The ninja clicking a big tally counter on the Byte Burrito counter, its window rolling from 12 to 17, a Ctrl-A keycap on the plunger.

The office down the street has called back about Friday's catering order. Five more burritos, two fewer guacamoles. The cursor is at the start of the burritos line in catering.js, a few characters before the 12, and you'd rather not do the sum in your head and type it over the old number.

Before
burritos: 12,
5Ctrl-a
burritos: 17,
5Ctrl-a finds the 12 further along the line and adds five. The cursor ends on the last digit of the new number
Try it here

Change the catering order

Ctrl-a is Ctrl and A pressed together, and Ctrl-x is Ctrl and X. The cursor is at the start of the burritos line.

Add five burritos with 5Ctrl-acatering.js
1const order = {
2 burritos: 12,
1 guacamole: 4,
2 churros: 30,
3};
NORMALLoading the editor…0.0sLn 2, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Which number Vim changes

Ctrl-a changes the number under the cursor. When the cursor isn't on one, Vim looks right along the line and takes the first number it finds, so you rarely need to move onto it first. It never looks past the end of the line, and on a line with no number after the cursor nothing happens.

That matters for version numbers. To Vim, 1.4.2 is three numbers with dots between them, and each counts on its own. With the cursor on the 4, Ctrl-a gives 1.5.2 and 10Ctrl-a would give 1.14.2. Nothing carries into the next part.

Check yourself

A bug-fix release

You want the version to say 1.4.3. The cursor is at the start of the line, in Normal mode, and you press Ctrl-a. What does the line say now?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
1 "version": "1.4.2",
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

Negative numbers

A minus sign right before the digits belongs to the number. Ctrl-x on a 1 gives 0 and then -1, and Ctrl-a on -3 climbs to -2. The walk-in freezer runs at -18, and the manager wants it two degrees colder.

Before
freezer: -18,
2Ctrl-x
freezer: -20,
The minus is part of the number, so 2Ctrl-x takes -18 down to -20

Leading zeros, hex and octal

Vim keeps a number's width. 0099 becomes 0100. Hex keeps its 0x and its zeros, so 0x00ff becomes 0x0100, and binary works the same way with 0b.

The nrformats option decides which kinds Vim knows. Neovim's default is bin,hex, and so is Vim's while it runs on its defaults.vim, which it does until you have a vimrc of your own. After that Vim's built-in default applies, and it adds octal. A number with a leading zero then counts in eights, so 007 becomes 010, and 0099, which can't be octal, comes out as 100. Put set nrformats-=octal in your vimrc if your files have zero-padded numbers like order tickets.

Before
ticket: 0099
Ctrl-a
ticket: 0100
Ctrl-a carries into the hundreds and keeps the ticket four digits wide

Number a list 1, 2, 3 in Visual mode

Select some lines first and Ctrl-a adds to the first number on every one of them. V selects the line the cursor is on, and each j stretches the selection down a line. When every price on the menu goes up a dollar, that's V j j and then Ctrl-a.

Before
Carnitas $9.50
Al pastor $10.50
Veggie $8.00
VjjCtrl-a
Carnitas $10.50
Al pastor $11.50
Veggie $9.00
V j j selects the menu and Ctrl-a puts a dollar on each price. The cents are a number of their own, so they stay as they are

gCtrl-a adds a little more on each line: one on the first selected line, two on the second, three on the third. A list where every line starts with 1., which is what you get after pasting one line a few times, becomes 1, 2, 3, 4 once you select from the second line down. A count sets the step, so on a column of zeros 10gCtrl-a gives 10, 20, 30.

Try it here

Number the rolling steps

The burrito-rolling steps for new staff were pasted from one line, so every step says 1.. The cursor is on the first step.

Go down to the second step with j. Select it and the two below with V j j, then press gCtrl-arolling.txt
11. Warm the tortilla
11. Add rice and beans
21. Add the filling
31. Fold and roll
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

KeysDoes
Ctrl-a / Ctrl-xadds one to / takes one from the number under or after the cursor
5Ctrl-a / 5Ctrl-xadds five / takes five
.repeats the last one, with the same count
Ctrl-a / Ctrl-x in Visual modechanges the first number on every selected line by one
gCtrl-a / gCtrl-x in Visual modeone on the first line, two on the second, and so on
2gCtrl-a in Visual modethe same in steps of two
Ctrl-v, select a column, Ctrl-aonly the numbers inside the column
set nrformats-=octal007 becomes 008 instead of 010
set nrformats+=unsigneda dash in a date is no longer a minus sign
set nrformats+=alphaa single letter counts too, so a becomes b