Learn Practice Daily golf Reference

How to select a column in Vim with Visual Block mode

Press Ctrl-v to start Visual Block mode and extend the block with j and l, then press I, type, and press Esc: the text appears on every line. $A appends to the end of every line instead.

Try it here

Export them, then end them

Every line needs export in front: Ctrl-v, 2j down to the last line, I, type export once and press Esc. Then a semicolon at the end of each, though the lines are different lengths: Ctrl-v, 2j, $, A, ;, Esc.

1 of 2Insert export at the start of all three lines
1const API_URL = '/api'
1const TIMEOUT_MS = 5000
2const RETRIES = 3
NORMALLoading the editor…1/20.0s1:1

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

KeysWhat it does
Ctrl-vStart Visual Block mode: a rectangle, from the cursor.
j k / l wMake the block taller / wider.
$Stretch the block to the end of every line, however long each one is.
I, text, EscInsert the text in front of the block, on every line.
A, text, EscAppend the text after the block, on every line.
$A, text, EscAppend to the end of every line.
c, text, EscReplace the block with the text on every line.
r and a characterFill the whole block with that character.
d or xDelete the block (a column of text).
o / OJump to the opposite corner / the other corner on the same line.

The Esc does the work

While you type after I or A, the text appears on the first line only, and it can look as though the block did nothing. It's the Esc that copies it to every other line of the block. So: block first, then I or A, then type, then Esc.

This is how you comment out lines by hand (Ctrl-v, down, I, // , Esc), add a prefix to a list, or turn a column of words into a column of strings. It needs the text to line up: a block is a rectangle, so it works on columns, not on words that move around from line to line.

Without a block

When the lines don't line up, :normal runs Normal-mode keys on every line of a range instead. Select the lines with V, press :, and type normal A; to append a semicolon to each, or normal I// to comment them out, then ↵. With % in place of the selection, :%normal A; does every line in the file.