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