blob: 4100578381238e086c289fd863ebc1ab7531f6df [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001*visual.txt* For Vim version 7.0aa. Last change: 2004 Jun 08
2
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
49not be highlighted. However, All uppercase and non-alpha operators, except
50"~" 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*
64v start Visual mode per character.
65
66 *V* *linewise-visual*
67V start Visual mode linewise.
68
69 *CTRL-V* *blockwise-visual*
70CTRL-V start Visual mode blockwise. Note: Under Windows
71 CTRL-V could be mapped to paste text, it doesn't work
72 to start Visual mode then, see |CTRL-V-alternative|.
73
74If you use <Esc>, click the left mouse button or use any command that
75does a jump to another buffer while in Visual mode, the highlighting stops
76and no text is affected. Also when you hit "v" in characterwise Visual mode,
77"CTRL-V" in blockwise Visual mode or "V" in linewise Visual mode. If you hit
78CTRL-Z the highlighting stops and the editor is suspended or a new shell is
79started |CTRL-Z|.
80
81 new mode after typing: *v_v* *v_CTRL-V* *v_V*
82old mode "v" "CTRL-V" "V" ~
83
84Normal Visual blockwise Visual linewise Visual
85Visual Normal blockwise Visual linewise Visual
86blockwise Visual Visual Normal linewise Visual
87linewise Visual Visual blockwise Visual Normal
88
89 *gv* *v_gv*
90gv Start Visual mode with the same area as the previous
91 area and the same mode. In Visual mode the current and
92 the previous Visual area are exchanged.
93
94 *<LeftMouse>*
95<LeftMouse> Set the current cursor position. If Visual mode is
96 active it is stopped. Only when 'mouse' option is
97 contains 'n' or 'a'. If the position is within 'so'
98 lines from the last line on the screen the text is
99 scrolled up. If the position is within 'so' lines from
100 the first line on the screen the text is scrolled
101 down.
102
103 *<RightMouse>*
104<RightMouse> Start Visual mode if it is not active. The text from
105 the cursor position to the position of the click is
106 highlighted. If Visual mode was already active move
107 the start or end of the highlighted text, which ever
108 is closest, to the position of the click. Only when
109 'mouse' option contains 'n' or 'a'.
110
111 Note: when 'mousemodel' is set to "popup",
112 <S-LeftMouse> has to be used instead of <RightMouse>.
113
114 *<LeftRelease>*
115<LeftRelease> This works like a <LeftMouse>, if it is not at
116 the same position as <LeftMouse>. In an older version
117 of xterm you won't see the selected area until the
118 button is released, unless there is access to the
119 display where the xterm is running (via the DISPLAY
120 environment variable or the -display argument). Only
121 when 'mouse' option contains 'n' or 'a'.
122
123If Visual mode is not active and the "v", "V" or CTRL-V is preceded with a
124count, the size of the previously highlighted area is used for a start. You
125can then move the end of the highlighted area and give an operator. The type
126of the old area is used (character, line or blockwise).
127- Linewise Visual mode: The number of lines is multiplied with the count.
128- Blockwise Visual mode: The number of lines and columns is multiplied with
129 the count.
130- Normal Visual mode within one line: The number of characters is multiplied
131 with the count.
132- Normal Visual mode with several lines: The number of lines is multiplied
133 with the count, in the last line the same number of characters is used as
134 in the last line in the previously highlighted area.
135The start of the text is the Cursor position. If the "$" command was used as
136one of the last commands to extend the highlighted text, the area will be
137extended to the rightmost column of the longest line.
138
139If you want to highlight exactly the same area as the last time, you can use
140"gv" |gv| |v_gv|.
141
142 *v_CTRL-C*
143CTRL-C In Visual mode: Stop Visual mode. When insert mode is
144 pending (the mode message shows
145 "-- (insert) VISUAL --"), it is also stopped.
146
147==============================================================================
1483. Changing the Visual area *visual-change*
149
150 *v_o*
151o Go to Other end of highlighted text: The current
152 cursor position becomes the start of the highlighted
153 text and the cursor is moved to the other end of the
154 highlighted text. The highlighted area remains the
155 same.
156
157 *v_O*
158O Go to Other end of highlighted text. This is like
159 "o", but in Visual block mode the cursor moves to the
160 other corner in the same line. When the corner is at
161 a character that occupies more than one position on
162 the screen (e.g., a <Tab>), the highlighted text may
163 change.
164
165 *v_$*
166When the "$" command is used with blockwise Visual mode, the right end of the
167highlighted text will be determined by the longest highlighted line. This
168stops when a motion command is used that does not move straight up or down.
169
170For moving the end of the block many commands can be used, but you cannot
171use Ex commands, commands that make changes or abandon the file. Commands
172(starting with) ".pPiIaAO&", CTRL-^, "Z", CTRL-], CTRL-T, CTRL-R, CTRL-I
173and CTRL-O cause a beep and Visual mode continues.
174
175When switching to another window on the same buffer, the cursor position in
176that window is adjusted, so that the same Visual area is still selected. This
177is especially useful to view the start of the Visual area in one window, and
178the end in another. You can then use <RightMouse> (or <S-LeftMouse> when
179'mousemodel' is "popup") to move either end of the Visual area.
180
181==============================================================================
1824. Operating on the Visual area *visual-operators*
183
184The operators that can be used are:
185 ~ switch case |v_~|
186 d delete |v_d|
187 c change (4) |v_c|
188 y yank |v_y|
189 > shift right (4) |v_>|
190 < shift left (4) |v_<|
191 ! filter through external command (1) |v_!|
192 = filter through 'equalprg' option command (1) |v_=|
193 gq format lines to 'textwidth' length (1) |v_gq|
194
195The objects that can be used are:
196 aw a word (with white space) |v_aw|
197 iw inner word |v_iw|
198 aW a WORD (with white space) |v_aW|
199 iW inner WORD |v_iW|
200 as a sentence (with white space) |v_as|
201 is inner sentence |v_is|
202 ap a paragraph (with white space) |v_ap|
203 ip inner paragraph |v_ip|
204 ab a () block (with parenthesis) |v_ab|
205 ib inner () block |v_ib|
206 aB a {} block (with braces) |v_aB|
207 iB inner {} block |v_iB|
208 a< a <> block (with <>) |v_a<|
209 i< inner <> block |v_i<|
210 a[ a [] block (with []) |v_a[|
211 i[ inner [] block |v_i[|
212
213Additionally the following commands can be used:
214 : start ex command for highlighted lines (1) |v_:|
215 r change (4) |v_r|
216 s change |v_s|
217 C change (2)(4) |v_C|
218 S change (2) |v_S|
219 R change (2) |v_R|
220 x delete |v_x|
221 D delete (3) |v_D|
222 X delete (2) |v_X|
223 Y yank (2) |v_Y|
224 p put |v_p|
225 J join (1) |v_J|
226 U make uppercase |v_U|
227 u make lowercase |v_u|
228 ^] find tag |v_CTRL-]|
229 I block insert |v_b_I|
230 A block append |v_b_A|
231
232(1): Always whole lines, see |:visual_example|.
233(2): Whole lines when not using CTRL-V.
234(3): Whole lines when not using CTRL-V, delete until the end of the line when
235 using CTRL-V.
236(4): When using CTRL-V operates on the block only.
237
238Note that the ":vmap" command can be used to specifically map keys in Visual
239mode. For example, if you would like the "/" command not to extend the Visual
240area, but instead take the highlighted text and search for that: >
241 :vmap / y/<C-R>"<CR>
242(In the <> notation |<>|, when typing it you should type it literally; you
243need to remove the 'B' and '<' flags from 'cpoptions'.)
244
245If you want to give a register name using the """ command, do this just before
246typing the operator character: "v{move-around}"xd".
247
248If you want to give a count to the command, do this just before typing the
249operator character: "v{move-around}3>" (move lines 3 indents to the right).
250
251 *{move-around}*
252The {move-around} is any sequence of movement commands. Note the difference
253with {motion}, which is only ONE movement command.
254
255==============================================================================
2565. Blockwise operators *blockwise-operators*
257
258{not available when compiled without the |+visualextra| feature}
259
260Reminder: Use 'virtualedit' to be able to select blocks that start or end
261after the end of a line or halfway a tab.
262
263Visual-block Insert *v_b_I*
264With a blockwise selection, I{string}<ESC> will insert {string} at the start
265of block on every line of the block, provided that the line extends into the
266block. Thus lines that are short will remain unmodified. TABs are split to
267retain visual columns.
268See |v_b_I_example|.
269
270Visual-block Append *v_b_A*
271With a blockwise selection, A{string}<ESC> will append {string} to the end of
272block on every line of the block. There is some differing behavior where the
273block RHS is not straight, due to different line lengths:
274
2751. Block was created with <C-v>$
276 In this case the string is appended to the end of each line.
2772. Block was created with <C-v>{move-around}
278 In this case the string is appended to the end of the block on each line,
279 and whitespace is inserted to pad to the end-of-block column.
280See |v_b_A_example|.
281Note: "I" and "A" behave differently for lines that don't extend into the
282selected block. This was done intentionally, so that you can do it the way
283you want.
284
285Visual-block change *v_b_c*
286All selected text in the block will be replaced by the same text string. When
287using "c" the selected text is deleted and Insert mode started. You can then
288enter text (without a line break). When you hit <Esc>, the same string is
289inserted in all previously selected lines.
290
291Visual-block Change *v_b_C*
292Like using "c", but the selection is extended until the end of the line for
293all lines.
294
295 *v_b_<*
296Visual-block Shift *v_b_>*
297The block is shifted by 'shiftwidth'. The RHS of the block is irrelevant. The
298LHS of the block determines the point from which to apply a right shift, and
299padding includes TABs optimally according to 'ts' and 'et'. The LHS of the
300block determines the point upto which to shift left.
301 Note: v_< padding is buggy if the Visual Block starts and ends in the same
302 TAB. (Vim 5.4c).
303See |v_b_>_example|.
304See |v_b_<_example|.
305
306Visual-block Replace *v_b_r*
307Every screen char in the highlighted region is replaced with the same char, ie
308TABs are split and the virtual whitespace is replaced, maintaining screen
309layout.
310See |v_b_r_example|.
311
312
313==============================================================================
3146. Repeating *visual-repeat*
315
316When repeating a Visual mode operator, the operator will be applied to the
317same amount of text as the last time:
318- Linewise Visual mode: The same number of lines.
319- Blockwise Visual mode: The same number of lines and columns.
320- Normal Visual mode within one line: The same number of characters.
321- Normal Visual mode with several lines: The same number of lines, in the
322 last line the same number of characters as in the last line the last time.
323The start of the text is the Cursor position. If the "$" command was used as
324one of the last commands to extend the highlighted text, the repeating will
325be applied up to the rightmost column of the longest line.
326
327
328==============================================================================
3297. Examples *visual-examples*
330
331 *:visual_example*
332Currently the ":" command works on whole lines only. When you select part of
333a line, doing something like ":!date" will replace the whole line. If you
334want only part of the line to be replaced you will have to make a mapping for
335it. In a future release ":" may work on partial lines.
336
337Here is an example, to replace the selected text with the output of "date": >
338 :vmap _a <Esc>`>a<CR><Esc>`<i<CR><Esc>!!date<CR>kJJ
339
340(In the <> notation |<>|, when typing it you should type it literally; you
341need to remove the 'B' and '<' flags from 'cpoptions')
342
343What this does is:
344<Esc> stop Visual mode
345`> go to the end of the Visual area
346a<CR><Esc> break the line after the Visual area
347`< jump to the start of the Visual area
348i<CR><Esc> break the line before the Visual area
349!!date<CR> filter the Visual text through date
350kJJ Join the lines back together
351
352 *visual-search*
353Here is an idea for a mapping that makes it possible to do a search for the
354selected text: >
355 :vmap X y/<C-R>"<CR>
356
357(In the <> notation |<>|, when typing it you should type it literally; you
358need to remove the 'B' and '<' flags from 'cpoptions')
359
360Note that special characters (like '.' and '*') will cause problems.
361
362Visual-block Examples *blockwise-examples*
363With the following text, I will indicate the commands to produce the block and
364the results below. In all cases, the cursor begins on the 'a' in the first
365line if the test text.
366The following modeline settings are assumed ":ts=8:sw=4:".
367
368It will be helpful to
369:set hls
370/<TAB>
371where <TAB> is a real TAB. This helps visualise the operations.
372
373The test text is:
374
375abcdefghijklmnopqrstuvwxyz
376abc defghijklmnopqrstuvwxyz
377abcdef ghi jklmnopqrstuvwxyz
378abcdefghijklmnopqrstuvwxyz
379
3801. fo<C-v>3jISTRING<ESC> *v_b_I_example*
381
382abcdefghijklmnSTRINGopqrstuvwxyz
383abc STRING defghijklmnopqrstuvwxyz
384abcdef ghi STRING jklmnopqrstuvwxyz
385abcdefghijklmnSTRINGopqrstuvwxyz
386
3872. fo<C-v>3j$ASTRING<ESC> *v_b_A_example*
388
389abcdefghijklmnopqrstuvwxyzSTRING
390abc defghijklmnopqrstuvwxyzSTRING
391abcdef ghi jklmnopqrstuvwxyzSTRING
392abcdefghijklmnopqrstuvwxyzSTRING
393
3943. fo<C-v>3j3l<.. *v_b_<_example*
395
396abcdefghijklmnopqrstuvwxyz
397abc defghijklmnopqrstuvwxyz
398abcdef ghi jklmnopqrstuvwxyz
399abcdefghijklmnopqrstuvwxyz
400
4014. fo<C-v>3j>.. *v_b_>_example*
402
403abcdefghijklmn opqrstuvwxyz
404abc defghijklmnopqrstuvwxyz
405abcdef ghi jklmnopqrstuvwxyz
406abcdefghijklmn opqrstuvwxyz
407
4085. fo<C-v>5l3jrX *v_b_r_example*
409
410abcdefghijklmnXXXXXXuvwxyz
411abc XXXXXXhijklmnopqrstuvwxyz
412abcdef ghi XXXXXX jklmnopqrstuvwxyz
413abcdefghijklmnXXXXXXuvwxyz
414
415==============================================================================
4168. Select mode *Select* *Select-mode*
417
418Select mode looks like Visual mode, but the commands accepted are quite
419different. This resembles the selection mode in Microsoft Windows.
420When the 'showmode' option is set, "-- SELECT --" is shown in the last line.
421
422Entering Select mode:
423- Using the mouse to select an area, and 'selectmode' contains "mouse".
424 'mouse' must also contain a flag for the current mode.
425- Using a non-printable movement command, with the Shift key pressed, and
426 'selectmode' contains "key". For example: <S-Left> and <S-End>. 'keymodel'
427 must also contain "startsel".
428- Using "v", "V" or CTRL-V command, and 'selectmode' contains "cmd".
429- Using "gh", "gH" or "g_CTRL-H" command in Normal mode.
430- From Visual mode, press CTRL-G. *v_CTRL-G*
431
432Commands in Select mode:
433- Printable characters, <NL> and <CR> cause the selection to be deleted, and
434 Vim enters Insert mode. The typed character is inserted.
435- Non-printable movement commands, with the Shift key pressed, extend the
436 selection. 'keymodel' must include "startsel".
437- Non-printable movement commands, with the Shift key NOT pressed, stop Select
438 mode. 'keymodel' must include "stopsel".
439- ESC stops Select mode.
440- CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O*
441- CTRL-G switches to Visual mode.
442
443Otherwise, typed characters are handled as in Visual mode.
444
445When using an operator in Select mode, and the selection is linewise, the
446selected lines are operated upon, but like in characterwise selection. For
447example, when a whole line is deleted, it can later be pasted halfway a line.
448
449
450Mappings and menus in Select mode. *Select-mode-mapping*
451
452In Select mode the mappings and menus of Visual mode are used. Before it is
453executed, Vim automatically switches to Visual mode, so that the same
454behavior as in Visual mode is effective.
455
456After the mapping or menu finishes, the selection is enabled again and Select
457mode entered, unless the selected area was deleted, another buffer became
458the current one or the window layout was changed.
459
460When a character was typed that causes the selection to be deleted and Insert
461mode started, Insert mode mappings are applied to this character. This may
462cause some confusion, because it means Insert mode mappings apply to a
463character typed in Select mode. Language mappings apply as well.
464
465 *gV* *v_gV*
466gV Avoid the automatic reselection of the Visual area
467 after a Select mode mapping or menu has finished.
468 Put this just before the end of the mapping or menu.
469 At least it should be after any operations on the
470 selection.
471
472 *gh*
473gh Start Select mode, characterwise. This is like "v",
474 but starts Select mode instead of Visual mode.
475 Mnemonic: "get highlighted".
476
477 *gH*
478gH Start Select mode, linewise. This is like "V",
479 but starts Select mode instead of Visual mode.
480 Mnemonic: "get Highlighted".
481
482 *g_CTRL-H*
483g CTRL-H Start Select mode, blockwise. This is like CTRL-V,
484 but starts Select mode instead of Visual mode.
485 Mnemonic: "get Highlighted".
486
487 vim:tw=78:ts=8:ft=help:norl: