Learn Practice Daily golf Reference

How to select lines in Vim

In Normal mode, press V to select the whole line, extend the selection with j or k (V3j for four lines, Vip for the paragraph), then press an operator: y to copy, d to delete, > to indent.

Selecting in Vim is Visual mode, and there are three kinds. V selects whole lines (Visual Line mode); v selects characters, from where you start to where the cursor is; Ctrl-v selects a rectangle (Visual Block mode). The bottom line shows which one you're in: -- VISUAL LINE --, -- VISUAL -- or -- VISUAL BLOCK --. Any motion moves the end of the selection, and Esc leaves without doing anything.

Try it here

Carry two tasks over

The two open tasks move to tomorrow. The cursor is on the first: V selects its line, j adds the next, d cuts both. Then G goes to the last line and p puts them below it.

Select both open tasks with Vj, cut them with d, paste them under Tomorrow
2## Today
1- [x] Ship the release
3- [ ] Write the changelog
1- [ ] Reply to Sam
2
3## Tomorrow
+- [ ] Write the changelog
+- [ ] Reply to Sam
NORMALLoading the editor…0.0s3:3

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

KeysSelects
Vthe current line
Vj / Vkthis line and the one below / above
V3jthis line and the three below it
Vipthe paragraph, up to the blank lines around it
VG / Vggfrom this line to the end / the start of the file
v, then a motioncharacters: vw, ve, v$ to the end of the line
viw / vi(the word under the cursor / what is inside the brackets
onot a selection: jumps to the other end, so you can move that end instead
gvthe last selection again

Then act on it

A selection waits for a command. y copies it, d (or x) cuts it, c deletes it and starts Insert mode, > and < indent and unindent it, J joins the lines into one, and p replaces it with what you copied. Pressing : starts a command on the selected lines: Vim types :'<,'> for you, so :'<,'>s/old/new/g replaces only inside them.

After the command, Vim is back in Normal mode and the selection is gone.

Fix a selection instead of starting over

A selection has two ends, and motions move the end the cursor is on. Started one line too low? Press o and the cursor jumps to the other end, so k now grows the selection upwards. Lost it to a stray Esc, or want a second command on the same lines? gv selects them again: V2j> then gv> indents three lines two levels.

Practise the keys

Practise selecting

Type the keys for each one, as you would in Vim.

Select this line and the two below it.
scroll here, then press the keys3 keys
1 of 5
Select this line and the two below it.