Visual Mode in Vim
Visual Mode in Vim
Visual mode enables text selection in Vim. Unlike normal mode where commands operate on text objects, visual mode highlights text first, then applies operations to the selection.
Three Visual Mode Types
| Mode | Key | Selection Type | Primary Use Case |
|---|---|---|---|
| Visual | v | Individual characters | Precise selections, partial lines |
| Visual Line | V | Entire lines | Code blocks, whole lines |
| Visual Block | ⌃V | Rectangular columns | Aligned data, column editing |
Visual Mode (v) - Character Selection
Press v to enter character-wise visual mode.
Selection behavior:
- Starts at cursor position
- Expands character by character as cursor moves
- Can span multiple lines
- Selects partial lines
Example:
This is line one
This is line two
This is line three
With cursor on "line" in line one, pressing v then moving right selects:
This is [line] one
Moving down includes characters across lines:
This is [line one
This is line t]wo
Use cases:
- Selecting part of a word
- Selecting specific text spanning multiple lines
- Precise selections where line boundaries don't matter
Visual Line Mode (V) - Full Line Selection
Press V (Shift+v) to enter line-wise visual mode.
Selection behavior:
- Always selects complete lines
- Cursor position on line doesn't matter
- Expanding selection adds entire lines
- Cannot select partial lines
Example:
function hello() {
console.log("hello");
return true;
}
With cursor anywhere on line 2, pressing V selects:
function hello() {
[ console.log("hello");]
return true;
}
Moving down adds entire lines:
function hello() {
[ console.log("hello");
return true;]
}
Use cases:
- Copying function definitions
- Deleting code blocks
- Moving entire lines
- Most common for code editing
Visual Block Mode (⌃V) - Rectangular Selection
Press ⌃V (Control+v) to enter block-wise visual mode.
Selection behavior:
- Selects same column position across lines
- Creates rectangular selection
- Width determined by horizontal movement
- Height determined by vertical movement
Example:
Name: John
Age: 30
City: LA
State: CA
Starting on "J" in "John", pressing ⌃V then moving down and right:
Name: [John]
Age: [30 ]
City: [LA ]
State: [CA ]
Use cases:
- Editing aligned columns
- Adding same text to multiple lines
- Commenting multiple lines (add
//to column) - Working with CSV/TSV data
Visual Mode Operations
Once text is selected, press an operator key:
Copy and Paste
y- Yank (copy) selectiond- Delete selection (also copies to register)p- Paste after cursorP- Paste before cursor
Modification
c- Change (delete and enter insert mode)r- Replace all selected characters with one character~- Toggle caseu- Make lowercaseU- Make uppercase
Formatting
=- Auto-indent selection>- Indent right<- Indent leftgq- Format/wrap text
Advanced
:- Run Ex command on selection (automatically adds'<,'>)!- Filter through external commandJ- Join lines (remove line breaks)
Entering and Exiting
Entering visual mode:
v- Character-wise from cursorV- Line-wise from cursor⌃V- Block-wise from cursorgv- Re-select previous visual selection
Exiting visual mode:
Esc- Exit without action- Any operator (
y,d,c, etc.) - Perform action and exit v(when invmode) - Toggle back to normal modeV(when inVmode) - Toggle back to normal mode⌃V(when in⌃Vmode) - Toggle back to normal mode
Visual Mode Indicators
When in visual mode, the bottom of the screen displays:
-- VISUAL --- Character mode-- VISUAL LINE --- Line mode-- VISUAL BLOCK --- Block mode
Switching Between Visual Modes
While in any visual mode, press a different visual mode key to switch:
Example:
- Press
v(character mode) --- VISUAL -- - Press
V(switch to line mode) --- VISUAL LINE -- - Press
⌃V(switch to block mode) --- VISUAL BLOCK --
The selection persists but changes type.
Common Patterns
Select entire function
V " Enter visual line mode
% " Jump to matching brace (selects function body)
Select inside quotes/brackets
vi" " Select inside double quotes
vi' " Select inside single quotes
vi( " Select inside parentheses
vi{ " Select inside braces
Select around quotes/brackets (includes delimiters)
va" " Select around double quotes
va' " Select around single quotes
va( " Select around parentheses
va{ " Select around braces
Visual Mode vs Text Objects
Visual mode: Highlight first, then operate
vjjd " Visual mode, down 2 lines, delete
Text objects: Operate directly without highlighting
d2j " Delete current line plus 2 down (no visual feedback)
Both achieve the same result. Visual mode provides visual feedback before committing to the action.
Links
Official Vim Documentation
- URL: https://vimhelp.org/visual.txt.html
- Summary: Complete visual mode documentation
- Related: Select All in Vim, Vim Keyboard Reference
Related Articles
- Select All in Vim - Using visual mode to select entire files
- Vim Keyboard Reference - Complete command reference
- Neovim - The Vim Ecosystem - Modern Vim setup