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:

KeyModeWhat It Selects
vCharacter-wiseIndividual characters (like click-and-drag)
VLine-wiseEntire lines at a time
Ctrl-vBlock-wiseA 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:

KeyWhat It Does
d or xDelete the selected text
cChange — delete the selection and enter Insert mode
yYank (copy) the selected text
>Indent the selection to the right
<Unindent the selection to the left
~Toggle case — lowercase becomes uppercase and vice versa
uMake the selection all lowercase
UMake the selection all UPPERCASE
pReplace the selection with whatever you've copied or deleted
:Enter a command that applies to the selected lines
JJoin 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.
  1. Move your cursor to the b in brown
  2. Press v to start Visual mode
  3. Press e twice (to select to the end of "brown", then to the end of "fox")
  4. Press y to yank (copy) the selected text
  5. Move to where you want to paste it
  6. Press p to 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 file

Common 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 line

Indent a block of code:

V        " Start line selection
5j       " Select 6 lines
>        " Indent all selected lines one level to the right

Quick 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 lines

Block 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 "// " prepended

Append 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 lines

Delete 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 block

This 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 lines

Reselecting (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 ModePressSwitch To
Any visualvCharacter-wise
Any visualVLine-wise
Any visualCtrl-vBlock-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 fixed

Visual 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 commented

To 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 lines

Surround 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 quotes

Or 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 alphabetically

Replace 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 lines

Summary

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)
  • gv reselects your last Visual mode selection
  • o swaps the cursor to the other end of the selection
  • Combine with text objects: viw, vi", vip for quick selections
  • Visual mode works with substitute: :'<,'>s/old/new/g
🏋️

Practice: Visual Mode

Create a file with this content:

vim visual-practice.txt

Press 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:

  1. Select the first 5 fruit lines with V4j, then indent them with >
  2. Press u to undo, then try: select the fruit lines again with V4j, yank them with y, go to the end with G, paste with p
  3. Use block visual mode (Ctrl-v) on the fruit lines: select down 4 lines and insert - at the beginning (press I, type - , press Escape)
  4. On the name/age/city lines, select the word inside the quotes using vi" and change it with c
  5. Select the three console.log lines with V2j and delete them with d
  6. Undo with u, then select those lines again with V2j and sort them with :sort
  7. Use gv to 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 saving
Was this page helpful?
SR

Written 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 →