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:
| Keys | Mode | Selects |
|---|---|---|
v | Visual | characters, from the start point to the cursor |
V | Visual Line | whole lines, from the start line to the cursor's line |
| Ctrl-v | Visual Block | a 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:
| Keys | Cursor starts on | After |
|---|---|---|
vd | q of quick | the uick brown fox (one character, like x) |
ved | q of quick | the brown fox |
v2ed | q of quick | the fox |
vwd | q of quick | the rown fox (the b goes too) |
vfod | q of quick | the wn fox |
vtnd | q of quick | the n fox |
vbd | b of brown | the 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:
| Keys | Selects | Lines left |
|---|---|---|
Vd | this line | one three four five |
Vjd | this line and the next | one four five |
V2jd | this line and the next two | one five |
Vkd | this line and the one above | three four five |
VGd | from here to the end of the file | one |
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
- Switch kind: press
Vduring avselection to make it linewise, orvduring aVselection to make it charwise. The start point stays where it was. - Move the other end:
ojumps the cursor to the opposite end of the selection, so you can grow it in the other direction. Onthe quick brown fox, starting on thebofbrown,veselectsbrown,omoves to theb, andbextends the selection back toquick.veobdleavesthe fox. - Leave without doing anything: Esc, or press the same key again (
vin Visual mode,Vin Visual Line mode). The text is unchanged and the cursor stays where it is.
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.
| Key | Example | After |
|---|---|---|
d or x: delete | the quick brown fox, cursor on the q, ved | the brown fox |
c or s: change | the quick brown fox, cursor on the q, vecslow<Esc> | the slow brown fox |
y: copy | big red box, cursor on the r, vey$p | big red boxred |
p: paste over | big red box, yew then vep | big big box |
U: uppercase | make it loud, veU | MAKE it loud |
u: lowercase | STOP SHOUTING, veu | stop SHOUTING |
~: toggle case | hELLO wORLD, ve~ | Hello wORLD |
r: replace every character | abcdef, v3lrx | xxxxef |
J: join lines | one / two / three / four, VjjJ | one 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:
| Keys | What it does | Without Visual mode |
|---|---|---|
ggVGy | copy the whole file | ggyG |
ggVGd | delete everything, leaving one empty line | ggdG |
ggVGU | uppercase the whole file | gggUG |
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:
- the text does not match a single motion or text object, and you want to build the selection with several moves (
vjjee) and check it before acting - you are working on several lines and do not want to count them first
- you want to paste over something with
p
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.
| Before | Keys | After |
|---|---|---|
id name email | veUw. | ID NAme email (. uppercased two characters, the length of id) |
id name email | gUiww.w. | ID NAME EMAIL |
one to five, cursor on one | Vjd. | 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:
| Before | Keys | After |
|---|---|---|
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:
gv: select the last selection again, as inveyand latergvU>and<: indent or unindent the selected lines:on a selection: opens the command line with:'<,'>already typed, so a command such as:'<,'>s/old/new/gworks only on the selected lines- Text objects and block mode, described above
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.
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.