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:
- Character-wise visual mode: Press
v
- Line-wise visual mode: Press
V
, i.e.shift-v
- 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 linew
,b
: Extend by word{
,}
: Extend by paragraph0
,$
: Extend to beginning or end of linegg
,G
: Extend to beginning or end of file
Basic Operations
With text selected, you can perform various operations:
d
: Delete selected texty
: Yank (copy) selected textc
: Change (delete and enter insert mode)>
,<
: Indent or outdentu
,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 wordvap
: Select a paragraphvi"
: Select text inside quotesva(
: Select text including parentheses
Marks and Jumps
Use marks to quickly select large portions of text:
- Set a mark with
ma
at the start point - Move to the end point
- 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
Commenting Multiple Lines
Ctrl-v Select lines I// <Esc>
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)
Indenting a Code Block
V (enter line-wise visual mode) Select lines > (indent)
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
- Use
o
in visual mode to toggle the cursor to the other end of the selection Ctrl-v
followed by$A
allows you to append to multiple lines- In visual mode,
u
andU
for lowercase and uppercase are quick alternatives to complex substitutions - 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!