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