Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 36 ยท Modes and Navigation

Vim Visual Mode: Character, Line and Block Selection

Select text with v, V and Ctrl-v, extend selections with motions and text objects, swap ends with o, edit many lines with block insert, and reselect with gv.

Operators plus motions are fast when you know the target in advance. Visual mode is the other approach: you select first, then act. You see exactly what will change before you change it, which is helpful when learning and still useful for experts on irregular regions. Visual mode also unlocks one special trick, the rectangular block, that few other editors do as elegantly.

Three kinds of selection

Key Selects Typical use
v characters part of a line, a few words
V whole lines move, indent, delete line blocks
Ctrl-v a rectangular block edit a column, many lines at once

After starting, move the cursor with any motion to grow the selection: w, $, }, G, /text<Enter>, %. Press Esc to cancel. Then press an operator to act on it: d delete, c change, y yank, > indent, u/U lowercase/uppercase, ~ toggle case, J join lines, = re-indent, : start an ex command on the selected lines (Vim fills in :'<,'>).

On Windows, Ctrl-v may paste in some terminals; use Ctrl-q there. In Neovim it is Ctrl-v unless your terminal intercepts it.

Extending with text objects

In Visual mode, text objects extend the selection: viw selects a word, vip a paragraph, vi" inside quotes, va( a parenthesized group including the parentheses. Repeating one grows outward: vi( then i( again selects the next enclosing pair. This is the "expanding selection" workflow, similar to modern editors' smart select. Chapter 3's text objects lesson lists all of them.

Useful Visual-mode keys

  • o jumps to the other end of the selection so you can extend from there. O does the same horizontally in a block.
  • gv reselects the last selection after you leave Visual mode.
  • v, V, Ctrl-v switch kinds while selecting (press V inside v to widen to lines).
  • aw, ip, etc. extend or begin a selection.
  • r{char} replaces every selected character with char.
  • p pastes over the selection, replacing it (the replaced text goes to the unnamed register unless you use the black hole; see the registers lesson).

Visual line mode in practice

Select several lines with Vjjj, then:

  • > to indent them (. repeats it).
  • : then sort to sort them.
  • J to join into one line.
  • gq to reformat paragraphs to textwidth.

To move lines, V + motion, d, jump somewhere, p. For a quicker reorder, :m or ddp (swap with next line) are options; plugins add Alt-j mappings.

Block mode: edit columns

Ctrl-v selects a rectangle. This makes column edits trivial:

  1. Insert at the start of many lines. Select a column with Ctrl-v and j down. Press I, type text, then Esc. The text appears on every selected line. For example, comment out lines with Ctrl-v, jjj, I, # , Esc.
  2. Append to the end of lines. Ctrl-v, move down, $ (to the true end of each line), A, type, Esc.
  3. Change a column. Select, c, type the new text, Esc; every row changes.
  4. Delete a column. Select and d.
  5. Increase numbers in a column. g Ctrl-a after selecting a list of identical numbers increments each successive line (1, 2, 3, ...). Plain Ctrl-a adds to all.

A common job is aligning or commenting code; a column of identical edits is one block operation instead of editing line by line.

The block model (stand-in)

This stand-in applies a block insert and a block delete to an array of lines. It mirrors what Ctrl-v then I and d do: operate on one rectangle only, even though rows have different lengths.

JS Block insert and delete (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first call is the "comment out three lines" recipe: column 0, rows 0 to 2. The second removes the second column from three rows.

Selecting with search and marks

Because v accepts any motion, you can select up to a search result: v/end<Enter> selects until the next match. With marks, `a before v to `b selects between two positions. After a Visual operation, the '< and '> marks remember the region, and gv returns to it: that is how you repeat or tweak a selection.

You can also feed a selection to external programs: :'<,'>!sort pipes the selected lines through sort and replaces them with the output. The ex command line you see (:'<,'>) is just a line range.

Block selection and the clipboard

Pasting a blockwise yank pastes a block, columns aligned, which works for table data. Yank a block with y and put it with p somewhere with enough lines; the block keeps its shape.

A second stand-in shows how the '<,'> range works on lines:

JS Selected line range (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key starts a rectangular block selection?

    Ctrl-v starts block mode (Ctrl-q on terminals where Ctrl-v pastes).
  2. 2. What does gv do?

    gv restores the last selection even after you pressed Esc.
  3. 3. How do you insert text at the start of every selected line in block mode?

    In block mode I inserts at the left edge of the block, and the text is repeated on every line after Esc.
  4. 4. What does o do in Visual mode?

    o moves the cursor to the opposite corner so you can extend the other side.

Summary

  • v, V and Ctrl-v select characters, lines and rectangles; act with d c y > ~ J :.
  • Extend selections with any motion or text object; o swaps ends and gv reselects.
  • Block mode I and A edit many lines at once; $ then A appends at each line's true end.
  • : after a selection runs ex commands on '<,'>, for example sort or !command.
  • Use Visual mode when you want to see the target before acting.

Next: Operators and Motions in Depth