visual.txt (22380B)
1 *visual.txt* Nvim 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 Type |gO| to see the table of contents. 15 16 ============================================================================== 17 1. Using Visual mode *visual-use* 18 19 Using Visual mode consists of three parts: 20 1. Mark the start of the text with "v", "V" or CTRL-V. 21 The character under the cursor will be used as the start. 22 2. Move to the end of the text. 23 The text from the start of the Visual mode up to and including the 24 character under the cursor is highlighted. 25 3. Type an operator command. 26 The highlighted characters will be operated upon. 27 28 The |hl-Visual| group determines the highlighting of the visual selection. 29 The 'virtualedit' option can be used to allow positioning the cursor to 30 positions where there is no actual character. 31 32 The highlighted text normally includes the character under the cursor. 33 However, when the 'selection' option is set to "exclusive" and the cursor is 34 after the Visual area, the character under the cursor is not included. 35 36 With "v" the text before the start position and after the end position will 37 not be highlighted. However, all uppercase and non-alpha operators, except 38 "~" and "U", will work on whole lines anyway. See the list of operators 39 below. 40 41 *visual-block* 42 With CTRL-V (blockwise Visual mode) the highlighted text will be a rectangle 43 between start position and the cursor. However, some operators work on whole 44 lines anyway (see the list below). The change and substitute operators will 45 delete the highlighted text and then start insertion at the top left 46 position. 47 48 ============================================================================== 49 2. Starting and stopping Visual mode *visual-start* 50 51 *v* *charwise-visual* 52 [count]v Start Visual mode per character. 53 With [count] select the same number of characters or 54 lines as used for the last Visual operation, but at 55 the current cursor position, multiplied by [count]. 56 When the previous Visual operation was on a block both 57 the width and height of the block are multiplied by 58 [count]. 59 When there was no previous Visual operation [count] 60 characters are selected. This is like moving the 61 cursor right N * [count] characters. One less when 62 'selection' is not "exclusive". 63 64 *V* *linewise-visual* 65 [count]V Start Visual mode linewise. 66 With [count] select the same number of lines as used 67 for the last Visual operation, but at the current 68 cursor position, multiplied by [count]. When there 69 was no previous Visual operation [count] lines are 70 selected. 71 72 *CTRL-V* *blockwise-visual* 73 [count]CTRL-V Start Visual mode blockwise. 74 75 If you use <Esc>, click the left mouse button or use any command that 76 does a jump to another buffer while in Visual mode, the highlighting stops 77 and no text is affected. Also when you hit "v" in charwise Visual mode, 78 "CTRL-V" in blockwise Visual mode or "V" in linewise Visual mode. If you hit 79 CTRL-Z the highlighting stops and the editor is suspended or a new shell is 80 started |CTRL-Z|. 81 82 new mode after typing: *v_v* *v_CTRL-V* *v_V* 83 old mode "v" "CTRL-V" "V" ~ 84 85 Normal Visual blockwise Visual linewise Visual 86 Visual Normal blockwise Visual linewise Visual 87 blockwise Visual Visual Normal linewise Visual 88 linewise Visual Visual blockwise Visual Normal 89 90 *gv* *v_gv* *reselect-Visual* 91 gv Start Visual mode with the same area as the previous 92 area and the same mode. 93 In Visual mode the current and the previous Visual 94 area are exchanged. 95 After using "p" or "P" in Visual mode the text that 96 was put will be selected. 97 98 *gn* *v_gn* 99 gn Search forward for the last used search pattern, like 100 with `n`, and start Visual mode to select the match. 101 If the cursor is on the match, visually selects it. 102 If an operator is pending, operates on the match. 103 E.g., "dgn" deletes the text of the next match. 104 If Visual mode is active, extends the selection 105 until the end of the next match. 106 'wrapscan' applies. 107 Note: Unlike `n` the search direction does not depend 108 on the previous search command. 109 110 *gN* *v_gN* 111 gN Like |gn| but searches backward, like with `N`. 112 113 *<LeftMouse>* 114 <LeftMouse> Set the current cursor position. If Visual mode is 115 active it is stopped. Only when 'mouse' option 116 contains 'n' or 'a'. If the position is within 'so' 117 lines from the last line on the screen the text is 118 scrolled up. If the position is within 'so' lines 119 from the first line on the screen the text is scrolled 120 down. 121 122 *<RightMouse>* 123 <RightMouse> Start Visual mode if it is not active. The text from 124 the cursor position to the position of the click is 125 highlighted. If Visual mode was already active move 126 the start or end of the highlighted text, whichever 127 is closest, to the position of the click. Only when 128 'mouse' option contains 'n' or 'a'. 129 130 Note: when 'mousemodel' is set to "popup", 131 <S-LeftMouse> has to be used instead of <RightMouse>. 132 133 *<LeftRelease>* 134 <LeftRelease> This works like a <LeftMouse>, if it is not at 135 the same position as <LeftMouse>. In an older version 136 of xterm you won't see the selected area until the 137 button is released, unless there is access to the 138 display where the xterm is running (via the DISPLAY 139 environment variable or the -display argument). Only 140 when 'mouse' option contains 'n' or 'a'. 141 142 If Visual mode is not active and the "v", "V" or CTRL-V is preceded with a 143 count, the size of the previously highlighted area is used for a start. You 144 can then move the end of the highlighted area and give an operator. The type 145 of the old area is used (character, line or blockwise). 146 - Linewise Visual mode: The number of lines is multiplied with the count. 147 - Blockwise Visual mode: The number of lines and columns is multiplied with 148 the count. 149 - Normal Visual mode within one line: The number of characters is multiplied 150 with the count. 151 - Normal Visual mode with several lines: The number of lines is multiplied 152 with the count, in the last line the same number of characters is used as 153 in the last line in the previously highlighted area. 154 The start of the text is the Cursor position. If the "$" command was used as 155 one of the last commands to extend the highlighted text, the area will be 156 extended to the rightmost column of the longest line. 157 158 If you want to highlight exactly the same area as the last time, you can use 159 "gv" |gv| |v_gv|. 160 161 *v_<Esc>* 162 <Esc> In Visual mode: Stop Visual mode. 163 *v_META* *v_ALT* 164 ALT (|META|) may act like <Esc> if the chord is not mapped. 165 For example <A-x> acts like <Esc>x if <A-x> does not have a 166 visual-mode mapping. 167 168 *v_CTRL-C* 169 CTRL-C In Visual mode: Stop Visual mode. When insert mode is 170 pending (the mode message shows 171 "-- (insert) VISUAL --"), it is also stopped. 172 On MS-Windows, you may need to press CTRL-Break. 173 174 ============================================================================== 175 3. Changing the Visual area *visual-change* 176 177 *v_o* 178 o Go to Other end of highlighted text: The current 179 cursor position becomes the start of the highlighted 180 text and the cursor is moved to the other end of the 181 highlighted text. The highlighted area remains the 182 same. 183 184 *v_O* 185 O Go to Other end of highlighted text. This is like 186 "o", but in Visual block mode the cursor moves to the 187 other corner in the same line. When the corner is at 188 a character that occupies more than one position on 189 the screen (e.g., a <Tab>), the highlighted text may 190 change. 191 192 *v_$* 193 When the "$" command is used with blockwise Visual mode, the right end of the 194 highlighted text will be determined by the longest highlighted line. This 195 stops when a motion command is used that does not move straight up or down. 196 197 For moving the end of the block many commands can be used, but you cannot 198 use Ex commands, commands that make changes or abandon the file. Commands 199 (starting with) ".", "&", CTRL-^, "Z", CTRL-], CTRL-T, CTRL-R, CTRL-I 200 and CTRL-O cause a beep and Visual mode continues. 201 202 When switching to another window on the same buffer, the cursor position in 203 that window is adjusted, so that the same Visual area is still selected. This 204 is especially useful to view the start of the Visual area in one window, and 205 the end in another. You can then use <RightMouse> (or <S-LeftMouse> when 206 'mousemodel' is "popup") to drag either end of the Visual area. 207 208 ============================================================================== 209 4. Operating on the Visual area *visual-operators* 210 211 The operators that can be used are: 212 ~ switch case |v_~| 213 d delete |v_d| 214 c change (4) |v_c| 215 y yank |v_y| 216 > shift right (4) |v_>| 217 < shift left (4) |v_<| 218 ! filter through external command (1) |v_!| 219 = filter through 'equalprg' option command (1) |v_=| 220 gq format lines to 'textwidth' length (1) |v_gq| 221 222 The objects that can be used are: 223 aw a word (with white space) |v_aw| 224 iw inner word |v_iw| 225 aW a WORD (with white space) |v_aW| 226 iW inner WORD |v_iW| 227 as a sentence (with white space) |v_as| 228 is inner sentence |v_is| 229 ap a paragraph (with white space) |v_ap| 230 ip inner paragraph |v_ip| 231 ab a () block (with parentheses) |v_ab| 232 ib inner () block |v_ib| 233 aB a {} block (with braces) |v_aB| 234 iB inner {} block |v_iB| 235 at a <tag> </tag> block (with tags) |v_at| 236 it inner <tag> </tag> block |v_it| 237 a< a <> block (with <>) |v_a<| 238 i< inner <> block |v_i<| 239 a[ a [] block (with []) |v_a[| 240 i[ inner [] block |v_i[| 241 a" a double quoted string (with quotes) |v_aquote| 242 i" inner double quoted string |v_iquote| 243 a' a single quoted string (with quotes) |v_a'| 244 i' inner simple quoted string |v_i'| 245 a` a string in backticks (with backticks) |v_a`| 246 i` inner string in backticks |v_i`| 247 248 Additionally the following commands can be used: 249 : start Ex command for highlighted lines (1) |v_:| 250 r change (4) |v_r| 251 s change |v_s| 252 C change (2)(4) |v_C| 253 S change (2) |v_S| 254 R change (2) |v_R| 255 x delete |v_x| 256 D delete (3) |v_D| 257 X delete (2) |v_X| 258 Y yank (2) |v_Y| 259 p put |v_p| 260 P put without overwriting registers |v_P| 261 J join (1) |v_J| 262 U make uppercase |v_U| 263 u make lowercase |v_u| 264 ^] find tag |v_CTRL-]| 265 I block insert |v_b_I| 266 A block append |v_b_A| 267 268 (1): Always whole lines, see |:visual_example|. 269 (2): Whole lines when not using CTRL-V. 270 (3): Whole lines when not using CTRL-V, delete until the end of the line when 271 using CTRL-V. 272 (4): When using CTRL-V operates on the block only. 273 274 Note that the ":vmap" command can be used to specifically map keys in Visual 275 mode. For example, if you would like the "/" command not to extend the Visual 276 area, but instead take the highlighted text and search for that: > 277 :vmap / y/<C-R>"<CR> 278 (In the <> notation |<>|, when typing it you should type it literally; you 279 need to remove the 'B' flag from 'cpoptions'.) 280 281 If you want to give a register name using the """ command, do this just before 282 typing the operator character: "v{move-around}"xd". 283 284 If you want to give a count to the command, do this just before typing the 285 operator character: "v{move-around}3>" (move lines 3 indents to the right). 286 287 *{move-around}* 288 The {move-around} is any sequence of movement commands. Note the difference 289 with {motion}, which is only ONE movement command. 290 291 Another way to operate on the Visual area is using the |/\%V| item in a 292 pattern. For example, to replace all '(' in the Visual area with '#': > 293 294 :'<,'>s/\%V(/#/g 295 296 Note that the "'<,'>" will appear automatically when you press ":" in Visual 297 mode. 298 299 ============================================================================== 300 5. Blockwise operators *blockwise-operators* 301 302 Reminder: Use 'virtualedit' to be able to select blocks that start or end 303 after the end of a line or halfway through a tab. 304 305 Visual-block Insert *v_b_I* 306 With a blockwise selection, I{string}<ESC> will insert {string} at the start 307 of block on every line of the block, provided that the line extends into the 308 block. Thus lines that are short will remain unmodified. TABs are split to 309 retain visual columns. Works only for adding text to a line, not for 310 deletions. See |v_b_I_example|. 311 312 Visual-block Append *v_b_A* 313 With a blockwise selection, A{string}<ESC> will append {string} to the end of 314 block on every line of the block. There is some differing behavior where the 315 block RHS is not straight, due to different line lengths: 316 317 1. Block was created with <C-v>$ 318 In this case the string is appended to the end of each line. 319 2. Block was created with <C-v>{move-around} 320 In this case the string is appended to the end of the block on each line, 321 and whitespace is inserted to pad to the end-of-block column. 322 See |v_b_A_example|. 323 Note: "I" and "A" behave differently for lines that don't extend into the 324 selected block. This was done intentionally, so that you can do it the way 325 you want. 326 Works only for adding text to a line, not for deletions. 327 328 Visual-block change *v_b_c* 329 All selected text in the block will be replaced by the same text string. When 330 using "c" the selected text is deleted and Insert mode started. You can then 331 enter text (without a line break). When you hit <Esc>, the same string is 332 inserted in all previously selected lines. 333 334 Visual-block Change *v_b_C* 335 Like using "c", but the selection is extended until the end of the line for 336 all lines. 337 338 *v_b_<* 339 Visual-block Shift *v_b_>* 340 The block is shifted by 'shiftwidth'. The RHS of the block is irrelevant. 341 The LHS of the block determines the point from which to apply a right shift, 342 and padding includes TABs optimally according to 'ts' and 'et'. The LHS of 343 the block determines the point up to which to shift left. 344 See |v_b_>_example|. 345 See |v_b_<_example|. 346 347 Visual-block Replace *v_b_r* 348 Every screen char in the highlighted region is replaced with the same char, ie 349 TABs are split and the virtual whitespace is replaced, maintaining screen 350 layout. 351 See |v_b_r_example|. 352 353 354 ============================================================================== 355 6. Repeating *visual-repeat* 356 357 When repeating a Visual mode operator, the operator will be applied to the 358 same amount of text as the last time: 359 - Linewise Visual mode: The same number of lines. 360 - Blockwise Visual mode: The same number of lines and columns. 361 - Normal Visual mode within one line: The same number of characters. 362 - Normal Visual mode with several lines: The same number of lines, in the 363 last line the same number of characters as in the last line the last time. 364 The start of the text is the Cursor position. If the "$" command was used as 365 one of the last commands to extend the highlighted text, the repeating will 366 be applied up to the rightmost column of the longest line. Any count passed 367 to the `.` command is not used. 368 369 Visual mode |default-mappings| "@" and "Q" repeat a register for all selected 370 lines if the selection is linewise. See |v_@-default| and |v_Q-default| for 371 details. For example, given the text: 372 373 123(hello)321 374 456(world)654 375 456(NOT THIS)654 376 377 With register "x" containing the commands `yi(VP`, visually selecting the 378 first two lines and typing `@x` produces: 379 380 hello 381 world 382 456(NOT THIS)654 383 384 ============================================================================== 385 7. Examples *visual-examples* 386 387 *:visual_example* 388 Currently the ":" command works on whole lines only. When you select part of 389 a line, doing something like ":!date" will replace the whole line. If you 390 want only part of the line to be replaced you will have to make a mapping for 391 it. In a future release ":" may work on partial lines. 392 393 Here is an example, to replace the selected text with the output of "date": > 394 :vmap _a <Esc>`>a<CR><Esc>`<i<CR><Esc>!!date<CR>kgJgJ 395 396 (In the <> notation |<>|, when typing it you should type it literally; you 397 need to remove the 'B' flag from 'cpoptions') 398 399 What this does is: 400 <Esc> stop Visual mode 401 `> go to the end of the Visual area 402 a<CR><Esc> break the line after the Visual area 403 `< jump to the start of the Visual area 404 i<CR><Esc> break the line before the Visual area 405 !!date<CR> filter the Visual text through date 406 kJJ Join the lines back together 407 408 *visual-search* 409 Here is an idea for a mapping that makes it possible to do a search for the 410 selected text: > 411 :vmap X y/<C-R>"<CR> 412 413 (In the <> notation |<>|, when typing it you should type it literally; you 414 need to remove the 'B' flag from 'cpoptions') 415 416 Note that special characters (like '.' and "*") will cause problems. 417 418 Visual-block Examples *blockwise-examples* 419 With the following text, I will indicate the commands to produce the block and 420 the results below. In all cases, the cursor begins on the 'a' in the first 421 line of the test text. 422 The following modeline settings are assumed ":ts=8:sw=4:". 423 424 It will be helpful to 425 :set hls 426 /<TAB> 427 where <TAB> is a real TAB. This helps visualise the operations. 428 429 The test text is: 430 431 abcdefghijklmnopqrstuvwxyz 432 abc defghijklmnopqrstuvwxyz 433 abcdef ghi jklmnopqrstuvwxyz 434 abcdefghijklmnopqrstuvwxyz 435 436 1. fo<C-v>3jISTRING<ESC> *v_b_I_example* 437 438 abcdefghijklmnSTRINGopqrstuvwxyz 439 abc STRING defghijklmnopqrstuvwxyz 440 abcdef ghi STRING jklmnopqrstuvwxyz 441 abcdefghijklmnSTRINGopqrstuvwxyz 442 443 2. fo<C-v>3j$ASTRING<ESC> *v_b_A_example* 444 445 abcdefghijklmnopqrstuvwxyzSTRING 446 abc defghijklmnopqrstuvwxyzSTRING 447 abcdef ghi jklmnopqrstuvwxyzSTRING 448 abcdefghijklmnopqrstuvwxyzSTRING 449 450 3. fo<C-v>3j3l<.. *v_b_<_example* 451 452 abcdefghijklmnopqrstuvwxyz 453 abc defghijklmnopqrstuvwxyz 454 abcdef ghi jklmnopqrstuvwxyz 455 abcdefghijklmnopqrstuvwxyz 456 457 4. fo<C-v>3j>.. *v_b_>_example* 458 459 abcdefghijklmn opqrstuvwxyz 460 abc defghijklmnopqrstuvwxyz 461 abcdef ghi jklmnopqrstuvwxyz 462 abcdefghijklmn opqrstuvwxyz 463 464 5. fo<C-v>5l3jrX *v_b_r_example* 465 466 abcdefghijklmnXXXXXXuvwxyz 467 abc XXXXXXhijklmnopqrstuvwxyz 468 abcdef ghi XXXXXX jklmnopqrstuvwxyz 469 abcdefghijklmnXXXXXXuvwxyz 470 471 ============================================================================== 472 8. Select mode *Select* *Select-mode* 473 474 Select mode looks like Visual mode, but the commands accepted are quite 475 different. This resembles the selection mode in Microsoft Windows. 476 When the 'showmode' option is set, "-- SELECT --" is shown in the last line. 477 478 Entering Select mode: 479 - Using the mouse to select an area, and 'selectmode' contains "mouse". 480 'mouse' must also contain a flag for the current mode. 481 - Using a non-printable movement command, with the Shift key pressed, and 482 'selectmode' contains "key". For example: <S-Left> and <S-End>. 'keymodel' 483 must also contain "startsel". 484 - Using "v", "V" or CTRL-V command, and 'selectmode' contains "cmd". 485 - Using "gh", "gH" or "g_CTRL-H" command in Normal mode. 486 - From Visual mode, press CTRL-G. *v_CTRL-G* 487 488 Commands in Select mode: 489 - Printable characters, <NL> and <CR> cause the selection to be deleted, and 490 Vim enters Insert mode. The typed character is inserted. 491 - Non-printable movement commands, with the Shift key pressed, extend the 492 selection. 'keymodel' must include "startsel". 493 - Non-printable movement commands, with the Shift key NOT pressed, stop Select 494 mode. 'keymodel' must include "stopsel". 495 - ESC stops Select mode. 496 - CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O* 497 - CTRL-G switches to Visual mode. 498 - CTRL-R {register} selects the register to be used for the text that is 499 deleted when typing text. *v_CTRL-R* 500 Unless you specify the "_" (black hole) register, the unnamed register is 501 also overwritten. 502 503 Otherwise, typed characters are handled as in Visual mode. 504 505 When using an operator in Select mode, and the selection is linewise, the 506 selected lines are operated upon, but like in charwise selection. For 507 example, when a whole line is deleted, it can later be pasted in the middle of 508 a line. 509 510 511 Mappings and menus in Select mode. *Select-mode-mapping* 512 513 When mappings and menus are defined with the |:vmap| or |:vmenu| command they 514 work both in Visual mode and in Select mode. When these are used in Select 515 mode Vim automatically switches to Visual mode, so that the same behavior as 516 in Visual mode is effective. If you don't want this use |:xmap| or |:smap|. 517 518 One particular edge case: > 519 :vnoremap <C-K> <Esc> 520 This ends Visual mode when in Visual mode, but in Select mode it does not 521 work, because Select mode is restored after executing the mapped keys. You 522 need to use: > 523 :snoremap <C-K> <Esc> 524 < 525 Users will expect printable characters to replace the selected area. 526 Therefore avoid mapping printable characters in Select mode. Or use 527 |:sunmap| after |:map| and |:vmap| to remove it for Select mode. 528 529 After the mapping or menu finishes, the selection is enabled again and Select 530 mode entered, unless the selected area was deleted, another buffer became 531 the current one or the window layout was changed. 532 533 When a character was typed that causes the selection to be deleted and Insert 534 mode started, Insert mode mappings are applied to this character. This may 535 cause some confusion, because it means Insert mode mappings apply to a 536 character typed in Select mode. Language mappings apply as well. 537 538 *gV* *v_gV* 539 gV Avoid the automatic reselection of the Visual area 540 after a Select mode mapping or menu has finished. 541 Put this just before the end of the mapping or menu. 542 At least it should be after any operations on the 543 selection. 544 545 *gh* 546 gh Start Select mode, charwise. This is like "v", 547 but starts Select mode instead of Visual mode. 548 Mnemonic: "get highlighted". 549 550 *gH* 551 gH Start Select mode, linewise. This is like "V", 552 but starts Select mode instead of Visual mode. 553 Mnemonic: "get Highlighted". 554 555 *g_CTRL-H* 556 g CTRL-H Start Select mode, blockwise. This is like CTRL-V, 557 but starts Select mode instead of Visual mode. 558 Mnemonic: "get Highlighted". 559 560 vim:tw=78:ts=8:noet:ft=help:norl: