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