Nvim Session 07: Visual Mode Mastery
Select first, then act. Visual mode lets you see what you’re affecting before you apply operators.
Pre-Session State
-
Know operators work with motions
-
Understand text objects
-
Master all three visual modes
-
Use visual block for column editing
Visual Mode Overview
| Key | Mode | Selection Type |
|---|---|---|
|
Character |
Arbitrary text selection |
|
Line |
Whole lines only |
|
Block |
Rectangular column selection |
Lesson 1: Character Visual Mode
Exercise 1.1: Basic selection
v " Enter character visual
" Move with any motion...
hjkl " Character by character
w b e " Word motions
f\{char} " Find character
/pattern " Search
Exercise 1.2: Visual + motion
The quick brown fox jumps over the lazy dog
|
v/over<CR> " Select from cursor to "over"
ve " Select to end of word
v$ " Select to end of line
vip " Select inner paragraph
v% " Select to matching bracket
Exercise 1.3: Acting on selection
After selecting:
d " Delete selection
c " Change selection (delete + insert)
y " Yank selection
> " Indent selection
< " Outdent selection
~ " Toggle case
U " Uppercase
u " Lowercase
: " Enter command mode for range
Lesson 2: Line Visual Mode
Exercise 2.1: Whole line selection
V " Select current line
j j j " Extend down 3 more lines
k " Shrink back up
Exercise 2.2: Line visual + motion
Vgg " Select from current to file start
VG " Select from current to file end
V} " Select to next paragraph
V/pattern" Select to search match
Exercise 2.3: Common line operations
Vjjd " Delete 3 lines
Vjjy " Yank 3 lines
Vjj> " Indent 3 lines
Vjj:s/old/new/g " Substitute in 3 lines
Lesson 3: Block Visual Mode
Concept: Select rectangular columns - powerful for tables and aligned text.
Exercise 3.1: Column selection
hostname1 10.0.0.1 hostname2 10.0.0.2 hostname3 10.0.0.3 hostname4 10.0.0.4
Position on first 1 of 10.0.0.1:
Ctrl-v " Enter block visual
jjj " Down 4 lines
e " To end of IP
Selection is now a rectangle covering all IPs.
Exercise 3.2: Block delete
Ctrl-v " Block visual
jjj " Select rows
ll " Extend columns
d " Delete the block
Exercise 3.3: Block insert
server1 server2 server3 server4
Add prefix to all lines:
Ctrl-v " Block visual
jjj " Select all 4 lines
I " Insert mode (capital I!)
prefix_ " Type the text
<Esc> " Apply to all lines
Result:
prefix_server1 prefix_server2 prefix_server3 prefix_server4
Exercise 3.4: Block append
file1 file2 file3
Add suffix:
Ctrl-v " Block visual
jj " Select lines
$ " Extend to end (variable length!)
A " Append mode
.txt " Type suffix
<Esc> " Apply to all
Result:
file1.txt file2.txt file3.txt
Exercise 3.5: Block change
var1 = 100 var2 = 200 var3 = 300
Change all values:
f=w " Position on first value
Ctrl-v " Block visual
jj " Select all values
e " To end of number
c " Change
0<Esc> " Replace with 0
Result:
var1 = 0 var2 = 0 var3 = 0
Lesson 4: Visual Mode Operations
Exercise 4.1: Reselect with gv
viw " Select word
<Esc> " Exit visual
gv " Reselect same area!
> " Indent it
Exercise 4.2: Switch modes in visual
While in visual mode:
v " To character visual
V " To line visual
Ctrl-v " To block visual
Exercise 4.3: Swap selection ends
v " Start visual
w w w " Extend forward
o " Jump to OTHER end of selection
b b " Now extend backward
O " In block mode: opposite corner
Exercise 4.4: Visual mode marks
viw " Select word
<Esc>
'< " Jump to start of last visual
'> " Jump to end of last visual
gv " Reselect (uses these marks)
Lesson 5: Visual + Text Objects
Exercise 5.1: Expand selection with text objects
v " Start character visual
iw " Expand to inner word
ip " Expand to inner paragraph
i" " Expand to inner quotes
Exercise 5.2: Select around and inside
vi" " Select inside quotes
va" " Select including quotes
vi( " Select inside parens
va{ " Select including braces
vit " Select inside tags
vat " Select including tags
Exercise 5.3: Nested selections
function(outer(inner))
vi( " Select "inner"
a( " Expand to include inner parens
a( " Expand to include outer parens
Lesson 6: Practical Visual Patterns
Exercise 6.1: Comment multiple lines
Ctrl-v " Block mode
jjjj " Select lines
I " Insert at start
# <Esc> " Add comment
Or with line visual:
Vjjjj " Select lines
:s/^/# / " Prepend comment
Exercise 6.2: Align columns
name:value longer_name:value2 x:v
Select and align:
Ctrl-v " Block mode
jj " Select lines
f: " Find colons
lI " Insert after colon
<Esc> " Add spaces
Exercise 6.3: Duplicate lines
Vjj " Select lines
y " Yank
p " Paste below
Exercise 6.4: Move lines
Vjj " Select lines
d " Delete (cut)
" Navigate to destination
p " Paste
Or use ex commands:
Vjj:m 20 " Move selection to line 20
Vjj:m $ " Move to end of file
Vjj:m 0 " Move to beginning
Exercise 6.5: Sort lines
Vjjjj " Select lines
:sort " Alphabetical sort
:sort n " Numeric sort
:sort u " Unique sort
Summary: Visual Mode Reference
| Command | Effect |
|---|---|
|
Character visual |
|
Line visual |
|
Block visual |
|
Reselect last visual |
|
Go to other end |
|
Go to other corner (block) |
|
Start of last visual |
|
End of last visual |
Block Mode Operations
| Command | Effect |
|---|---|
|
Insert before block (all lines) |
|
Append after block (all lines) |
|
Change block |
|
Replace block with char |
|
Delete block |
|
Indent block |
|
Outdent block |
Visual + Ex Commands
:'<,'>s/old/new/g " Substitute in selection
:'<,'>!sort " Filter through external command
:'<,'>w filename " Write selection to file
:'<,'>d " Delete selection
:'<,'>y a " Yank to register 'a'
:'<,'>m 0 " Move to beginning
:'<,'>t $ " Copy to end
Exercises to Complete
-
[ ] Select a paragraph with
vip, yank it, paste elsewhere -
[ ] Use
Ctrl-vto add a prefix to multiple lines -
[ ] Select a code block with
vi{, indent with> -
[ ] Comment multiple lines using block visual
-
[ ] Sort a visual selection with
:sort
Next Session
Session 08: Text Objects Complete - Every text object, in depth.
Session Log
| Timestamp | Notes |
|---|---|
Start |
<Record when you started> |
End |
<Record when you finished> |
Discoveries |
<What surprised you?> |