Learn Practice Daily golf Reference

How to select all in Vim

In Normal mode, press gg to go to the first line, V to start selecting lines and G to extend to the last: ggVG. Then y copies everything, d deletes it and > indents it.

Try it here

Indent the whole snippet

These lines are moving inside a function, so every one of them needs one more level of indentation. Select them all with ggVG, then indent with >.

Select everything with ggVG and indent it with >
4const total = items.reduce(
3 (sum, item) => sum + item.price,
2 0,
1);
5console.log(total);
NORMALLoading the editor…0.0s5:1

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

KeysWhat it does
ggVGSelect every line (and see it highlighted).
ggVG, then y / d / > / =Copy / delete / indent / reindent everything.
ggVG, then "+yCopy everything to the system clipboard.
ggyGCopy everything, without selecting.
ggdGDelete everything, without selecting.
gg=GReindent the whole file.
:%y ↵Copy every line, wherever the cursor is.
:%y+ ↵Copy every line to the system clipboard.
:%d ↵Delete every line.

How ggVG works

It's three commands in a row. gg moves to the first line. V starts Visual Line mode, which selects whole lines, so it doesn't matter which column you're in. G moves to the last line, and the selection follows. Nothing has happened to the text yet: the next key decides what does. y copies it, d deletes it, > indents it one level, = reindents it with the rules for the file type, and Esc cancels.

Often you don't need to select

The selection is there so you can see what's about to change. When you already know it's the whole file, skip it: an operator followed by a motion from the top does the same thing. ggyG copies everything, ggdG deletes everything, gg=G reindents everything.

On the command line, % means every line, so :%y copies the whole file and :%d deletes it, and neither moves the cursor. :%y+ copies it to the system clipboard, the quickest way to get a whole file into another program.