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