Skip to main content

Visual Mode

Complete guide to Vim's visual selection modes and operations.

Visual Mode Types

Character-wise Visual Mode

v    " Start character-wise visual mode
  • Selects characters individually
  • Good for selecting words, phrases, or partial lines
  • Selection highlighted as you move cursor

Line-wise Visual Mode

V    " Start line-wise visual mode
  • Selects entire lines
  • Good for selecting multiple complete lines
  • Always selects complete lines, even if cursor is mid-line

Block-wise Visual Mode

Ctrl+v    " Start block-wise visual mode
  • Selects rectangular blocks of text
  • Good for column editing and table manipulation
  • Selects columns across multiple lines

Entering Visual Mode

Starting Visual Selection

v         " Character-wise visual mode
V " Line-wise visual mode
Ctrl+v " Block-wise visual mode
gv " Reselect last visual selection

Visual Mode from Insert Mode

Ctrl+o v    " Enter visual mode from insert mode temporarily

Basic Movement

All normal mode movement commands work in visual mode:

h j k l    " Character movement
w b e " Word movement
0 ^ $ " Line movement
gg G " Document movement

Extending Selection

v3w        " Select 3 words
V5j " Select 5 lines down
Ctrl+v3j " Select 3 lines in block mode

Text Objects in Visual Mode

viw        " Select inner word
vaw " Select around word
vi" " Select inside quotes
va{ " Select around braces
vip " Select inner paragraph
vat " Select around HTML tag

Visual Mode Operations

Basic Operations

d    " Delete selection
y " Yank (copy) selection
c " Change selection (delete and enter insert mode)
x " Delete selection (same as d)
s " Substitute selection (same as c)

Case Operations

~    " Toggle case of selection
u " Convert selection to lowercase
U " Convert selection to uppercase

Indentation

>    " Indent selection
< " Unindent selection
= " Auto-indent selection

Advanced Operations

J    " Join lines in selection
gJ " Join lines without inserting spaces
r " Replace all characters in selection with single character

Block Mode Operations

Column Editing

Ctrl+v    " Start block mode
{motion} " Select column
I " Insert at beginning of block
A " Insert at end of block
c " Change block
d " Delete block

Block Mode Examples

Ctrl+v 5j I// <Esc>    " Add // comment to 5 lines
Ctrl+v 3j A; <Esc> " Add semicolon to end of 3 lines
Ctrl+v 4j c new<Esc> " Replace 4-line block with 'new'

Block Mode with Patterns

Ctrl+v G $    " Select from cursor to end of file in block mode
Ctrl+v /end " Select block to search pattern

Selection Manipulation

Extending Selection

o    " Move to other end of selection
O " Move to other corner (block mode)

Selection Boundaries

v    " Start/stop character selection
V " Start/stop line selection
Ctrl+v " Start/stop block selection

Swapping Selection Ends

o    " Move cursor to opposite end of selection

In block mode:

o    " Move to opposite corner on same line
O " Move to opposite corner on different line

Visual Mode with Registers

Using Registers

"ay    " Yank selection to register 'a'
"ad " Delete selection to register 'a'
"0p " Paste from register 0 (last yank)
"+y " Yank selection to system clipboard

Multiple Clipboards

"1y    " Store in numbered register 1
"2y " Store in numbered register 2
"ay " Store in named register 'a'
"Ay " Append to named register 'a'

Advanced Visual Mode Techniques

Sorting Selection

:sort     " Sort selected lines
:sort! " Sort in reverse order
:sort u " Sort and remove duplicates
:sort n " Sort numerically

Filtering Selection

:!command    " Filter selection through shell command
:!sort " Sort selection using external sort
:!uniq " Remove duplicate lines
:!grep pattern " Filter lines matching pattern

Arithmetic on Selection

g Ctrl+a    " Increment numbers in selection
g Ctrl+x " Decrement numbers in selection

Reformatting Selection

gq    " Format selection (wrap to textwidth)
gw " Format selection (cursor returns to start)

Searching in Selection

/pattern    " Search forward in selection
?pattern " Search backward in selection

Replace in Selection

:'<,'>s/old/new/g    " Replace in selection (automatic range)

Practical Visual Mode Examples

Code Manipulation

vip>    " Indent paragraph
V5j= " Auto-indent 5 lines
Ctrl+v 3j I# <Esc> " Add # comment to 3 lines

Text Processing

vip:sort    " Sort paragraph
V3j:s/$/;/ " Add semicolon to end of 3 lines

Table/Column Editing

Ctrl+v 5j 2l c new<Esc>    " Replace 5x2 block with 'new'
Ctrl+v G $ A |<Esc> " Add pipe to end of all lines

Copying/Moving Blocks

V5j y    " Copy 5 lines
V3j d " Cut 3 lines
Ctrl+v 4j 3l y " Copy 4x3 block

Visual Mode Tips

Efficiency Tips

  1. Use text objects - viw, vip, vi" for quick selections
  2. Use block mode for column operations
  3. Use gv to reselect last selection
  4. Use o to move to other end of selection
  5. Combine with operators for powerful editing

Common Patterns

vip     " Select paragraph
vi{ " Select inside braces
va" " Select around quotes
V/end " Select lines until pattern

Block Mode Patterns

Ctrl+v G I    " Insert at beginning of all lines
Ctrl+v $ A " Insert at end of all lines
Ctrl+v 5j c " Change 5-line column

Visual Mode Best Practices

When to Use Each Mode

  • Character mode (v): For selecting partial lines, words, or phrases
  • Line mode (V): For selecting complete lines or paragraphs
  • Block mode (Ctrl+v): For column editing, tables, or aligned text

Avoiding Common Mistakes

  1. Don't forget to exit visual mode with <Esc> when done
  2. Use appropriate mode for your selection needs
  3. Remember that operations work on entire selection
  4. Use gv to reselect instead of manually reselecting

Combining with Other Features

v/pattern<CR>d    " Select to pattern and delete
V}:sort<CR> " Select to paragraph end and sort
Ctrl+v G $y " Select entire columns and yank

Visual Mode and Macros

Recording Macros with Visual Mode

qa           " Start recording macro 'a'
v3w " Select 3 words
> " Indent selection
q " Stop recording
@a " Play macro

Visual Mode in Macro Playback

V            " Select line
@a " Apply macro to selection

Troubleshooting Visual Mode

Common Issues

  1. Selection not what expected - Check which visual mode you're in
  2. Can't exit visual mode - Press <Esc> or Ctrl+c
  3. Operations not working - Ensure you're in the right visual mode
  4. Block mode not aligning - Check tab settings and alignment

Debug Tips

:set number       " Show line numbers for reference
:set list " Show whitespace characters
:set ruler " Show cursor position

This comprehensive guide covers all aspects of Vim's visual mode. Master these techniques to efficiently select and manipulate text in your files.