1.3 โ Visual Mode โ Selecting Text
1.3 โ Visual Mode โ Selecting Text#
Visual mode lets you select text and then operate on the selection. It bridges the gap between Vimโs command-based approach and the familiar โselect then actโ paradigm.
What is Visual Mode?#
Visual mode allows you to visually select a region of text, then apply an operator to that selection. Itโs like highlighting text with your mouse, but using keyboard commands.
When youโre in Visual mode:
-- VISUAL --,-- VISUAL LINE --, or-- VISUAL BLOCK --appears at the bottom- Selected text is highlighted
- Movement commands extend the selection
Three Types of Visual Mode#
Vim offers three visual selection modes:
| Mode | Command | Selects | Indicator |
|---|---|---|---|
| Character-wise | v | Characters | -- VISUAL -- |
| Line-wise | V | Entire lines | -- VISUAL LINE -- |
| Block-wise | Ctrl-v | Rectangular block | -- VISUAL BLOCK -- |
Character-wise Visual Mode (v)#
Selects individual characters. Use for selecting parts of lines.
This is [some text] to select
^^^^^^^^^^^
Selected with v
Line-wise Visual Mode (V)#
Selects entire lines. Use for operating on multiple lines.
[First line of text ]
[Second line of text ]
[Third line of text ]
^^^^^^^^^^^^^^^^^^^^^^^^
Selected with V
Block-wise Visual Mode (Ctrl-v)#
Selects a rectangular block. Use for column editing.
Line one [text] here
Line two [text] here
Line thr [text] here
^^^^^^
Selected with Ctrl-v
Using Visual Mode#
Basic Workflow#
- Position cursor at selection start
- Enter Visual mode (
v,V, orCtrl-v) - Move cursor to extend selection
- Apply an operator
Movement in Visual Mode#
All Normal mode movements work in Visual mode:
| Command | Selection extends toโฆ |
|---|---|
h/j/k/l | Adjacent character/line |
w/b | Next/previous word |
e | End of word |
$ | End of line |
0 | Start of line |
gg | Start of file |
G | End of file |
} | Next paragraph |
{ | Previous paragraph |
Operators in Visual Mode#
After selecting, apply an operator:
| Operator | Action |
|---|---|
d or x | Delete selection |
c | Change selection (delete + insert) |
y | Yank (copy) selection |
> | Indent selection |
< | Unindent selection |
~ | Toggle case |
u | Make lowercase |
U | Make uppercase |
J | Join lines |
: | Enter command for selection |
Visual Mode Techniques#
Reselecting the Last Selection#
Press gv to reselect the last visual selection. Useful when you need to operate on the same region again.
Switching Selection Modes#
While in Visual mode, you can switch between modes:
| Current Mode | Press | New Mode |
|---|---|---|
| Character | V | Line |
| Character | Ctrl-v | Block |
| Line | v | Character |
| Line | Ctrl-v | Block |
| Block | v | Character |
| Block | V | Line |
Adjusting Selection Boundaries#
Press o to jump to the other end of the selection. This lets you adjust both boundaries.
[Selected text here]
^ ^
Start End (cursor)
Press 'o':
[Selected text here]
^ ^
Cursor (start) End
Selecting with Text Objects#
In Visual mode, you can use text objects to expand selection:
| Command | Selects |
|---|---|
aw | A word (includes surrounding space) |
iw | Inner word |
a" | A quoted string (includes quotes) |
i" | Inner quoted string |
a( or ab | A parenthesized block |
i( or ib | Inner parenthesized block |
a{ or aB | A braced block |
i{ or iB | Inner braced block |
at | A tag block (HTML/XML) |
it | Inner tag block |
Block Visual Mode Power#
Block mode (Ctrl-v) enables column-oriented editing:
Insert at Start of Multiple Lines#
Ctrl-vto enter Block modejto extend down multiple linesI(capital I) to insert at start- Type your text
Escโ text appears on all lines
Example: Add // to comment multiple lines:
line one โ // line one
line two โ // line two
line three โ // line three
Append at End of Multiple Lines#
Ctrl-vto enter Block modejto extend down multiple lines$to extend to end of each lineAto append- Type your text
Escโ text appears on all lines
Delete a Column#
Ctrl-vto enter Block mode- Move to select a column
dto delete
Replace a Column#
Ctrl-vto enter Block mode- Select the column
cto change- Type replacement
Escโ replacement appears on all lines
Practice Exercises#
Exercise 1: Character Selection#
- Open a file with text
- Press
vto enter Visual mode - Use
wto select words,$to select to end of line - Press
dto delete the selection
Exercise 2: Line Selection#
- Press
Vto enter Line Visual mode - Press
jseveral times to select multiple lines - Press
>to indent them - Press
gvto reselect - Press
<to unindent
Exercise 3: Block Selection#
- Create several lines with similar structure
- Press
Ctrl-vto enter Block mode - Move to create a rectangular selection
- Press
dto delete the block - Undo with
u
Exercise 4: Multi-line Insert#
- Create several lines
Ctrl-v, thenjseveral times- Press
I, type#, pressEsc - All lines now start with
#
Exercise 5: Text Objects in Visual#
- Position cursor inside a quoted string
- Press
vtheni"to select inside quotes - Press
yto yank - Move elsewhere and
pto paste
Visual Mode vs. Operators + Motions#
Visual mode and Normal mode operators can often achieve the same result:
| Visual Mode | Normal Mode | Result |
|---|---|---|
viw then d | diw | Delete inner word |
V then d | dd | Delete line |
v$ then y | y$ | Yank to end of line |
When to use Visual mode:
- You want to see what youโre selecting before acting
- Youโre unsure of the exact motion
- You need to make adjustments to the selection
- Block operations
When to use operators + motions:
- You know exactly what you want
- Speed is important
- You want the operation to be repeatable with
.
Summary#
| Mode | Command | Use Case |
|---|---|---|
| Character | v | Select parts of lines |
| Line | V | Select whole lines |
| Block | Ctrl-v | Column editing |
| Technique | Command |
|---|---|
| Reselect last | gv |
| Switch ends | o |
| Exit | Esc |
Visual mode is your โpreview before commitโ tool. Use it when you want to see exactly what will be affected before taking action.