blob: a8c8ce67228d680cbb76106342b918f10bc02f82 [file] [log] [blame]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +02001*visual.txt* For Vim version 7.3. Last change: 2012 Jul 25
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Visual mode *Visual* *Visual-mode* *visual-mode*
8
9Visual mode is a flexible and easy way to select a piece of text for an
10operator. It is the only way to select a block of text.
11
12This is introduced in section |04.4| of the user manual.
13
141. Using Visual mode |visual-use|
152. Starting and stopping Visual mode |visual-start|
163. Changing the Visual area |visual-change|
174. Operating on the Visual area |visual-operators|
185. Blockwise operators |blockwise-operators|
196. Repeating |visual-repeat|
207. Examples |visual-examples|
218. Select mode |Select-mode|
22
23{Vi has no Visual mode, the name "visual" is used for Normal mode, to
24distinguish it from Ex mode}
25{not available when the |+visual| feature was disabled when compiling}
26
27==============================================================================
281. Using Visual mode *visual-use*
29
30Using Visual mode consists of three parts:
311. Mark the start of the text with "v", "V" or CTRL-V.
32 The character under the cursor will be used as the start.
332. Move to the end of the text.
34 The text from the start of the Visual mode up to and including the
35 character under the cursor is highlighted.
363. Type an operator command.
37 The highlighted characters will be operated upon.
38
39The 'highlight' option can be used to set the display mode to use for
40highlighting in Visual mode.
41The 'virtualedit' option can be used to allow positioning the cursor to
42positions where there is no actual character.
43
44The highlighted text normally includes the character under the cursor.
45However, when the 'selection' option is set to "exclusive" and the cursor is
46after the Visual area, the character under the cursor is not included.
47
48With "v" the text before the start position and after the end position will
Bram Moolenaar81695252004-12-29 20:58:21 +000049not be highlighted. However, all uppercase and non-alpha operators, except
Bram Moolenaar071d4272004-06-13 20:20:40 +000050"~" and "U", will work on whole lines anyway. See the list of operators
51below.
52
53 *visual-block*
54With CTRL-V (blockwise Visual mode) the highlighted text will be a rectangle
55between start position and the cursor. However, some operators work on whole
56lines anyway (see the list below). The change and substitute operators will
57delete the highlighted text and then start insertion at the top left
58position.
59
60==============================================================================
612. Starting and stopping Visual mode *visual-start*
62
63 *v* *characterwise-visual*
Bram Moolenaarc8734422012-06-01 22:38:45 +020064[count]v Start Visual mode per character.
65 With [count] select that many characters, like moving
66 the cursor right [count] characters. One less when
67 'selection' is not "exclusive".
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69 *V* *linewise-visual*
Bram Moolenaarc8734422012-06-01 22:38:45 +020070[count]V Start Visual mode linewise.
71 With [count] select that many lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000072
73 *CTRL-V* *blockwise-visual*
Bram Moolenaarc8734422012-06-01 22:38:45 +020074[count]CTRL-V Start Visual mode blockwise. Note: Under Windows
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 CTRL-V could be mapped to paste text, it doesn't work
76 to start Visual mode then, see |CTRL-V-alternative|.
Bram Moolenaarc8734422012-06-01 22:38:45 +020077 [count] is used as with `v` above.
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79If you use <Esc>, click the left mouse button or use any command that
80does a jump to another buffer while in Visual mode, the highlighting stops
81and no text is affected. Also when you hit "v" in characterwise Visual mode,
82"CTRL-V" in blockwise Visual mode or "V" in linewise Visual mode. If you hit
83CTRL-Z the highlighting stops and the editor is suspended or a new shell is
84started |CTRL-Z|.
85
86 new mode after typing: *v_v* *v_CTRL-V* *v_V*
87old mode "v" "CTRL-V" "V" ~
88
89Normal Visual blockwise Visual linewise Visual
90Visual Normal blockwise Visual linewise Visual
91blockwise Visual Visual Normal linewise Visual
92linewise Visual Visual blockwise Visual Normal
93
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000094 *gv* *v_gv* *reselect-Visual*
Bram Moolenaar071d4272004-06-13 20:20:40 +000095gv Start Visual mode with the same area as the previous
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000096 area and the same mode.
97 In Visual mode the current and the previous Visual
98 area are exchanged.
99 After using "p" or "P" in Visual mode the text that
100 was put will be selected.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar641e2862012-07-25 15:06:34 +0200102 *gn* *v_gn*
103gn Search forward for the last used search pattern, like
104 with `n`, and start Visual mode to select the match.
105 If the cursor is on the match, visually selects it.
106 If an operator is pending, operates on the match.
107 E.g., "dgn" deletes the text of the next match.
108 If Visual mode is active, extends the selection
109 until the end of the next match.
110
111 *gN* *v_gN*
112gN Like |gn| but searches backward, like with `N`.
113
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 *<LeftMouse>*
115<LeftMouse> Set the current cursor position. If Visual mode is
116 active it is stopped. Only when 'mouse' option is
117 contains 'n' or 'a'. If the position is within 'so'
118 lines from the last line on the screen the text is
119 scrolled up. If the position is within 'so' lines from
120 the first line on the screen the text is scrolled
121 down.
122
123 *<RightMouse>*
124<RightMouse> Start Visual mode if it is not active. The text from
125 the cursor position to the position of the click is
126 highlighted. If Visual mode was already active move
127 the start or end of the highlighted text, which ever
128 is closest, to the position of the click. Only when
129 'mouse' option contains 'n' or 'a'.
130
131 Note: when 'mousemodel' is set to "popup",
132 <S-LeftMouse> has to be used instead of <RightMouse>.
133
134 *<LeftRelease>*
135<LeftRelease> This works like a <LeftMouse>, if it is not at
136 the same position as <LeftMouse>. In an older version
137 of xterm you won't see the selected area until the
138 button is released, unless there is access to the
139 display where the xterm is running (via the DISPLAY
140 environment variable or the -display argument). Only
141 when 'mouse' option contains 'n' or 'a'.
142
143If Visual mode is not active and the "v", "V" or CTRL-V is preceded with a
144count, the size of the previously highlighted area is used for a start. You
145can then move the end of the highlighted area and give an operator. The type
146of the old area is used (character, line or blockwise).
147- Linewise Visual mode: The number of lines is multiplied with the count.
148- Blockwise Visual mode: The number of lines and columns is multiplied with
149 the count.
150- Normal Visual mode within one line: The number of characters is multiplied
151 with the count.
152- Normal Visual mode with several lines: The number of lines is multiplied
153 with the count, in the last line the same number of characters is used as
154 in the last line in the previously highlighted area.
155The start of the text is the Cursor position. If the "$" command was used as
156one of the last commands to extend the highlighted text, the area will be
157extended to the rightmost column of the longest line.
158
159If you want to highlight exactly the same area as the last time, you can use
160"gv" |gv| |v_gv|.
161
Bram Moolenaar81695252004-12-29 20:58:21 +0000162 *v_<Esc>*
163<Esc> In Visual mode: Stop Visual mode.
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 *v_CTRL-C*
166CTRL-C In Visual mode: Stop Visual mode. When insert mode is
167 pending (the mode message shows
168 "-- (insert) VISUAL --"), it is also stopped.
169
170==============================================================================
1713. Changing the Visual area *visual-change*
172
173 *v_o*
174o Go to Other end of highlighted text: The current
175 cursor position becomes the start of the highlighted
176 text and the cursor is moved to the other end of the
177 highlighted text. The highlighted area remains the
178 same.
179
180 *v_O*
181O Go to Other end of highlighted text. This is like
182 "o", but in Visual block mode the cursor moves to the
183 other corner in the same line. When the corner is at
184 a character that occupies more than one position on
185 the screen (e.g., a <Tab>), the highlighted text may
186 change.
187
188 *v_$*
189When the "$" command is used with blockwise Visual mode, the right end of the
190highlighted text will be determined by the longest highlighted line. This
191stops when a motion command is used that does not move straight up or down.
192
193For moving the end of the block many commands can be used, but you cannot
194use Ex commands, commands that make changes or abandon the file. Commands
Bram Moolenaar9964e462007-05-05 17:54:07 +0000195(starting with) ".", "&", CTRL-^, "Z", CTRL-], CTRL-T, CTRL-R, CTRL-I
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196and CTRL-O cause a beep and Visual mode continues.
197
198When switching to another window on the same buffer, the cursor position in
199that window is adjusted, so that the same Visual area is still selected. This
200is especially useful to view the start of the Visual area in one window, and
201the end in another. You can then use <RightMouse> (or <S-LeftMouse> when
Bram Moolenaar81695252004-12-29 20:58:21 +0000202'mousemodel' is "popup") to drag either end of the Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204==============================================================================
2054. Operating on the Visual area *visual-operators*
206
207The operators that can be used are:
208 ~ switch case |v_~|
209 d delete |v_d|
210 c change (4) |v_c|
211 y yank |v_y|
212 > shift right (4) |v_>|
213 < shift left (4) |v_<|
214 ! filter through external command (1) |v_!|
215 = filter through 'equalprg' option command (1) |v_=|
216 gq format lines to 'textwidth' length (1) |v_gq|
217
218The objects that can be used are:
219 aw a word (with white space) |v_aw|
220 iw inner word |v_iw|
221 aW a WORD (with white space) |v_aW|
222 iW inner WORD |v_iW|
223 as a sentence (with white space) |v_as|
224 is inner sentence |v_is|
225 ap a paragraph (with white space) |v_ap|
226 ip inner paragraph |v_ip|
227 ab a () block (with parenthesis) |v_ab|
228 ib inner () block |v_ib|
229 aB a {} block (with braces) |v_aB|
230 iB inner {} block |v_iB|
Bram Moolenaar5e3dae82010-03-02 16:19:40 +0100231 at a <tag> </tag> block (with tags) |v_at|
232 it inner <tag> </tag> block |v_it|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 a< a <> block (with <>) |v_a<|
234 i< inner <> block |v_i<|
235 a[ a [] block (with []) |v_a[|
236 i[ inner [] block |v_i[|
Bram Moolenaar5e3dae82010-03-02 16:19:40 +0100237 a" a double quoted string (with quotes) |v_aquote|
238 i" inner double quoted string |v_iquote|
239 a' a single quoted string (with quotes) |v_a'|
240 i' inner simple quoted string |v_i'|
241 a` a string in backticks (with backticks) |v_a`|
242 i` inner string in backticks |v_i`|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
244Additionally the following commands can be used:
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100245 : start Ex command for highlighted lines (1) |v_:|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 r change (4) |v_r|
247 s change |v_s|
248 C change (2)(4) |v_C|
249 S change (2) |v_S|
250 R change (2) |v_R|
251 x delete |v_x|
252 D delete (3) |v_D|
253 X delete (2) |v_X|
254 Y yank (2) |v_Y|
255 p put |v_p|
256 J join (1) |v_J|
257 U make uppercase |v_U|
258 u make lowercase |v_u|
259 ^] find tag |v_CTRL-]|
260 I block insert |v_b_I|
261 A block append |v_b_A|
262
263(1): Always whole lines, see |:visual_example|.
264(2): Whole lines when not using CTRL-V.
265(3): Whole lines when not using CTRL-V, delete until the end of the line when
266 using CTRL-V.
267(4): When using CTRL-V operates on the block only.
268
269Note that the ":vmap" command can be used to specifically map keys in Visual
270mode. For example, if you would like the "/" command not to extend the Visual
271area, but instead take the highlighted text and search for that: >
272 :vmap / y/<C-R>"<CR>
273(In the <> notation |<>|, when typing it you should type it literally; you
274need to remove the 'B' and '<' flags from 'cpoptions'.)
275
276If you want to give a register name using the """ command, do this just before
277typing the operator character: "v{move-around}"xd".
278
279If you want to give a count to the command, do this just before typing the
280operator character: "v{move-around}3>" (move lines 3 indents to the right).
281
282 *{move-around}*
283The {move-around} is any sequence of movement commands. Note the difference
284with {motion}, which is only ONE movement command.
285
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000286Another way to operate on the Visual area is using the |/\%V| item in a
287pattern. For example, to replace all '(' in the Visual area with '#': >
288
Bram Moolenaar30b65812012-07-12 22:01:11 +0200289 :'<,'>s/\%V(/#/g
290
291Note that the "'<,'>" will appear automatically when you press ":" in Visual
292mode.
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294==============================================================================
2955. Blockwise operators *blockwise-operators*
296
297{not available when compiled without the |+visualextra| feature}
298
299Reminder: Use 'virtualedit' to be able to select blocks that start or end
300after the end of a line or halfway a tab.
301
302Visual-block Insert *v_b_I*
303With a blockwise selection, I{string}<ESC> will insert {string} at the start
304of block on every line of the block, provided that the line extends into the
305block. Thus lines that are short will remain unmodified. TABs are split to
306retain visual columns.
307See |v_b_I_example|.
308
309Visual-block Append *v_b_A*
310With a blockwise selection, A{string}<ESC> will append {string} to the end of
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000311block on every line of the block. There is some differing behavior where the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312block RHS is not straight, due to different line lengths:
313
3141. Block was created with <C-v>$
315 In this case the string is appended to the end of each line.
3162. Block was created with <C-v>{move-around}
317 In this case the string is appended to the end of the block on each line,
318 and whitespace is inserted to pad to the end-of-block column.
319See |v_b_A_example|.
320Note: "I" and "A" behave differently for lines that don't extend into the
321selected block. This was done intentionally, so that you can do it the way
322you want.
323
324Visual-block change *v_b_c*
325All selected text in the block will be replaced by the same text string. When
326using "c" the selected text is deleted and Insert mode started. You can then
327enter text (without a line break). When you hit <Esc>, the same string is
328inserted in all previously selected lines.
329
330Visual-block Change *v_b_C*
331Like using "c", but the selection is extended until the end of the line for
332all lines.
333
334 *v_b_<*
335Visual-block Shift *v_b_>*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000336The block is shifted by 'shiftwidth'. The RHS of the block is irrelevant. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337LHS of the block determines the point from which to apply a right shift, and
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000338padding includes TABs optimally according to 'ts' and 'et'. The LHS of the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339block determines the point upto which to shift left.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340See |v_b_>_example|.
341See |v_b_<_example|.
342
343Visual-block Replace *v_b_r*
344Every screen char in the highlighted region is replaced with the same char, ie
345TABs are split and the virtual whitespace is replaced, maintaining screen
346layout.
347See |v_b_r_example|.
348
349
350==============================================================================
3516. Repeating *visual-repeat*
352
353When repeating a Visual mode operator, the operator will be applied to the
354same amount of text as the last time:
355- Linewise Visual mode: The same number of lines.
356- Blockwise Visual mode: The same number of lines and columns.
357- Normal Visual mode within one line: The same number of characters.
358- Normal Visual mode with several lines: The same number of lines, in the
359 last line the same number of characters as in the last line the last time.
360The start of the text is the Cursor position. If the "$" command was used as
361one of the last commands to extend the highlighted text, the repeating will
362be applied up to the rightmost column of the longest line.
363
364
365==============================================================================
3667. Examples *visual-examples*
367
368 *:visual_example*
369Currently the ":" command works on whole lines only. When you select part of
370a line, doing something like ":!date" will replace the whole line. If you
371want only part of the line to be replaced you will have to make a mapping for
372it. In a future release ":" may work on partial lines.
373
374Here is an example, to replace the selected text with the output of "date": >
375 :vmap _a <Esc>`>a<CR><Esc>`<i<CR><Esc>!!date<CR>kJJ
376
377(In the <> notation |<>|, when typing it you should type it literally; you
378need to remove the 'B' and '<' flags from 'cpoptions')
379
380What this does is:
381<Esc> stop Visual mode
382`> go to the end of the Visual area
383a<CR><Esc> break the line after the Visual area
384`< jump to the start of the Visual area
385i<CR><Esc> break the line before the Visual area
386!!date<CR> filter the Visual text through date
387kJJ Join the lines back together
388
389 *visual-search*
390Here is an idea for a mapping that makes it possible to do a search for the
391selected text: >
392 :vmap X y/<C-R>"<CR>
393
394(In the <> notation |<>|, when typing it you should type it literally; you
395need to remove the 'B' and '<' flags from 'cpoptions')
396
397Note that special characters (like '.' and '*') will cause problems.
398
399Visual-block Examples *blockwise-examples*
400With the following text, I will indicate the commands to produce the block and
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000401the results below. In all cases, the cursor begins on the 'a' in the first
Bram Moolenaar81695252004-12-29 20:58:21 +0000402line of the test text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403The following modeline settings are assumed ":ts=8:sw=4:".
404
405It will be helpful to
406:set hls
407/<TAB>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408where <TAB> is a real TAB. This helps visualise the operations.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
410The test text is:
411
412abcdefghijklmnopqrstuvwxyz
413abc defghijklmnopqrstuvwxyz
414abcdef ghi jklmnopqrstuvwxyz
415abcdefghijklmnopqrstuvwxyz
416
4171. fo<C-v>3jISTRING<ESC> *v_b_I_example*
418
419abcdefghijklmnSTRINGopqrstuvwxyz
420abc STRING defghijklmnopqrstuvwxyz
421abcdef ghi STRING jklmnopqrstuvwxyz
422abcdefghijklmnSTRINGopqrstuvwxyz
423
4242. fo<C-v>3j$ASTRING<ESC> *v_b_A_example*
425
426abcdefghijklmnopqrstuvwxyzSTRING
427abc defghijklmnopqrstuvwxyzSTRING
428abcdef ghi jklmnopqrstuvwxyzSTRING
429abcdefghijklmnopqrstuvwxyzSTRING
430
4313. fo<C-v>3j3l<.. *v_b_<_example*
432
433abcdefghijklmnopqrstuvwxyz
434abc defghijklmnopqrstuvwxyz
435abcdef ghi jklmnopqrstuvwxyz
436abcdefghijklmnopqrstuvwxyz
437
4384. fo<C-v>3j>.. *v_b_>_example*
439
440abcdefghijklmn opqrstuvwxyz
441abc defghijklmnopqrstuvwxyz
442abcdef ghi jklmnopqrstuvwxyz
443abcdefghijklmn opqrstuvwxyz
444
4455. fo<C-v>5l3jrX *v_b_r_example*
446
447abcdefghijklmnXXXXXXuvwxyz
448abc XXXXXXhijklmnopqrstuvwxyz
449abcdef ghi XXXXXX jklmnopqrstuvwxyz
450abcdefghijklmnXXXXXXuvwxyz
451
452==============================================================================
4538. Select mode *Select* *Select-mode*
454
455Select mode looks like Visual mode, but the commands accepted are quite
456different. This resembles the selection mode in Microsoft Windows.
457When the 'showmode' option is set, "-- SELECT --" is shown in the last line.
458
459Entering Select mode:
460- Using the mouse to select an area, and 'selectmode' contains "mouse".
461 'mouse' must also contain a flag for the current mode.
462- Using a non-printable movement command, with the Shift key pressed, and
463 'selectmode' contains "key". For example: <S-Left> and <S-End>. 'keymodel'
464 must also contain "startsel".
465- Using "v", "V" or CTRL-V command, and 'selectmode' contains "cmd".
466- Using "gh", "gH" or "g_CTRL-H" command in Normal mode.
467- From Visual mode, press CTRL-G. *v_CTRL-G*
468
469Commands in Select mode:
470- Printable characters, <NL> and <CR> cause the selection to be deleted, and
471 Vim enters Insert mode. The typed character is inserted.
472- Non-printable movement commands, with the Shift key pressed, extend the
473 selection. 'keymodel' must include "startsel".
474- Non-printable movement commands, with the Shift key NOT pressed, stop Select
475 mode. 'keymodel' must include "stopsel".
476- ESC stops Select mode.
477- CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O*
478- CTRL-G switches to Visual mode.
479
480Otherwise, typed characters are handled as in Visual mode.
481
482When using an operator in Select mode, and the selection is linewise, the
483selected lines are operated upon, but like in characterwise selection. For
484example, when a whole line is deleted, it can later be pasted halfway a line.
485
486
487Mappings and menus in Select mode. *Select-mode-mapping*
488
Bram Moolenaar371d5402006-03-20 21:47:49 +0000489When mappings and menus are defined with the |:vmap| or |:vmenu| command they
490work both in Visual mode and in Select mode. When these are used in Select
491mode Vim automatically switches to Visual mode, so that the same behavior as
492in Visual mode is effective. If you don't want this use |:xmap| or |:smap|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100494Users will expect printable characters to replace the selected area.
495Therefore avoid mapping printable characters in Select mode. Or use
496|:sunmap| after |:map| and |:vmap| to remove it for Select mode.
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498After the mapping or menu finishes, the selection is enabled again and Select
499mode entered, unless the selected area was deleted, another buffer became
500the current one or the window layout was changed.
501
502When a character was typed that causes the selection to be deleted and Insert
503mode started, Insert mode mappings are applied to this character. This may
504cause some confusion, because it means Insert mode mappings apply to a
505character typed in Select mode. Language mappings apply as well.
506
507 *gV* *v_gV*
508gV Avoid the automatic reselection of the Visual area
509 after a Select mode mapping or menu has finished.
510 Put this just before the end of the mapping or menu.
511 At least it should be after any operations on the
512 selection.
513
514 *gh*
515gh Start Select mode, characterwise. This is like "v",
516 but starts Select mode instead of Visual mode.
517 Mnemonic: "get highlighted".
518
519 *gH*
520gH Start Select mode, linewise. This is like "V",
521 but starts Select mode instead of Visual mode.
522 Mnemonic: "get Highlighted".
523
524 *g_CTRL-H*
525g CTRL-H Start Select mode, blockwise. This is like CTRL-V,
526 but starts Select mode instead of Visual mode.
527 Mnemonic: "get Highlighted".
528
529 vim:tw=78:ts=8:ft=help:norl: