Vim’s visual mode is a powerful feature that allows you to select and manipulate text visually. This mode bridges the gap between Vim’s command-line efficiency and the intuitive nature of graphical text editors. In this article, we’ll explore the ins and outs of visual mode, providing you with the knowledge to leverage its full potential.

Basic Visual Mode Operations

Entering Visual Mode

There are three types of visual mode in Vim:

  1. Character-wise visual mode: Press v
  2. Line-wise visual mode: Press V, i.e. shift-v
  3. Block-wise visual mode: Press Ctrl-v

Making Selections

Once in visual mode, you can use normal Vim motions to expand your selection:

  • h, j, k, l: Extend selection by character or line
  • w, b: Extend by word
  • {, }: Extend by paragraph
  • 0, $: Extend to beginning or end of line
  • gg, G: Extend to beginning or end of file

Basic Operations

With text selected, you can perform various operations:

  • d: Delete selected text
  • y: Yank (copy) selected text
  • c: Change (delete and enter insert mode)
  • >, <: Indent or outdent
  • u, U: Change to lowercase or uppercase

Advanced Visual Mode Techniques

Text Objects

Visual mode becomes even more powerful when combined with text objects:

  • viw: Select inner word
  • vap: Select a paragraph
  • vi": Select text inside quotes
  • va(: Select text including parentheses

Marks and Jumps

Use marks to quickly select large portions of text:

  1. Set a mark with ma at the start point
  2. Move to the end point
  3. Enter visual mode and select to mark with v`a

Block-wise Visual Mode

Block-wise visual mode (Ctrl-v) is particularly useful for working with columnar data:

  • Select a block and use I to insert at the beginning of each line
  • Use $ to extend selection to the end of each line, regardless of line length
  • c to change the selected block, d to delete it

Repeating Visual Selections

  • gv: Reselect the last visual selection
  • .: Repeat the last visual mode command on the current line

Practical Examples

  1. Commenting Multiple Lines

    Ctrl-v
    Select lines
    I// <Esc>
    
  2. Changing Variable Names

    *         (to find next occurrence)
    viw       (visually select word)
    c         (change)
    NewName   (type new name)
    <Esc>     (exit insert mode)
    n         (find next occurrence)
    .         (repeat change)
    
  3. Indenting a Code Block

    V         (enter line-wise visual mode)
    Select lines
    >         (indent)
    
  4. Selecting Inside Parentheses

    vi(       (visually select inside parentheses)
    

Visual Mode with Ex Commands

You can apply Ex commands to visually selected text:

  • : in visual mode applies the command to the selected range
  • For example, :s/foo/bar/g on a visual selection replaces all occurrences of “foo” with “bar” in the selected text

Tips and Tricks

  1. Use o in visual mode to toggle the cursor to the other end of the selection
  2. Ctrl-v followed by $A allows you to append to multiple lines
  3. In visual mode, u and U for lowercase and uppercase are quick alternatives to complex substitutions
  4. Use visual mode with macros: record actions on one line, visually select multiple lines, then :'<,'>normal @q to apply the macro

Customizing Visual Mode

You can enhance visual mode with custom mappings:

" Quickly re-select pasted text
nnoremap gp `[v`]

" Search for visually selected text
vnoremap // y/\V<C-R>=escape(@",'/\')<CR><CR>

Conclusion

Mastering Vim’s visual mode can significantly enhance your text editing workflow. It combines the precision of Vim’s command language with the intuitive nature of visual selection, offering a powerful tool for text manipulation. As with all things in Vim, the key to mastery is practice. Incorporate these techniques into your daily editing, and you’ll soon find yourself effortlessly performing complex text operations.

Remember to explore Vim’s built-in help (:help visual-mode) for even more details and options. Happy vimming!