1.3 โ€” Visual Mode โ€” Selecting Text

1.3 โ€” Visual Mode โ€” Selecting Text#

Visual mode lets you select text and then operate on the selection. It bridges the gap between Vimโ€™s command-based approach and the familiar โ€œselect then actโ€ paradigm.

What is Visual Mode?#

Visual mode allows you to visually select a region of text, then apply an operator to that selection. Itโ€™s like highlighting text with your mouse, but using keyboard commands.

When youโ€™re in Visual mode:

Three Types of Visual Mode#

Vim offers three visual selection modes:

ModeCommandSelectsIndicator
Character-wisevCharacters-- VISUAL --
Line-wiseVEntire lines-- VISUAL LINE --
Block-wiseCtrl-vRectangular block-- VISUAL BLOCK --

Character-wise Visual Mode (v)#

Selects individual characters. Use for selecting parts of lines.

This is [some text] to select
        ^^^^^^^^^^^
        Selected with v

Line-wise Visual Mode (V)#

Selects entire lines. Use for operating on multiple lines.

[First line of text     ]
[Second line of text    ]
[Third line of text     ]
 ^^^^^^^^^^^^^^^^^^^^^^^^
 Selected with V

Block-wise Visual Mode (Ctrl-v)#

Selects a rectangular block. Use for column editing.

Line one [text] here
Line two [text] here
Line thr [text] here
         ^^^^^^
         Selected with Ctrl-v

Using Visual Mode#

Basic Workflow#

  1. Position cursor at selection start
  2. Enter Visual mode (v, V, or Ctrl-v)
  3. Move cursor to extend selection
  4. Apply an operator

Movement in Visual Mode#

All Normal mode movements work in Visual mode:

CommandSelection extends toโ€ฆ
h/j/k/lAdjacent character/line
w/bNext/previous word
eEnd of word
$End of line
0Start of line
ggStart of file
GEnd of file
}Next paragraph
{Previous paragraph

Operators in Visual Mode#

After selecting, apply an operator:

OperatorAction
d or xDelete selection
cChange selection (delete + insert)
yYank (copy) selection
>Indent selection
<Unindent selection
~Toggle case
uMake lowercase
UMake uppercase
JJoin lines
:Enter command for selection

Visual Mode Techniques#

Reselecting the Last Selection#

Press gv to reselect the last visual selection. Useful when you need to operate on the same region again.

Switching Selection Modes#

While in Visual mode, you can switch between modes:

Current ModePressNew Mode
CharacterVLine
CharacterCtrl-vBlock
LinevCharacter
LineCtrl-vBlock
BlockvCharacter
BlockVLine

Adjusting Selection Boundaries#

Press o to jump to the other end of the selection. This lets you adjust both boundaries.

[Selected text here]
^                  ^
Start              End (cursor)

Press 'o':

[Selected text here]
^                  ^
Cursor (start)     End

Selecting with Text Objects#

In Visual mode, you can use text objects to expand selection:

CommandSelects
awA word (includes surrounding space)
iwInner word
a"A quoted string (includes quotes)
i"Inner quoted string
a( or abA parenthesized block
i( or ibInner parenthesized block
a{ or aBA braced block
i{ or iBInner braced block
atA tag block (HTML/XML)
itInner tag block

Block Visual Mode Power#

Block mode (Ctrl-v) enables column-oriented editing:

Insert at Start of Multiple Lines#

  1. Ctrl-v to enter Block mode
  2. j to extend down multiple lines
  3. I (capital I) to insert at start
  4. Type your text
  5. Esc โ€” text appears on all lines

Example: Add // to comment multiple lines:

line one          โ†’    // line one
line two          โ†’    // line two
line three        โ†’    // line three

Append at End of Multiple Lines#

  1. Ctrl-v to enter Block mode
  2. j to extend down multiple lines
  3. $ to extend to end of each line
  4. A to append
  5. Type your text
  6. Esc โ€” text appears on all lines

Delete a Column#

  1. Ctrl-v to enter Block mode
  2. Move to select a column
  3. d to delete

Replace a Column#

  1. Ctrl-v to enter Block mode
  2. Select the column
  3. c to change
  4. Type replacement
  5. Esc โ€” replacement appears on all lines

Practice Exercises#

Exercise 1: Character Selection#

  1. Open a file with text
  2. Press v to enter Visual mode
  3. Use w to select words, $ to select to end of line
  4. Press d to delete the selection

Exercise 2: Line Selection#

  1. Press V to enter Line Visual mode
  2. Press j several times to select multiple lines
  3. Press > to indent them
  4. Press gv to reselect
  5. Press < to unindent

Exercise 3: Block Selection#

  1. Create several lines with similar structure
  2. Press Ctrl-v to enter Block mode
  3. Move to create a rectangular selection
  4. Press d to delete the block
  5. Undo with u

Exercise 4: Multi-line Insert#

  1. Create several lines
  2. Ctrl-v, then j several times
  3. Press I, type # , press Esc
  4. All lines now start with #

Exercise 5: Text Objects in Visual#

  1. Position cursor inside a quoted string
  2. Press v then i" to select inside quotes
  3. Press y to yank
  4. Move elsewhere and p to paste

Visual Mode vs. Operators + Motions#

Visual mode and Normal mode operators can often achieve the same result:

Visual ModeNormal ModeResult
viw then ddiwDelete inner word
V then dddDelete line
v$ then yy$Yank to end of line

When to use Visual mode:

When to use operators + motions:

Summary#

ModeCommandUse Case
CharactervSelect parts of lines
LineVSelect whole lines
BlockCtrl-vColumn editing
TechniqueCommand
Reselect lastgv
Switch endso
ExitEsc

Visual mode is your โ€œpreview before commitโ€ tool. Use it when you want to see exactly what will be affected before taking action.