Learn Practice Daily golf Reference

How to select text in Vim (Visual mode)

In Normal mode, press v and move the cursor, with e to the end of a word or j down a line. The highlight follows. Then d deletes the selection, y copies it and c replaces it. V selects whole lines, Ctrl-v a rectangle.

The ninja stretching a glowing blue band between two pegs over three word tiles in the middle of a row, only those tiles lit, a v keycap on the counter.

The pineapple has run out, and the specials board still promises extra pineapple on the al pastor burrito. Those three words have to go, and you'd like to see exactly what's going before it goes. The cursor is on the space after burrito.

v starts Visual mode, and -- VISUAL -- shows at the bottom. From then on every move stretches a highlight from where you pressed v to the cursor. e jumps to the end of the word ahead, so each press takes in one more word. The next command you give acts on whatever is highlighted.

Before
FRIDAY SPECIALS
Al pastor burrito with extra pineapple 9.50
Three churros in cinnamon sugar 3.00
veee
FRIDAY SPECIALS
Al pastor burrito with extra pineapple 9.50
Three churros in cinnamon sugar 3.00
d
FRIDAY SPECIALS
Al pastor burrito 9.50
Three churros in cinnamon sugar 3.00
v starts the highlight on the space, three presses of e stretch it to the end of pineapple, and d deletes all of it
Try it here

The pineapple is off

Take the pineapple off the board, then swap the churros' topping for tonight's.

Start the highlight with v, stretch it to the end of pineapple with e three times, then delete it with dspecials.txt
1FRIDAY SPECIALS
2Al pastor burrito with extra pineapple 9.50
1Three churros in cinnamon sugar 3.00
NORMALLoading the editor…0.0sLn 2, Col :18
Open this text in the playground

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

Select a word, a quoted string or a paragraph in one go

Any move works while you select, so w, b, j and $ all stretch the highlight. The selection always includes the character under the cursor, though. v then w reaches one letter into the next word, which is why e is the better way to stop at the end of one.

For the usual shapes there's a shortcut. After v, i means "inside" rather than Insert mode, and the letter after it names the shape. viw selects the word under the cursor from anywhere in it, vi" what's between the quotes around the cursor, and vip the paragraph, meaning the lines between two blank ones. Swap i for a to take the edges as well. va" includes the quotes, and vap includes the blank line after the paragraph.

Try it here

Ready for the weekend

The board has a review to show off and a notice that is out of date. The cursor is in the review.

Select the review inside its quotes with vi", then press U to turn it into capitalsspecials.txt
7FRIDAY SPECIALS
6Al pastor burrito 9.50
5Three churros in chocolate dip 3.00
4
3Closed Monday for a deep clean.
2Back Tuesday at 11:00.
1
8Review of the week: "best burrito in town"
NORMALLoading the editor…0.0sLn 8, Col :27
Open this text in the playground

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

What you can do with a selection

Once the text is highlighted, the next key says what happens to it, and Visual mode ends. d deletes it, y copies it, and c deletes it and leaves you typing in its place. p swaps it for whatever you copied last. > and < indent and unindent every line the selection touches. U turns the letters into capitals, u into small letters, and ~ flips each one.

: opens the command line with :'<,'> already typed, which means "the lines the selection is on". Type a command after it, such as s/old/new/g, and it runs on those lines only. It always takes whole lines, even when you selected a single word.

Check yourself

A command on a selected word

The cursor is on extra. In Normal mode you press viw to select the word, then press :. Vim types '<,'> for you, and you add s/a/A/g and press ↵. What does the line say now?

BeforeJust a picture: pick an answer below (1, 2, 3)
1Al pastor burrito with extra pineapple 9.50
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

Take in a word you missed, or reselect

You've highlighted chocolate dip and then notice the in in front of it should go too. Moving the cursor now only moves the end it's on, so b would shrink the selection from the right. Press o first. The cursor jumps to the start of the highlight, and b stretches it back over in. Press o again to return to the end you started from.

gv brings back the last selection, the same characters as before. Use it after a stray Esc, or after U when you want to do something more to the same words.

Check yourself

Stretch it the other way

The cursor is on the first letter of extra. In Normal mode you press v, then e twice, then o, then b, and finally U for capitals. What does the line say now?

BeforeJust a picture: pick an answer below (1, 2, 3)
1Al pastor burrito with extra pineapple 9.50
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.
Practise the keys

Visual mode keys from memory

Each one starts in Normal mode unless it says otherwise. Type the keys you would press.

Which keys do it?
Select the word under the cursor. The cursor is in the middle of it.
scroll here, then press it
1 of 5
Select the word under the cursor. The cursor is in the middle of it.

Select whole lines, a column or the whole file

v picks characters, which suits words and phrases. For whole lines, start with V instead. The highlight then covers every line the cursor passes over, wherever on the line it is, and Select lines has more on that. If you're halfway through a v selection, pressing V turns it into whole lines.

Ctrl-v (hold Ctrl, press v) selects a rectangle, the same columns on several lines. It's how you type the same thing at the start of every line, and Visual Block mode shows how. For everything in the file at once, ggVG does it, and Select all has the shortcuts that skip the selection.

KeysWhat it does
vStart selecting characters, at the cursor.
v, then e / w / j / $Stretch the selection to wherever the move takes the cursor.
viw / vawSelect the word under the cursor / the word and the space after it.
vi" / va"Select inside the quotes / the quotes as well.
vi( / vitSelect inside the brackets / inside an HTML tag.
vip / vapSelect the paragraph / the paragraph and the blank line after it.
VSelect whole lines (select lines).
Ctrl-vSelect a rectangle (Visual Block mode).
oJump to the other end of the selection.
gvSelect the last selection again.
d / y / cDelete it / copy it / delete it and start typing.
pReplace it with what you copied last.
> / <Indent / unindent the lines it touches.
U / u / ~Capitals / small letters / swap the case.
JJoin the selected lines into one.
:Run a command on the selected lines, from :'<,'>.
EscStop selecting and change nothing.