Working Code
Imagine this code is open in Vim:
name = "Alice"
age = 25
city = "Seoul"
email = "alice@example.com"
Press V on the first line and the whole line is highlighted. Press j twice — three lines are now selected. Press d and all three lines disappear at once.
That's Visual mode. It's a select-first, then-act style of editing.
Try It Yourself
Try all three visual modes on this code:
const items = ["apple", "banana", "cherry", "date"];
1. Character-wise Visual mode (v):
- Place the cursor on
"apple", pressv, theneto extend selection to the end of the word. - Press
yto yank, thenpelsewhere to paste.
2. Line-wise Visual mode (V):
- On the
"banana"line, pressV, thenjto extend through"cherry". - Press
>to indent the selection.
3. Block-wise Visual mode (Ctrl+v):
- Place the cursor on
"of each line, pressCtrl+v. - Press
3jto select four lines, then pressI(uppercase) and type//. - Press
Esc—//is inserted on all four lines.
"Why?"
The three visual modes
| Mode | Key | Selection unit | Main use |
|---|---|---|---|
| Character-wise | v | Per character | Selecting words or phrases |
| Line-wise | V | Per line | Deleting, moving, or indenting lines |
| Block-wise | Ctrl+v | Rectangle | Editing multiple lines / column select |
Commands you can apply after selecting
Apply any of these after making a selection:
| Key | Action |
|---|---|
d | Delete the selection |
y | Yank the selection |
c | Delete the selection and enter Insert mode |
> | Indent |
< | Unindent |
~ | Toggle case |
U | Uppercase |
u | Lowercase |
J | Join selected lines into one |
The magic of block-wise visual
Block-wise Visual mode (Ctrl+v) is one of Vim's most powerful features. It lets you edit multiple lines simultaneously.
Prepending text to multiple lines:
1. Start block selection with Ctrl+v
2. Extend with j or k to the last line
3. Press I (uppercase) to enter Insert mode
4. Type the text
5. Esc → same text is prepended to every selected line
Appending text to multiple lines:
1. Start block selection with Ctrl+v
2. Extend with j to the last line
3. $ extends selection to the end of each line
4. Press A (uppercase) to append
5. Type the text, then Esc
Handy tips
gv: Re-select the last visual selection. Useful for repeated indenting.o: In Visual mode, move the cursor to the other end of the selection.Esc: Cancel Visual mode and return to Normal mode.
Operator + motion vs. Visual mode
The d2w (delete 2 words) from the previous lesson works without Visual mode. So when do you use Visual mode?
- When you want to see the exact range before acting
- When you need block (column) editing
- When the range is too complex to express as a motion
As you grow comfortable, you'll reach for operator+motion for simple edits and Visual mode for complex ones.
Deep Dive
Using text objects in Visual mode
Text objects work in Visual mode too:
vi" → select content inside quotes
vi{ → select content inside braces
vip → select the current paragraph
Once selected, apply d, y, c, etc. The end result matches typing di" directly, but you get to confirm the selection visually.
Add/remove comments with block editing
Paste the code below into Vim and comment it out using block Visual mode:
name = "Alice"
age = 25
city = "Seoul"
email = "alice@example.com"
- Place the cursor at the start of the first line.
- Enter block Visual mode with
Ctrl+v. - Select four lines with
3j. - Press
I(uppercase), type#, and pressEsc. - Check the result. Are all four lines commented?
To remove the comments: Ctrl+v → 3j → l (select 2 columns) → d
question: "Which visual mode lets you edit multiple lines simultaneously?" options:
question: "Which key converts a Visual-mode selection to uppercase?" options:
question: "What does gv do?" options: