Visual Mode
Summary: in this tutorial, you will learn learn to select and manipulate text with vim's three visual modes — character, line, and block selection for powerful text editing.
Visual Mode
Visual mode lets you select text and then act on it — similar to clicking and dragging with a mouse in other editors, but much more powerful. Vim has three different types of visual selection, each useful for different tasks.
What you'll learn in this tutorial:
- Three types of Visual mode: character, line, and block selection
- How to select text using any motion you already know
- How to perform actions (delete, copy, change, indent) on selected text
- Block visual mode: a unique feature that lets you select and edit columns of text
- Practical tricks for everyday text editing with Visual mode
Three Types of Visual Mode
From Normal mode, you can enter Visual mode in three ways:
| Key | Mode | What It Selects |
|---|---|---|
v | Character-wise | Individual characters (like click-and-drag) |
V | Line-wise | Entire lines at a time |
Ctrl-v | Block-wise | A rectangular column of text |
Once you're in Visual mode, the selected text is highlighted. Use any movement command to expand your selection, then press an operator key to act on it.
Press Escape to cancel the selection and return to Normal mode.
Character-Wise Visual Mode (v)
Press v to start selecting individual characters. Then use any motion to extend the selection:
v " Start character selection at cursor position
w " Extend selection to the end of the next word
l " Extend one character to the right
j " Extend down one line
$ " Extend to the end of the line
/word " Extend to the next occurrence of "word"Everything between your starting position and your cursor position gets highlighted.
What Can You Do With the Selection?
Once you've selected text, press any of these keys:
| Key | What It Does |
|---|---|
d or x | Delete the selected text |
c | Change — delete the selection and enter Insert mode |
y | Yank (copy) the selected text |
> | Indent the selection to the right |
< | Unindent the selection to the left |
~ | Toggle case — lowercase becomes uppercase and vice versa |
u | Make the selection all lowercase |
U | Make the selection all UPPERCASE |
p | Replace the selection with whatever you've copied or deleted |
: | Enter a command that applies to the selected lines |
J | Join all selected lines into one line |
= | Auto-indent the selection (fix indentation) |
Practical Example
Say you want to copy the text "brown fox" from this line:
The quick brown fox jumps over the lazy dog.
- Move your cursor to the
binbrown - Press
vto start Visual mode - Press
etwice (to select to the end of "brown", then to the end of "fox") - Press
yto yank (copy) the selected text - Move to where you want to paste it
- Press
pto paste
Line-Wise Visual Mode (V)
Press V (capital V) to select entire lines. This is probably the most commonly used Visual mode:
V " Select the current line
j " Extend selection down one line
k " Extend selection up one line
5j " Extend selection down 5 lines
G " Extend selection to the bottom of the fileCommon Uses for Line-Wise Selection
Move a block of code:
V " Start line selection
3j " Select 4 lines total (current + 3 below)
d " Delete (cut) the selected lines
" Move cursor to the destination
p " Paste the lines below the current lineIndent a block of code:
V " Start line selection
5j " Select 6 lines
> " Indent all selected lines one level to the rightQuick indent trick: After indenting with >, press . (dot) to indent again. Each press of . adds another level of indentation. Use < with Visual mode to unindent.
Delete multiple lines:
V " Start line selection
G " Extend to the bottom of the file
d " Delete all selected linesBlock Visual Mode (Ctrl-v)
Block visual mode is one of Vim's most unique features. Press Ctrl-v to select a rectangular column of text — not whole lines or flowing characters, but a vertical block.
On Windows: If Ctrl-v pastes text from your clipboard instead of starting block visual mode, try Ctrl-q instead. Some terminal programs intercept Ctrl-v for pasting.
Selecting a Column
Imagine you have this text:
name: Alice
name: Bob
name: Charlie
name: Dave
If you put your cursor on the A in Alice, press Ctrl-v, then press j three times and $:
name: [Alice ]
name: [Bob ]
name: [Charlie]
name: [Dave ]
The [...] brackets show the selected rectangular block. You now have a column of names selected.
Insert Text on Multiple Lines (Block Insert)
This is incredibly useful! You can add the same text to the beginning of many lines at once:
Step 1: Move your cursor to where you want to insert text (e.g., the beginning of a line)
Step 2: Press Ctrl-v to start block selection
Step 3: Press j to extend the selection down to all the lines you want to modify
Step 4: Press I (capital I) to enter Insert mode at the left edge of the block
Step 5: Type the text you want to add (e.g., // to add comments)
Step 6: Press Escape
After pressing Escape, the text you typed appears on all selected lines! It may take a moment — the text appears on the first line as you type, but when you press Escape, it's applied to every line in the block.
" Example: Add "// " to the start of lines 5-10 to comment them out
5G " Go to line 5
Ctrl-v " Start block selection
5j " Select down 5 more lines (lines 5-10)
I " Insert at the beginning of the block
// <Esc> " Type "// " then press Escape — all 6 lines now have "// " prependedAppend Text on Multiple Lines (Block Append)
To add text to the end of multiple lines:
Ctrl-v " Start block selection
3j " Select 4 lines
$ " Extend to the end of each line
A " Append (Insert mode at the right edge)
; " Type a semicolon
<Esc> " Press Escape — semicolons appear at end of all selected linesDelete a Column
You can also delete a rectangular block:
Ctrl-v " Start block selection
3j " Select 4 lines down
5l " Select 5 characters to the right
d " Delete the rectangular blockThis removes a rectangular chunk of text — very handy for removing fixed-width columns from data.
Change a Column
Ctrl-v " Start block selection
3j " Select 4 lines
2w " Select 2 words wide
c " Change — deletes the selection, enters Insert mode
newtext " Type replacement (applies to all lines when you Escape)
<Esc> " Apply to all selected linesReselecting (gv)
After you've left Visual mode, press gv to reselect the same text you had selected before. This is handy when you need to do multiple operations on the same selection:
V5j " Select 6 lines
> " Indent them (leaves Visual mode)
gv " Reselect those same 6 lines
> " Indent them again (now 2 levels)Switching Between Visual Modes
While you're in Visual mode, you can switch between the three types:
| Current Mode | Press | Switch To |
|---|---|---|
| Any visual | v | Character-wise |
| Any visual | V | Line-wise |
| Any visual | Ctrl-v | Block-wise |
For example, if you started with v (character-wise) but realize you want to select whole lines, just press V to switch to line-wise without losing your position.
Swapping the Selection Boundary
Press o while in Visual mode to jump your cursor to the other end of the selection. This lets you expand or shrink the selection from the opposite side:
v " Start then move out
3w " Select some words forward
o " Cursor jumps to the start of the selection
2b " Extend the selection backward — the other end stays fixedVisual Mode + Text Objects
You can use text objects in Visual mode for very precise selections:
v " Start character-wise visual
iw " Select the inner word (the whole word, no matter where cursor is)Even better, you can go straight to visual selection of a text object:
viw " visually select the Inner Word
vi" " visually select text Inside double quotes
vi( " visually select text Inside parentheses
viB " visually select text Inside curly braces {}
vit " visually select text Inside an HTML/XML tag
vap " visually select Around a Paragraph (including blank line after)vip and vap are great for code! Since functions and code blocks are usually separated by blank lines, vip selects the entire block of code you're in. Try it — it's very satisfying.
Practical Examples
Comment Out Multiple Lines
Using block visual mode to add # at the start of several lines:
Ctrl-v " Start block selection
4j " Select 5 lines
I " Insert mode at the left of the block
# <Esc> " Type "# " then Escape — all 5 lines are now commentedTo uncomment (remove the #):
Ctrl-v " Start block selection
4j " Select the same 5 lines
l " Select 2 characters (# and the space)
d " Delete the block — removes "# " from all linesSurround a Word with Quotes
viw " Visually select the word
c""<Esc> " Change it to empty quotes, placing cursor between them
P " Paste the original word between the quotesOr a simpler approach:
ciw"<Ctrl-r>""<Esc>
" This changes the word, types a quote, inserts the original word from the " register,
" then types a closing quote. (Don't worry if this feels advanced — the first method works great!)Sort Selected Lines
V " Start line selection
5j " Select 6 lines
:sort " When you press :, Vim fills in :'<,'> automatically
" Type "sort" and press Enter — the selected lines are sorted alphabeticallyReplace Text in a Visual Selection
V " Select some lines
5j
:s/old/new/g " Vim fills in :'<,'>s/old/new/g
" This replaces only within the selected linesSummary
Here's what you've learned:
v— character-wise Visual mode (select individual characters)V— line-wise Visual mode (select whole lines)Ctrl-v— block Visual mode (select rectangular columns)- After selecting, use operators:
d(delete),c(change),y(copy),>(indent),<(unindent) - Block insert:
Ctrl-v→ select →I→ type → Escape (adds text to all lines) - Block append:
Ctrl-v→ select →$→A→ type → Escape (adds to end of all lines) gvreselects your last Visual mode selectionoswaps the cursor to the other end of the selection- Combine with text objects:
viw,vi",vipfor quick selections - Visual mode works with substitute:
:'<,'>s/old/new/g
Practice: Visual Mode
Create a file with this content:
vim visual-practice.txtPress i, type the following, then press Escape:
apple
banana
cherry
date
elderberry
name = "Alice"
age = 30
city = "New York"
function greet() {
console.log("hello");
console.log("world");
console.log("test");
}
Now try these tasks:
- Select the first 5 fruit lines with
V4j, then indent them with> - Press
uto undo, then try: select the fruit lines again withV4j, yank them withy, go to the end withG, paste withp - Use block visual mode (
Ctrl-v) on the fruit lines: select down 4 lines and insert-at the beginning (pressI, type-, press Escape) - On the
name/age/citylines, select the word inside the quotes usingvi"and change it withc - Select the three
console.loglines withV2jand delete them withd - Undo with
u, then select those lines again withV2jand sort them with:sort - Use
gvto reselect and indent with>
Show Solution
" 1. Line select and indent
gg " Go to the first line
V " Start line-wise Visual mode
4j " Select 5 lines (apple through elderberry)
> " Indent — all 5 lines shift right
" 2. Copy and paste lines
u " Undo the indent
gg " Back to top
V4j " Select the 5 fruit lines
y " Yank (copy) them
G " Go to the last line
p " Paste below — fruits appear at the bottom
" 3. Block insert (add "- " to the start of each fruit line)
gg " Go to "apple" line
Ctrl-v " Start block visual mode
4j " Select down 4 lines
I " Enter Insert mode at the left edge
- <Esc> " Type "- " then Escape
" Result: all fruit lines now have "- " at the start
" 4. Change text inside quotes
" Move cursor to the "name" line
f" " Jump to the first quote
vi" " Select everything inside the quotes ("Alice")
c " Delete selection and enter Insert mode
Bob<Esc> " Type the new name
" 5. Delete console.log lines
" Move to the first console.log line
V " Start line selection
2j " Select 3 lines
d " Delete them
" 6. Undo and sort
u " Undo — the console.log lines come back
V2j " Select the same 3 lines
:sort " Type :sort and press Enter — lines are sorted alphabetically
" Result: console.log("hello"), console.log("test"), console.log("world")
" 7. Reselect and indent
gv " Reselect the same 3 lines
> " Indent them one more level
:q! " Quit without savingWritten by the ShellRAG Team
The ShellRAG editorial team writes practical, beginner-friendly Vim tutorials with tested code examples and real-world use cases. Every article is technically reviewed for accuracy and updated regularly.
Learn more about us →