Vim Visual mode: select text with v and V

Visual mode is how you select text in Vim. Press v, move the cursor with any motion, and the text between where you started and where you are now lights up. Then press a command such as d, y or U and it acts on the selection. V does the same with whole lines, and ggVG selects everything.

Each example shows a line before and after, with the keys in between. Unless a row says otherwise, the cursor starts on the first character. Every example was checked in Neovim, and every one the game supports was also replayed in the Vim engine that runs it.

Select first, then act

In Normal mode an edit is verb first: dw says "delete", then "one word". Visual mode turns that around. You mark the text first, see exactly what is covered, and only then say what to do with it.

There are three kinds of selection:

KeysModeSelects
vVisualcharacters, from the start point to the cursor
VVisual Linewhole lines, from the start line to the cursor's line
Ctrl-vVisual Blocka rectangle of columns (see block mode below)

While a selection is active, Vim shows -- VISUAL -- or -- VISUAL LINE -- at the bottom of the screen. Once a command acts on the selection, the selection ends and you are back in Normal mode, or in Insert mode after c.

Select characters: v

Press v and the character under the cursor is selected. Every motion you make from there grows or shrinks the selection, and both ends are always included. Here is v with a few motions on the quick brown fox, each followed by d to show what was covered:

KeysCursor starts onAfter
vdq of quickthe uick brown fox (one character, like x)
vedq of quickthe  brown fox
v2edq of quickthe  fox
vwdq of quickthe rown fox (the b goes too)
vfodq of quickthe wn fox
vtndq of quickthe n fox
vbdb of brownthe rown fox

Compare vwd with dw. After an operator, w stops just before the next word, so dw keeps the b of brown. In Visual mode the selection runs up to and including the character the cursor sits on, so vwd takes the b as well. When you want a word and nothing else, select to its end with e, not to the next word with w.

The last row goes backwards. b moves the cursor to the start of quick, and the selection covers everything from there back to the b where you pressed v, both ends included.

Counts work as they do in Normal mode: v3l selects four characters (the one you started on and three more), and v2e selects to the end of the second word. Moving up or down with j or k keeps the selection charwise: it runs from the start point, through the end of the line, to the same column on the line below.

Selecting to the end of the line: g_ instead of $

v$ selects to the end of the line and the line break after it. So v$d deletes the rest of the line and pulls the next line up onto this one. On the quick brown fox with jumps over below it, v$d from the q leaves the single line the jumps over.

To stop at the last character, use g_: vg_d leaves the  and keeps jumps over on its own line. Outside Visual mode you do not need either: D deletes to the end of the line, and y$ copies to it.

Select lines: V

V selects the whole current line, wherever the cursor is in it. Moving up or down adds whole lines, and the column the cursor is on does not matter. In each row the buffer holds the five lines one to five, and the cursor starts on two:

KeysSelectsLines left
Vdthis lineone three four five
Vjdthis line and the nextone four five
V2jdthis line and the next twoone five
Vkdthis line and the one abovethree four five
VGdfrom here to the end of the fileone

A selection made with V is copied as whole lines, so it also pastes as whole lines. Vjy on two copies two and three, and Gp then puts them below five.

Each of these has a Normal-mode twin (dd, dj, 3dd, dk, dG; see deleting lines with operators). V is worth the extra key when you are not sure how many lines you need: press j until the highlight covers them, then act.

Switch, adjust and leave a selection

What you can do with a selection

Operators work on a selection without a motion: the selection is the motion. A few keys do something different in Visual mode from what they do in Normal mode, like u, which lowercases instead of undoing.

KeyExampleAfter
d or x: deletethe quick brown fox, cursor on the q, vedthe  brown fox
c or s: changethe quick brown fox, cursor on the q, vecslow<Esc>the slow brown fox
y: copybig red box, cursor on the r, vey$pbig red boxred
p: paste overbig red box, yew then vepbig big box
U: uppercasemake it loud, veUMAKE it loud
u: lowercaseSTOP SHOUTING, veustop SHOUTING
~: toggle casehELLO wORLD, ve~Hello wORLD
r: replace every characterabcdef, v3lrxxxxxef
J: join linesone / two / three / four, VjjJone two three / four

With V, the same keys act on whole lines: VU on make it loud gives MAKE IT LOUD, and V~ on hELLO wORLD gives Hello World.

The capital forms D, X, Y, C and S always work on whole lines, even in a v selection. vD deletes the whole line, not one character.

Pasting over a selection: p and P

p in Visual mode replaces the selection with what you last copied. That makes it the quickest way to replace a word with another: copy the new word, select the old one, press p.

The catch is that the text you replaced is now what p will paste next. On big red box, yew copies big and vep turns red into big, but a second wvep on box pastes red, giving big big red. To paste the same text over several selections, use P instead: it replaces the selection and keeps what you copied, so yewvePwveP gives big big big. Visual P works this way in Neovim and in current versions of Vim.

Select all in Vim: ggVG

Vim has no single select-all key. gg goes to the first line, V starts a line selection, and G extends it to the last line, so ggVG highlights the whole file. Add a command after it:

KeysWhat it doesWithout Visual mode
ggVGycopy the whole fileggyG
ggVGddelete everything, leaving one empty lineggdG
ggVGUuppercase the whole filegggUG

To copy the file to the system clipboard so you can paste it into another program, Vim needs to be built with clipboard support (vim --version lists +clipboard; Neovim uses an external clipboard tool). Then ggVG"+y copies everything into the clipboard. Vimrealm has a single register, so it does not practise "+.

Select a word, a quoted string or a paragraph

In Vim, text objects work in Visual mode too, and they are the fastest way to select a whole thing around the cursor: viw selects the word under the cursor, vi" the inside of a quoted string, vi( the inside of parentheses, and vip the paragraph. On sum(a, b, c) with the cursor on the a, vi(d leaves sum().

Vimrealm does not support text objects in Visual mode yet. You can make the same edits without selecting: di( does what vi(d does in one key fewer, and repeats better, as the next section shows.

Visual mode or operator + motion?

Visual mode costs one extra key, but you see what you are about to change before you change it. Reach for it when:

Operator + motion is better when you know the distance, and especially when you want to repeat the edit with the dot command. After a Visual-mode edit, . repeats it on the same amount of text from the cursor: the same number of characters for a v selection on one line, the same number of lines for a V selection. It does not replay the motions you used to select.

BeforeKeysAfter
id name emailveUw.ID NAme email (. uppercased two characters, the length of id)
id name emailgUiww.w.ID NAME EMAIL
one to five, cursor on oneVjd.five (two lines, then two more)

So for word-sized edits you mean to repeat, use an operator with a text object, as in the second row. Linewise selections repeat well, because the lines you want next are usually the same count.

Block mode: Ctrl-v

Ctrl-v starts Visual Block mode, which selects a rectangle: the same columns on every line from the start point to the cursor. It is the Vim way to edit a column of text, for example to add a prefix to several lines at once. Vimrealm does not have block mode, so these are Vim facts, not something the game practises:

BeforeKeysAfter
one / two / three<C-v>jjI- <Esc>- one / - two / - three
a / bb / ccc<C-v>jj$A;<Esc>a; / bb; / ccc;
a1 x / b2 y / c3 z<C-v>jjld x /  y /  z

I inserts before the block on every line and A appends after it. The text appears on the first line while you type and on the others when you press Esc. With $, the block reaches the end of every line, however long. In Vim on Windows, where Ctrl-v is often paste, Ctrl-q starts block mode instead.

Real Vim has more

A few more Visual-mode commands are worth knowing once v and V feel natural. Vimrealm does not teach these yet:

Vimrealm teaches v, V and the commands that act on a selection in quests scored by keystrokes. Chapters 1–5, which teach the motions you select with, are free, with no sign-up.

Play Vimrealm free

Where the commands are taught: the motions in chapters 1–5; x, r, J and ~ in chapter 6, The Breaking Arts; d, c, s and the case operators in chapter 8, Operator Sanctum; y, p, P and . in chapter 10, Hall of Echoes; v, V and U in chapter 12, Chamber of Light. Chapters 6–13 are part of the full version ($7.99, one payment). See how the course works or keep the cheat sheet entry for Visual mode open while you practise.