Visual mode
Visual mode allows you to perform editing tasks on selected portions of text. There are various visual commands to select the text of interest. If enabled, you can also use your mouse to select the desired portions.
Documentation links:
- :h 4.4 — visual mode
- :h 10.5 — visual block mode
- :h visual.txt — reference manual for visual mode
Recall that you need to add
v_
prefix for built-in help on Visual mode commands, for example :h v_o.
Selection
- v visually select the current character, use any motion command to extend the selection
- ve selects till the end of a word
- vip selects a paragraph (text object) and so on
- V visually select the current line, you can extend this using motion commands
- Vgg selects the current line and then extends to the start of the file
- Ctrl+v visually select columns
- Ctrl+v followed by 3j2l selects a 4x3 block
- you can also select using the mouse and then press Ctrl+v
- gv select previously highlighted visual area
- o move the cursor to the diagonally opposite corner of the visual selection
- O move the cursor to the other corner of the current line in visual block selection
- Esc go back to Normal mode
Pressing $ in block selection will select until the end of lines for the selected area, even if the lines have different number of characters. This will continue to be the case if you extend the selection with up/down motions.
Editing
- d delete the selected text
- y yank (copy) the selected text
- p replace the selected text with contents of the default
"
register- See :h v_p for more details, corner cases, uppercase P command behavior, etc
- c clear the selected text and change to Insert mode
- for block selection, you can type some text and press the Esc key to replicate that text across all the visually selected lines
- C similar to c but clears till the end of lines before changing to Insert mode
- I for block selection, press I, type some text and press the Esc key to replicate that text across all lines to the left of the column
- text will not be inserted for lines shorter than the starting column of selection
- if you type a multiline text, it will not get replicated
- A for block selection, press A, type some text and press the Esc key to replicate that text across all lines to the right of the column
- if $ was used for selection, text will be inserted only after the end of respective lines
- otherwise, text will be inserted after the selected column and space characters will used to extend shorter lines if any
- if you type a multiline text, it will not get replicated
- ra replace every character of the selected text with
a
- : perform Command-line mode editing commands like
g
,s
,!
,normal
, etc on the selected text - J and gJ join lines using the same rules as seen in Normal mode
Press Ctrl+c if you've typed text after using I or A but don't want to replicate the text across all the lines.
See :h visual-operators for a complete list of commands.
Search and Select
- gn search the last used pattern in the forward direction and visually select the matched portion
- selects the current match if the cursor is anywhere within the matching portion
- extends the visual selection if Visual mode is already active
- gN search the last used pattern in the backward direction and visually select the matched portion
- cgn here gn acts as the motion for the Normal mode command
c
- since this is considered as a single change, pressing . will change the next match in the forward direction
- whereas, if you apply
c
with Normal mode motion, you'll have to first use n (or N depending on the direction) and then use the . command to repeat the change
Searching will automatically wrap around when it reaches the top or bottom of the file contents, unless you set the
nowrapscan
option.
Indenting
- > indent the visual selection once
- 3> indent the visual selection three times
- < unindent the visual selection once
- = auto indent code
Consider the following unindented code:
for(i=1; i<5; i++)
{
for(j=i; j<10; j++)
{
statements
}
statements
}
Here's the result after applying vip= (you can also use =ip if you prefer Normal mode).
for(i=1; i<5; i++)
{
for(j=i; j<10; j++)
{
statements
}
statements
}
For block selection, space will be inserted before the starting column of the block.
Indentation depends on the
shiftwidth
setting. See :h shift-left-right, :h = and :h 'shiftwidth' for more details.
Changing Case
- ~ invert the case of the visually selected text (i.e. lowercase becomes UPPERCASE and vice versa)
- U change the visually selected text to UPPERCASE
- u change the visually selected text to lowercase
Increment and Decrement numbers
- Ctrl+a increment by
1
- 5 followed by Ctrl+a increment by
5
- Ctrl+x decrement by
1
- g followed by Ctrl+a increment by
1
for the first line, by2
for the second line, by3
for the third line and so on - 2g followed by Ctrl+a increment by
2
for the first line, by4
for the second line, by6
for the third line and so on (i.e. repeat the process specified by the count prefix) - g followed by Ctrl+x decrement by
1
for the first line, by2
for the second line, by3
for the third line and so on
The visual selection should cover the numeric portion you wish to increment or decrement. If there are multiple numbers in a visually selected line, only the first number will be affected.
Example for g followed by Ctrl+a:
# before
item[0]
item[0]
item[0]
# after
item[1]
item[2]
item[3]
Example for g followed by Ctrl+x:
# before
item[12]
item[16]
item[22]
# after
item[11]
item[14]
item[19]
Example for 3g followed by Ctrl+a:
# before
item[12]
item[16]
item[22]
# after
item[15]
item[22]
item[31]