robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1*change.txt* Nvim 2 3 4 VIM REFERENCE MANUAL by Bram Moolenaar 5 6 7This file describes commands that delete or change text. In this context, 8changing text means deleting the text and replacing it with other text using 9one command. You can undo all of these commands. You can repeat the non-Ex 10commands with the "." command. 11 12For inserting text see |insert.txt|. 13 14 Type |gO| to see the table of contents. 15 16============================================================================== 171. Deleting text *deleting* *E470* 18 19["x]<Del> or *<Del>* *x* *dl* 20["x]x Delete [count] characters under and after the cursor 21 [into register x] (not |linewise|). Does the same as 22 "dl". 23 The <Del> key does not take a [count]. Instead, it 24 deletes the last character of the count. 25 See 'whichwrap' for deleting a line break (join 26 lines). 27 28 *X* *dh* 29["x]X Delete [count] characters before the cursor [into 30 register x] (not |linewise|). Does the same as "dh". 31 Also see 'whichwrap'. 32 33 *d* 34["x]d{motion} Delete text that {motion} moves over [into register 35 x]. See below for exceptions. 36 37 *dd* 38["x]dd Delete [count] lines [into register x] |linewise|. 39 40 *D* 41["x]D Delete the characters under the cursor until the end 42 of the line and [count]-1 more lines [into register 43 x]; synonym for "d$". 44 (not |linewise|) 45 46{Visual}["x]x or *v_x* *v_d* *v_<Del>* 47{Visual}["x]d or 48{Visual}["x]<Del> Delete the highlighted text [into register x] (for 49 {Visual} see |Visual-mode|). 50 51{Visual}["x]CTRL-H or *v_CTRL-H* *v_<BS>* 52{Visual}["x]<BS> When in Select mode: Delete the highlighted text [into 53 register x]. 54 55{Visual}["x]X or *v_X* *v_D* *v_b_D* 56{Visual}["x]D Delete the highlighted lines [into register x] (for 57 {Visual} see |Visual-mode|). In Visual block mode, 58 "D" deletes the highlighted text plus all text until 59 the end of the line. 60 61 *:d* *:de* *:del* *:delete* *:dl* *:dp* 62:[range]d[elete] [x] Delete [range] lines (default: current line) [into 63 register x]. 64 Note these weird abbreviations: 65 :dl delete and list 66 :dell idem 67 :delel idem 68 :deletl idem 69 :deletel idem 70 :dp delete and print 71 :dep idem 72 :delp idem 73 :delep idem 74 :deletp idem 75 :deletep idem 76 77:[range]d[elete] [x] {count} 78 Delete {count} lines, starting with [range] 79 (default: current line |cmdline-ranges|) [into 80 register x]. 81 82These commands delete text. You can repeat them with the `.` command 83(except `:d`) and undo them. Use Visual mode to delete blocks of text. See 84|registers| for an explanation of registers. 85 *d-special* 86An exception for the d{motion} command: If the motion is not linewise, the 87start and end of the motion are not in the same line, and there are only 88blanks before the start and there are no non-blanks after the end of the 89motion, the delete becomes linewise. This means that the delete also removes 90the line of blanks that you might expect to remain. Use the |o_v| operator to 91force the motion to be charwise. 92 93Trying to delete an empty region of text (e.g., "d0" in the first column) 94is an error when 'cpoptions' includes the 'E' flag. 95 96 *J* 97J Join [count] lines, with a minimum of two lines. 98 Remove the indent and insert up to two spaces (see 99 below). Fails when on the last line of the buffer. 100 If [count] is too big it is reduced to the number of 101 lines available. 102 103 *v_J* 104{Visual}J Join the highlighted lines, with a minimum of two 105 lines. Remove the indent and insert up to two spaces 106 (see below). 107 108 *gJ* 109gJ Join [count] lines, with a minimum of two lines. 110 Don't insert or remove any spaces. 111 112 *v_gJ* 113{Visual}gJ Join the highlighted lines, with a minimum of two 114 lines. Don't insert or remove any spaces. 115 116 *:j* *:join* 117:[range]j[oin][!] [flags] 118 Join [range] lines. Same as "J", except with [!] 119 the join does not insert or delete any spaces. 120 If a [range] has equal start and end values, this 121 command does nothing. The default behavior is to 122 join the current line with the line below it. 123 See |ex-flags| for [flags]. 124 125:[range]j[oin][!] {count} [flags] 126 Join {count} lines, starting with [range] (default: 127 current line |cmdline-ranges|). Same as "J", except 128 with [!] the join does not insert or delete any 129 spaces. 130 See |ex-flags| for [flags]. 131 132These commands delete the <EOL> between lines. This has the effect of joining 133multiple lines into one line. You can repeat these commands (except `:j`) and 134undo them. 135 136These commands, except "gJ", insert one space in place of the <EOL> unless 137there is trailing white space or the next line starts with a ')'. These 138commands, except "gJ", delete any leading white space on the next line. If 139the 'joinspaces' option is on, these commands insert two spaces after a '.', 140'!' or '?'. 141The 'B' and 'M' flags in 'formatoptions' change the behavior for inserting 142spaces before and after a multibyte character |fo-table|. 143 144The |'[| mark is set at the end of the first line that was joined, |']| at the 145end of the resulting line. 146 147 148============================================================================== 1492. Delete and insert *delete-insert* *replacing* 150 151 *R* 152R Enter Replace mode: Each character you type replaces 153 an existing character, starting with the character 154 under the cursor. Repeat the entered text [count]-1 155 times. See |Replace-mode| for more details. 156 157 *gR* 158gR Enter Virtual Replace mode: Each character you type 159 replaces existing characters in screen space. So a 160 <Tab> may replace several characters at once. 161 Repeat the entered text [count]-1 times. See 162 |Virtual-Replace-mode| for more details. 163 164 *c* 165["x]c{motion} Delete {motion} text [into register x] and start 166 insert. When 'cpoptions' includes the 'E' flag and 167 there is no text to delete (e.g., with "cTx" when the 168 cursor is just after an 'x'), an error occurs and 169 insert mode does not start (this is Vi compatible). 170 When 'cpoptions' does not include the 'E' flag, the 171 "c" command always starts insert mode, even if there 172 is no text to delete. 173 174 *cc* 175["x]cc Delete [count] lines [into register x] and start 176 insert |linewise|. If 'autoindent' is on, preserve 177 the indent of the first line. 178 179 *C* 180["x]C Delete from the cursor position to the end of the 181 line and [count]-1 more lines [into register x], and 182 start insert. Synonym for c$ (not |linewise|). 183 184 *s* 185["x]s Delete [count] characters [into register x] and start 186 insert (s stands for Substitute). Synonym for "cl" 187 (not |linewise|). 188 189 *S* 190["x]S Delete [count] lines [into register x] and start 191 insert. Synonym for "cc" |linewise|. 192 193{Visual}["x]c or *v_c* *v_s* 194{Visual}["x]s Delete the highlighted text [into register x] and 195 start insert (for {Visual} see |Visual-mode|). 196 197 *v_r* 198{Visual}r{char} Replace all selected characters by {char}. 199 CTRL-C will be inserted literally. 200 201 *v_C* 202{Visual}["x]C Delete the highlighted lines [into register x] and 203 start insert. In Visual block mode it works 204 differently |v_b_C|. 205 *v_S* 206{Visual}["x]S Delete the highlighted lines [into register x] and 207 start insert (for {Visual} see |Visual-mode|). 208 *v_R* 209{Visual}["x]R Currently just like {Visual}["x]S. In a next version 210 it might work differently. 211 212Notes: 213- You can end Insert and Replace mode with <Esc>. 214- See the section "Insert and Replace mode" |mode-ins-repl| for the other 215 special characters in these modes. 216- The effect of [count] takes place after Vim exits Insert or Replace mode. 217- When the 'cpoptions' option contains '$' and the change is within one line, 218 Vim continues to show the text to be deleted and puts a '$' at the last 219 deleted character. 220 221See |registers| for an explanation of registers. 222 223Replace mode is just like Insert mode, except that every character you enter 224deletes one character. If you reach the end of a line, Vim appends any 225further characters (just like Insert mode). In Replace mode, the backspace 226key restores the original text (if there was any). (See section "Insert and 227Replace mode" |mode-ins-repl|). 228 229 *cw* *cW* 230Special case: When the cursor is in a word, "cw" and "cW" do not include the 231white space after a word, they only change up to the end of the word. This is 232because Vim interprets "cw" as change-word, and a word does not include the 233following white space. 234 235If you prefer "cw" to include the space after a word, see |cpo-_| to change 236the behavior. Alternatively, use "caw" (see |aw|). 237 238 *:c* *:ch* *:change* 239:{range}c[hange][!] Replace lines of text with some different text. 240 Type a line containing only "." to stop replacing. 241 Without {range}, this command changes only the current 242 line. 243 Adding [!] toggles 'autoindent' for the time this 244 command is executed. 245 246============================================================================== 2473. Simple changes *simple-change* *changing* 248 249 *r* 250r{char} Replace the character under the cursor with {char}. 251 If {char} is a <CR> or <NL>, a line break replaces the 252 character. To replace with a real <CR>, use CTRL-V 253 <CR>. CTRL-V <NL> replaces with a <Nul>. 254 255 If {char} is CTRL-E or CTRL-Y the character from the 256 line below or above is used, just like with |i_CTRL-E| 257 and |i_CTRL-Y|. This also works with a count, thus 258 `10r<C-E>` copies 10 characters from the line below. 259 260 If you give a [count], Vim replaces [count] characters 261 with [count] {char}s. When {char} is a <CR> or <NL>, 262 however, Vim inserts only one <CR>: "5r<CR>" replaces 263 five characters with a single line break. 264 When {char} is a <CR> or <NL>, Vim performs 265 autoindenting. This works just like deleting the 266 characters that are replaced and then doing 267 "i<CR><Esc>". 268 {char} can be entered as a digraph |digraph-arg|. 269 |:lmap| mappings apply to {char}. The CTRL-^ command 270 in Insert mode can be used to switch this on/off 271 |i_CTRL-^|. See |utf-8-char-arg| about using 272 composing characters when 'encoding' is Unicode. 273 274 *gr* 275gr{char} Replace the virtual characters under the cursor with 276 {char}. This replaces in screen space, not file 277 space. See |gR| and |Virtual-Replace-mode| for more 278 details. As with |r| a count may be given. 279 {char} can be entered like with |r|, but characters 280 that have a special meaning in Insert mode, such as 281 most CTRL-keys, cannot be used. 282 283 *gr-default* 284 Nvim creates |lsp-defaults| mappings which may inhibit 285 the builtin behavior of |gr|. Use this to restore the 286 builtin behavior: > 287 nnoremap <nowait> gr gr 288< 289 290 *digraph-arg* 291The argument for Normal mode commands like |r| and |t| is a single character. 292When 'cpo' doesn't contain the 'D' flag, this character can also be entered 293like |digraphs|. First type CTRL-K and then the two digraph characters. 294 295 *case* 296The following commands change the case of letters. The currently active 297|locale| is used. See |:language|. The LC_CTYPE value matters here. 298 299 *~* 300~ 'notildeop' option: Switch case of the character 301 under the cursor and move the cursor to the right. 302 If a [count] is given, do that many characters. 303 304~{motion} 'tildeop' option: switch case of {motion} text. 305 306 *g~* 307g~{motion} Switch case of {motion} text. 308 309g~g~ *g~g~* *g~~* 310g~~ Switch case of current line. 311 312 *v_~* 313{Visual}~ Switch case of highlighted text (for {Visual} see 314 |Visual-mode|). 315 316 *v_U* 317{Visual}U Make highlighted text uppercase (for {Visual} see 318 |Visual-mode|). 319 320 *gU* *uppercase* 321gU{motion} Make {motion} text uppercase. 322 Example: > 323 :map! <C-F> <Esc>gUiw`]a 324< This works in Insert mode: press CTRL-F to make the 325 word before the cursor uppercase. Handy to type 326 words in lowercase and then make them uppercase. 327 328 329gUgU *gUgU* *gUU* 330gUU Make current line uppercase. 331 332 *v_u* 333{Visual}u Make highlighted text lowercase (for {Visual} see 334 |Visual-mode|). 335 336 *gu* *lowercase* 337gu{motion} Make {motion} text lowercase. 338 339gugu *gugu* *guu* 340guu Make current line lowercase. 341 342 *g?* *rot13* 343g?{motion} Rot13 encode {motion} text. 344 345 *v_g?* 346{Visual}g? Rot13 encode the highlighted text (for {Visual} see 347 |Visual-mode|). 348 349g?g? *g?g?* *g??* 350g?? Rot13 encode current line. 351 352To turn one line into title caps, make every first letter of a word 353uppercase: > 354 :s/\v<(.)(\w*)/\u\1\L\2/g 355 356 357Adding and subtracting ~ 358 *CTRL-A* 359CTRL-A Add [count] to the number or alphabetic character at 360 or after the cursor. 361 362 *v_CTRL-A* 363{Visual}CTRL-A Add [count] to the number or alphabetic character in 364 the highlighted text. 365 366 *v_g_CTRL-A* 367{Visual}g CTRL-A Add [count] to the number or alphabetic character in 368 the highlighted text. If several lines are 369 highlighted, each one will be incremented by an 370 additional [count] (so effectively creating a 371 [count] incrementing sequence). 372 For Example, if you have this list of numbers: > 373 1. 374 1. 375 1. 376 1. 377< Move to the second "1." and Visually select three 378 lines, pressing g CTRL-A results in: > 379 1. 380 2. 381 3. 382 4. 383< 384 *CTRL-X* 385CTRL-X Subtract [count] from the number or alphabetic 386 character at or after the cursor. 387 388 *v_CTRL-X* 389{Visual}CTRL-X Subtract [count] from the number or alphabetic 390 character in the highlighted text. 391 392 *v_g_CTRL-X* 393{Visual}g CTRL-X Subtract [count] from the number or alphabetic 394 character in the highlighted text. If several lines 395 are highlighted, each value will be decremented by an 396 additional [count] (so effectively creating a [count] 397 decrementing sequence). 398 399The CTRL-A and CTRL-X commands work for (signed) decimal numbers, unsigned 400binary/octal/hexadecimal numbers and alphabetic characters. 401 402This depends on the 'nrformats' option: 403- When 'nrformats' includes "bin", Vim assumes numbers starting with '0b' or 404 '0B' are binary. 405- When 'nrformats' includes "octal", Vim considers numbers starting with a '0' 406 to be octal, unless the number includes a '8' or '9'. Other numbers are 407 decimal and may have a preceding minus sign. 408 If the cursor is on a number, the commands apply to that number; otherwise 409 Vim uses the number to the right of the cursor. 410- When 'nrformats' includes "hex", Vim assumes numbers starting with '0x' or 411 '0X' are hexadecimal. The case of the rightmost letter in the number 412 determines the case of the resulting hexadecimal number. If there is no 413 letter in the current number, Vim uses the previously detected case. 414- When 'nrformats' includes "alpha", Vim will change the alphabetic character 415 under or after the cursor. This is useful to make lists with an alphabetic 416 index. 417 418For decimals a leading negative sign is considered for incrementing or 419decrementing, for binary, octal and hex values, it won't be considered. To 420ignore the sign Visually select the number before using CTRL-A or CTRL-X. 421 422For numbers with leading zeros (including all octal and hexadecimal numbers), 423Vim preserves the number of characters in the number when possible. CTRL-A on 424"0077" results in "0100", CTRL-X on "0x100" results in "0x0ff". 425There is one exception: When a number that starts with a zero is found not to 426be octal (it contains a '8' or '9'), but 'nrformats' does include "octal", 427leading zeros are removed to avoid that the result may be recognized as an 428octal number. 429 430Note that when 'nrformats' includes "octal", decimal numbers with leading 431zeros cause mistakes, because they can be confused with octal numbers. 432 433Note similarly, when 'nrformats' includes both "bin" and "hex", binary numbers 434with a leading '0x' or '0X' can be interpreted as hexadecimal rather than 435binary since '0b' are valid hexadecimal digits. CTRL-A on "0x0b11" results in 436"0x0b12", not "0x0b100". 437When 'nrformats' includes "bin" and doesn't include "hex", CTRL-A on "0b11" in 438"0x0b11" results in "0x0b100". 439 440When the number under the cursor is too big to fit into 64 bits, it will be 441rounded off to the nearest number that can be represented, and the 442addition/subtraction is skipped. E.g. CTRL-X on 18446744073709551616 results 443in 18446744073709551615. Same for larger numbers, such as 18446744073709551618. 444 445The CTRL-A command is very useful in a macro. Example: Use the following 446steps to make a numbered list. 447 4481. Create the first list entry, make sure it starts with a number. 4492. qa - start recording into register 'a' 4503. Y - yank the entry 4514. p - put a copy of the entry below the first one 4525. CTRL-A - increment the number 4536. q - stop recording 4547. <count>@a - repeat the yank, put and increment <count> times 455 456 457SHIFTING LINES LEFT OR RIGHT *shift-left-right* 458 459 *<* 460 <{motion} Shift {motion} lines one 'shiftwidth' leftwards. 461 462 If the 'shiftwidth' option is set to zero, the amount 463 of indent is calculated at the first non-blank 464 character in the line. 465 *<<* 466 << Shift [count] lines one 'shiftwidth' leftwards. 467 468 *v_<* 469{Visual}[count]< Shift the highlighted lines [count] 'shiftwidth' 470 leftwards (for {Visual} see |Visual-mode|). 471 472 *>* 473 >{motion} Shift {motion} lines one 'shiftwidth' rightwards. 474 475 If the 'shiftwidth' option is set to zero, the amount 476 of indent is calculated at the first non-blank 477 character in the line. 478 *>>* 479 >> Shift [count] lines one 'shiftwidth' rightwards. 480 481 *v_>* 482{Visual}[count]> Shift the highlighted lines [count] 'shiftwidth' 483 rightwards (for {Visual} see |Visual-mode|). 484 485 *:<* 486:[range]< Shift [range] lines one 'shiftwidth' left. Repeat '<' 487 for shifting multiple 'shiftwidth's. 488 489:[range]< {count} Shift {count} lines one 'shiftwidth' left, starting 490 with [range] (default current line |cmdline-ranges|). 491 Repeat '<' for shifting multiple 'shiftwidth's. 492 493:[range]le[ft] [indent] left align lines in [range]. Sets the indent in the 494 lines to [indent] (default 0). 495 496 *:>* 497:[range]> [flags] Shift [range] lines one 'shiftwidth' right. 498 Repeat '>' for shifting multiple 'shiftwidth's. 499 See |ex-flags| for [flags]. 500 501:[range]> {count} [flags] 502 Shift {count} lines one 'shiftwidth' right, starting 503 with [range] (default current line |cmdline-ranges|). 504 Repeat '>' for shifting multiple 'shiftwidth's. 505 See |ex-flags| for [flags]. 506 507The ">" and "<" commands are handy for changing the indentation within 508programs. Use the 'shiftwidth' option to set the size of the white space 509which these commands insert or delete. Normally the 'shiftwidth' option is 8, 510but you can set it to, say, 3 to make smaller indents. The shift leftwards 511stops when there is no indent. The shift right does not affect empty lines. 512 513If the 'shiftround' option is on, the indent is rounded to a multiple of 514'shiftwidth'. 515 516If the 'smartindent' option is on, or 'cindent' is on and 'cinkeys' contains 517'#' with a zero value, shift right does not affect lines starting with '#' 518(these are supposed to be C preprocessor lines that must stay in column 1). 519This can be changed with the 'cino' option, see |cino-#|. 520 521When the 'expandtab' option is off (this is the default) Vim uses <Tab>s as 522much as possible to make the indent. You can use ">><<" to replace an indent 523made out of spaces with the same indent made out of <Tab>s (and a few spaces 524if necessary). If the 'expandtab' option is on, Vim uses only spaces. Then 525you can use ">><<" to replace <Tab>s in the indent by spaces (or use 526`:retab!`). 527 528To move a line several 'shiftwidth's, use Visual mode or the `:` commands. 529For example: > 530 Vjj4> move three lines 4 indents to the right 531 :<<< move current line 3 indents to the left 532 :>> 5 move 5 lines 2 indents to the right 533 :5>> move line 5 2 indents to the right 534 535============================================================================== 5364. Complex changes *complex-change* 537 5384.1 Filter commands *filter* 539 540A filter is a program that accepts text at standard input, changes it in some 541way, and sends it to standard output. You can use the commands below to send 542some text through a filter, so that it is replaced by the filter output. 543Examples of filters are "sort", which sorts lines alphabetically, and 544"indent", which formats C program files (you need a version of indent that 545works like a filter; not all versions do). The 'shell' option specifies the 546shell Vim uses to execute the filter command. You can repeat filter commands 547with ".". Vim does not recognize a comment (starting with '"') after the 548`:!` command. 549 550 *!* 551!{motion}{filter} Filter {motion} text lines through the external 552 program {filter}. 553 554 *!!* 555!!{filter} Filter [count] lines through the external program 556 {filter}. 557 558 *v_!* 559{Visual}!{filter} Filter the highlighted lines through the external 560 program {filter} (for {Visual} see |Visual-mode|). 561 562:{range}![!]{filter} [!][arg] *:range!* 563 For executing external commands see |:!| 564 565 Filter {range} lines through the external program 566 {filter}. Vim replaces the optional bangs with the 567 latest given command and appends the optional [arg]. 568 Vim saves the output of the filter command in a 569 temporary file and then reads the file into the buffer 570 |tempfile|. Vim uses the 'shellredir' option to 571 redirect the filter output to the temporary file. 572 However, if the 'shelltemp' option is off then pipes 573 are used when possible (on Unix). 574 When the 'R' flag is included in 'cpoptions' marks in 575 the filtered lines are deleted, unless the 576 |:keepmarks| command is used. Example: > 577 :keepmarks '<,'>!sort 578< When the number of lines after filtering is less than 579 before, marks in the missing lines are deleted anyway. 580 581 *=* 582={motion} Filter {motion} lines through the external program 583 given with the 'equalprg' option. When the 'equalprg' 584 option is empty (this is the default), use the 585 internal formatting function |C-indenting| and 'lisp'. 586 But when 'indentexpr' is not empty, it will be used 587 instead |indent-expression|. 588 589 *==* 590== Filter [count] lines like with ={motion}. 591 592 *v_=* 593{Visual}= Filter the highlighted lines like with ={motion}. 594 595 596 *tempdir* *tempfile* *setuid* 597Nvim uses temporary files for filtering and generating diffs. Plugins also 598commonly use |tempname()| for their own purposes. On the first request for 599a temporary file, Nvim creates a common directory (the "Nvim tempdir"), to 600serve as storage for all temporary files (including `stdpath("run")` files 601|$XDG_RUNTIME_DIR|) in the current session. 602 603The Nvim tempdir is created in the first available system tempdir: 604 Unix: $TMPDIR, /tmp, current-dir, $HOME. 605 Windows: $TMPDIR, $TMP, $TEMP, $USERPROFILE, current-dir. 606 607On unix the tempdir is created with permissions 0700 (only accessible by the 608current user) to avoid security problems (e.g. symlink attacks). On exit, 609Nvim deletes the tempdir and its contents. 610 *E5431* 611If you see an error or |log| message like: > 612 E5431: tempdir disappeared (2 times) 613this means an external process on your system deleted the Nvim tempdir. 614Typically this is caused by "antivirus" or a misconfigured cleanup service. 615 616If Nvim has the setuid bit set this may cause problems: the temp file 617is owned by the setuid user but the filter command probably runs as the 618original user. 619 620 6214.2 Substitute *:substitute* 622 *:s* *:su* 623:[range]s[ubstitute]/{pattern}/{string}/[flags] [count] 624 For each line in [range] replace a match of {pattern} 625 with {string}. 626 For the {pattern} see |pattern|. 627 {string} can be a literal string, or something 628 special; see |sub-replace-special|. 629 When [range] and [count] are omitted, replace in the 630 current line only. When [count] is given, replace in 631 [count] lines, starting with the last line in [range]. 632 When [range] is omitted start in the current line. 633 *E939* *E1510* 634 [count] must be a positive number (max 2147483647) 635 Also see |cmdline-ranges|. 636 637 See |:s_flags| for [flags]. 638 The delimiter doesn't need to be /, see 639 |pattern-delimiter|. 640 641:[range]s[ubstitute] [flags] [count] 642:[range]&[&][flags] [count] *:&* 643 Repeat last :substitute with same search pattern and 644 substitute string, but without the same flags. You 645 may add [flags], see |:s_flags|. 646 Note that after `:substitute` the '&' and '#' flags 647 can't be used, they're recognized as a pattern 648 separator. 649 The space between `:substitute` and the 'c', 'g', 650 'i', 'I' and 'r' flags isn't required, but in scripts 651 it's a good idea to keep it to avoid confusion. 652 Also see the two and three letter commands to repeat 653 :substitute below |:substitute-repeat|. 654 655:[range]~[&][flags] [count] *:~* 656 Repeat last substitute with same substitute string 657 but with last used search pattern. This is like 658 `:&r`. See |:s_flags| for [flags]. 659 660 *&* 661& Synonym for `:s` (repeat last substitute). Note 662 that the flags are not remembered, thus it might 663 actually work differently. You can use `:&&` to keep 664 the flags. 665 666 *&-default* 667 Mapped to ":&&<CR>" by default. |default-mappings| 668 669 *g&* 670g& Synonym for `:%s//~/&` (repeat last substitute with 671 last search pattern on all lines with the same flags). 672 For example, when you first do a substitution with 673 `:s/pattern/repl/flags` and then `/search` for 674 something else, `g&` will do `:%s/search/repl/flags`. 675 Mnemonic: global substitute. 676 677 *:snomagic* *:sno* 678:[range]sno[magic] ... Same as `:substitute`, but always use 'nomagic'. 679 680 *:smagic* *:sm* 681:[range]sm[agic] ... Same as `:substitute`, but always use 'magic'. 682 683 *:s_flags* 684The flags that you can use for the substitute commands: 685 686 *:&&* 687[&] Must be the first one: Keep the flags from the previous substitute 688 command. Examples: > 689 :&& 690 :s/this/that/& 691< Note that `:s` and `:&` don't keep the flags. 692 693[c] Confirm each substitution. Vim highlights the matching string (with 694 |hl-IncSearch|). You can type: *:s_c* 695 'y' to substitute this match 696 'l' to substitute this match and then quit ("last") 697 'n' to skip this match 698 <Esc> to quit substituting 699 'a' to substitute this and all remaining matches 700 'q' to quit substituting 701 CTRL-E to scroll the screen up 702 CTRL-Y to scroll the screen down 703 704 *:s_e* 705[e] When the search pattern fails, do not issue an error message and, in 706 particular, continue in maps as if no error occurred. This is most 707 useful to prevent the "No match" error from breaking a mapping. Vim 708 does not suppress the following error messages, however: 709 Regular expressions can't be delimited by letters 710 \ should be followed by /, ? or & 711 No previous substitute regular expression 712 Trailing characters 713 Interrupted 714 715 *:s_g* 716[g] Replace all occurrences in the line. Without this argument, 717 replacement occurs only for the first occurrence in each line. If the 718 'gdefault' option is on, this flag is on by default and the [g] 719 argument switches it off. 720 721 *:s_i* 722[i] Ignore case for the pattern. The 'ignorecase' and 'smartcase' options 723 are not used. 724 725 *:s_I* 726[I] Don't ignore case for the pattern. The 'ignorecase' and 'smartcase' 727 options are not used. 728 729 *:s_n* 730[n] Report the number of matches, do not actually substitute. The [c] 731 flag is ignored. The matches are reported as if 'report' is zero. 732 Useful to |count-items|. 733 If \= |sub-replace-expression| is used, the expression will be 734 evaluated in the |sandbox| at every match. 735 736[p] Print the line containing the last substitute. *:s_p* 737 738[#] Like [p] and prepend the line number. *:s_#* 739 740[l] Like [p] but print the text like |:list|. *:s_l* 741 742 *:s_r* 743[r] Only useful in combination with `:&` or `:s` without arguments. `:&r` 744 works the same way as `:~`: When the search pattern is empty, use the 745 previously used search pattern instead of the search pattern from the 746 last substitute or `:global`. If the last command that did a search 747 was a substitute or `:global`, there is no effect. If the last 748 command was a search command such as "/", use the pattern from that 749 command. 750 For `:s` with an argument this already happens: > 751 :s/blue/red/ 752 /green 753 :s//red/ or :~ or :&r 754< The last commands will replace "green" with "red". > 755 :s/blue/red/ 756 /green 757 :& 758< The last command will replace "blue" with "red". 759 760Note that there is no flag to change the "magicness" of the pattern. A 761different command is used instead, or you can use |/\v| and friends. The 762reason is that the flags can only be found by skipping the pattern, and in 763order to skip the pattern the "magicness" must be known. Catch 22! 764 765If the {pattern} for the substitute command is empty, the command uses the 766pattern from the last substitute or `:global` command. If there is none, but 767there is a previous search pattern, that one is used. With the [r] flag, the 768command uses the pattern from the last substitute, `:global`, or search 769command. 770 771If the {string} is omitted the substitute is done as if it's empty. Thus the 772matched pattern is deleted. The separator after {pattern} can also be left 773out then. Example: > 774 :%s/TESTING 775This deletes "TESTING" from all lines, but only one per line. 776 777For compatibility with Vi these two exceptions are allowed: 778"\/{string}/" and "\?{string}?" do the same as "//{string}/r". 779"\&{string}&" does the same as "//{string}/". 780 *pattern-delimiter* *E146* 781Instead of the '/' which surrounds the pattern and replacement string, you can 782use another single-byte character. This is useful if you want to include a 783'/' in the search pattern or replacement string. Example: > 784 :s+/+//+ 785 786You can use most characters, but not an alphanumeric character, '\', '"' or 787'|'. 788 789For the definition of a pattern, see |pattern|. In Visual block mode, use 790|/\%V| in the pattern to have the substitute work in the block only. 791Otherwise it works on whole lines anyway. 792 793 *sub-replace-special* *:s\=* 794When the {string} starts with "\=" it is evaluated as an expression, see 795|sub-replace-expression|. You can use that for complex replacement or special 796characters. 797 798The substitution is limited in recursion to 4 levels. *E1290* 799 800Otherwise these characters in {string} have a special meaning: 801 802magic nomagic action ~ 803 & \& replaced with the whole matched pattern *s/\&* 804 \& & replaced with & 805 \0 replaced with the whole matched pattern *\0* *s/\0* 806 \1 replaced with the matched pattern in the first 807 pair of () *s/\1* 808 \2 replaced with the matched pattern in the second 809 pair of () *s/\2* 810 .. .. *s/\3* 811 \9 replaced with the matched pattern in the ninth 812 pair of () *s/\9* 813 ~ \~ replaced with the {string} of the previous 814 substitute *s~* 815 \~ ~ replaced with ~ *s/\~* 816 \u next character made uppercase *s/\u* 817 \U following characters made uppercase, until \E *s/\U* 818 \l next character made lowercase *s/\l* 819 \L following characters made lowercase, until \E *s/\L* 820 \e end of \u, \U, \l and \L (NOTE: not <Esc>!) *s/\e* 821 \E end of \u, \U, \l and \L *s/\E* 822 <CR> split line in two at this point 823 (Type the <CR> as CTRL-V <Enter>) *s<CR>* 824 \r idem *s/\r* 825 \<CR> insert a carriage-return (CTRL-M) 826 (Type the <CR> as CTRL-V <Enter>) *s/\<CR>* 827 \n insert a <NL> (<NUL> in the file) 828 (does NOT break the line) *s/\n* 829 \b insert a <BS> *s/\b* 830 \t insert a <Tab> *s/\t* 831 \\ insert a single backslash *s/\\* 832 \x where x is any character not mentioned above: 833 Reserved for future expansion 834 835The special meaning is also used inside the third argument {sub} of 836the |substitute()| function with the following exceptions: 837 - A % inserts a percent literally without regard to 'cpoptions'. 838 - magic is always set without regard to 'magic'. 839 - A ~ inserts a tilde literally. 840 - <CR> and \r inserts a carriage-return (CTRL-M). 841 - \<CR> does not have a special meaning. It's just one of \x. 842 843Examples: > 844 :s/a\|b/xxx\0xxx/g modifies "a b" to "xxxaxxx xxxbxxx" 845 :s/\([abc]\)\([efg]\)/\2\1/g modifies "af fa bg" to "fa fa gb" 846 :s/abcde/abc^Mde/ modifies "abcde" to "abc", "de" (two lines) 847 :s/$/\^M/ modifies "abcde" to "abcde^M" 848 :s/\w\+/\u\0/g modifies "bla bla" to "Bla Bla" 849 :s/\w\+/\L\u\0/g modifies "BLA bla" to "Bla Bla" 850 851Note: "\L\u" can be used to capitalize the first letter of a word. This is 852not compatible with Vi and older versions of Vim, where the "\u" would cancel 853out the "\L". Same for "\U\l". 854 855Note: In previous versions CTRL-V was handled in a special way. Since this is 856not Vi compatible, this was removed. Use a backslash instead. 857 858command text result ~ 859:s/aa/a^Ma/ aa a<line-break>a 860:s/aa/a\^Ma/ aa a^Ma 861:s/aa/a\\^Ma/ aa a\<line-break>a 862 863(you need to type CTRL-V <CR> to get a ^M here) 864 865The numbering of "\1", "\2" etc. is done based on which "\(" comes first in 866the pattern (going left to right). When a parentheses group matches several 867times, the last one will be used for "\1", "\2", etc. Example: > 868 :s/\(\(a[a-d] \)*\)/\2/ modifies "aa ab x" to "ab x" 869The "\2" is for "\(a[a-d] \)". At first it matches "aa ", secondly "ab ". 870 871When using parentheses in combination with '|', like in \([ab]\)\|\([cd]\), 872either the first or second pattern in parentheses did not match, so either 873\1 or \2 is empty. Example: > 874 :s/\([ab]\)\|\([cd]\)/\1x/g modifies "a b c d" to "ax bx x x" 875< 876 877 *:sc* *:sce* *:scg* *:sci* *:scI* *:scl* *:scp* *:sg* *:sgc* 878 *:sge* *:sgi* *:sgI* *:sgl* *:sgn* *:sgp* *:sgr* *:sI* *:si* 879 *:sic* *:sIc* *:sie* *:sIe* *:sIg* *:sIl* *:sin* *:sIn* *:sIp* 880 *:sip* *:sIr* *:sir* *:sr* *:src* *:srg* *:sri* *:srI* *:srl* 881 *:srn* *:srp* *:substitute-repeat* 8822-letter and 3-letter :substitute commands ~ 883 884These commands repeat the previous `:substitute` command with the given flags. 885The first letter is always "s", followed by one or two of the possible flag 886characters. For example ":sce" works like ":s ce". The table lists the 887possible combinations, not all flags are possible, because the command is 888short for another command. 889 890 List of :substitute commands 891 | c e g i I n p l r 892 | c :sc :sce :scg :sci :scI :scn :scp :scl 893 | e 894 | g :sgc :sge :sg :sgi :sgI :sgn :sgp :sgl :sgr 895 | i :sic :sie :si :siI :sin :sip :sir 896 | I :sIc :sIe :sIg :sIi :sI :sIn :sIp :sIl :sIr 897 | n 898 | p 899 | l 900 | r :src :srg :sri :srI :srn :srp :srl :sr 901 902Exceptions: 903 :scr is `:scriptnames` 904 :se is `:set` 905 :sig is `:sign` 906 :sil is `:silent` 907 :sn is `:snext` 908 :sp is `:split` 909 :sl is `:sleep` 910 :sre is `:srewind` 911 912 913Substitute with an expression *sub-replace-expression* 914 *sub-replace-\=* *s/\=* 915When the substitute string starts with "\=" the remainder is interpreted as an 916expression. 917 918The special meaning for characters as mentioned at |sub-replace-special| does 919not apply except for "<CR>". A <NL> character is used as a line break, you 920can get one with a double-quote string: "\n". Prepend a backslash to get a 921real <NL> character (which will be a NUL in the file). 922 923The "\=" notation can also be used inside the third argument {sub} of 924|substitute()| function. In this case, the special meaning for characters as 925mentioned at |sub-replace-special| does not apply at all. Especially, <CR> and 926<NL> are interpreted not as a line break but as a carriage-return and a 927new-line respectively. 928 929When the result is a |List| then the items are joined with separating line 930breaks. Thus each item becomes a line, except that they can contain line 931breaks themselves. 932 933The |submatch()| function can be used to obtain matched text. The whole 934matched text can be accessed with "submatch(0)". The text matched with the 935first pair of () with "submatch(1)". Likewise for further sub-matches in (). 936 937Be careful: The separation character must not appear in the expression! 938Consider using a character like "@" or ":". There is no problem if the result 939of the expression contains the separation character. 940 941Examples: > 942 :s@\n@\="\r" .. expand("$HOME") .. "\r"@ 943This replaces an end-of-line with a new line containing the value of $HOME. > 944 945 s/E/\="\<Char-0x20ac>"/g 946This replaces each 'E' character with a euro sign. Read more in |<Char->|. 947 948 9494.3 Changing tabs *change-tabs* 950 *:ret* *:retab* *:retab!* 951:[range]ret[ab][!] [-indentonly] [{new-tabstop}] 952 Replace all sequences of white-space containing a 953 <Tab> with new strings of white-space using 954 {new-tabstop}. If you do not specify {new-tabstop} or 955 it is zero, Vim uses the current value of 'tabstop'. 956 The current value of 'tabstop' is always used to 957 compute the width of existing tabs. 958 With !, Vim also replaces strings of only normal 959 spaces with tabs where appropriate. 960 With 'expandtab' on, Vim replaces all tabs with the 961 appropriate number of spaces. 962 This command sets 'tabstop' to {new-tabstop} and if 963 performed on the whole file, which is default, should 964 not make any visible change. 965 966 When [-indentonly] is specified, only the leading 967 white-space will be targeted. Any other consecutive 968 white-space will not be changed. 969 970 Warning: This command modifies any <Tab> characters 971 inside of strings in a C program. Use "\t" to avoid 972 this (that's a good habit anyway). 973 `:retab!` may also change a sequence of spaces by 974 <Tab> characters, which can mess up a printf(). 975 A list of tab widths separated by commas may be used 976 in place of a single tabstop. Each value in the list 977 represents the width of one tabstop, except the final 978 value which applies to all following tabstops. 979 980 *retab-example* 981Example for using autocommands and ":retab" to edit a file which is stored 982with tabstops at 8 but edited with tabstops set at 4. Warning: white space 983inside of strings can change! Also see 'softtabstop' option. > 984 985 :auto BufReadPost *.xx retab! 4 986 :auto BufWritePre *.xx retab! 8 987 :auto BufWritePost *.xx retab! 4 988 :auto BufNewFile *.xx set ts=4 989 990============================================================================== 9915. Copying and moving text *copy-move* 992 993 *quote* 994"{register} Use {register} for next delete, yank or put. Use 995 an uppercase character to append with delete and yank. 996 Registers ".", "%", "#" and ":" only work with put. 997 998 *:reg* *:registers* 999:reg[isters] Display the type and contents of all numbered and 1000 named registers. If a register is written to for 1001 |:redir| it will not be listed. 1002 Type can be one of: 1003 "c" for |characterwise| text 1004 "l" for |linewise| text 1005 "b" for |blockwise-visual| text 1006 1007 1008:reg[isters] {arg} Display the contents of the numbered and named 1009 registers that are mentioned in {arg}. For example: > 1010 :reg 1a 1011< to display registers '1' and 'a'. Spaces are allowed 1012 in {arg}. 1013 1014 *:di* *:dis* *:display* 1015:di[splay] [arg] Same as :registers. 1016 1017 *y* *yank* 1018["x]y{motion} Yank {motion} text [into register x]. When no 1019 characters are to be yanked (e.g., "y0" in column 1), 1020 this is an error when 'cpoptions' includes the 'E' 1021 flag. 1022 1023 *yy* 1024["x]yy Yank [count] lines [into register x] |linewise|. 1025 1026 *Y* 1027["x]Y yank [count] lines [into register x] (synonym for 1028 yy, |linewise|). 1029 *Y-default* 1030 Mapped to "y$" by default. |default-mappings| 1031 1032 *zy* 1033["x]zy{motion} Yank {motion} text [into register x]. Only differs 1034 from `y` when selecting a block of text, see |v_zy|. 1035 1036 *v_y* 1037{Visual}["x]y Yank the highlighted text [into register x] (for 1038 {Visual} see |Visual-mode|). 1039 1040 *v_Y* 1041{Visual}["x]Y Yank the highlighted lines [into register x] (for 1042 {Visual} see |Visual-mode|). 1043 1044 *v_zy* 1045{Visual}["x]zy Yank the highlighted text [into register x]. Trailing 1046 whitespace at the end of each line of a selected block 1047 won't be yanked. Especially useful in combination 1048 with `zp`. (for {Visual} see |Visual-mode|) 1049 1050 *:y* *:yank* *E850* 1051:[range]y[ank] [x] Yank [range] lines [into register x]. 1052 1053:[range]y[ank] [x] {count} 1054 Yank {count} lines, starting with last line number 1055 in [range] (default: current line |cmdline-ranges|), 1056 [into register x]. 1057 1058 *p* *put* *E353* *E1240* 1059["x]p Put the text [from register x] after the cursor 1060 [count] times. 1061 1062 *P* 1063["x]P Put the text [from register x] before the cursor 1064 [count] times. 1065 1066 *<MiddleMouse>* 1067["x]<MiddleMouse> Put the text from a register before the cursor [count] 1068 times. Uses the "* register, unless another is 1069 specified. 1070 Leaves the cursor at the end of the new text. 1071 Using the mouse only works when 'mouse' contains 'n' 1072 or 'a'. 1073 If you have a scrollwheel and often accidentally paste 1074 text, you can use these mappings to disable the 1075 pasting with the middle mouse button: > 1076 :map <MiddleMouse> <Nop> 1077 :imap <MiddleMouse> <Nop> 1078< You might want to disable the multi-click versions 1079 too, see |double-click|. 1080 1081 *gp* 1082["x]gp Just like "p", but leave the cursor just after the new 1083 text. 1084 1085 *gP* 1086["x]gP Just like "P", but leave the cursor just after the new 1087 text. 1088 1089 *:pu* *:put* 1090:[line]pu[t] [x] Put the text [from register x] after [line] (default 1091 current line). This always works |linewise|, thus 1092 this command can be used to put a yanked block as new 1093 lines. 1094 If no register is specified, it depends on the 1095 'clipboard' option: If 'clipboard' contains 1096 "unnamedplus", paste from the + register |quoteplus|. 1097 Otherwise, if 'clipboard' contains "unnamed", paste 1098 from the * register |quotestar|. Otherwise, paste from 1099 the unnamed register |quote_quote|. 1100 The register can also be '=' followed by an optional 1101 expression. The expression continues until the end of 1102 the command. You need to escape the '|' and '"' 1103 characters to prevent them from terminating the 1104 command. Example: > 1105 :put ='path' .. \",/test\" 1106< If there is no expression after '=', Vim uses the 1107 previous expression. You can see it with ":dis =". 1108 1109:[line]pu[t]! [x] Put the text [from register x] before [line] (default 1110 current line). 1111 1112 *:ip* *:iput* 1113:[line]ip[ut] [x] Like |:put|, but adjust indent to the current line. 1114 1115:[line]ip[ut]! [x] Like |:put|!, but adjust indent to the current line. 1116 1117["x]]p or *]p* *]<MiddleMouse>* 1118["x]]<MiddleMouse> Like "p", but adjust the indent to the current line. 1119 Using the mouse only works when 'mouse' contains 'n' 1120 or 'a'. 1121 1122["x][P or *[P* 1123["x]]P or *]P* 1124["x][p or *[p* *[<MiddleMouse>* 1125["x][<MiddleMouse> Like "P", but adjust the indent to the current line. 1126 Using the mouse only works when 'mouse' contains 'n' 1127 or 'a'. 1128 1129["x]zp or *zp* *zP* 1130["x]zP Like "p" and "P", except without adding trailing 1131 spaces when pasting a block. Thus the inserted text 1132 will not always be a rectangle. Especially useful in 1133 combination with |v_zy|. 1134 1135You can use these commands to copy text from one place to another. Do this 1136by first getting the text into a register with a yank, delete or change 1137command, then inserting the register contents with a put command. You can 1138also use these commands to move text from one file to another, because Vim 1139preserves all registers when changing buffers (the CTRL-^ command is a quick 1140way to toggle between two files). 1141 1142 *linewise-register* *charwise-register* 1143You can repeat the put commands with "." (except for :put) and undo them. If 1144the command that was used to get the text into the register was |linewise|, 1145Vim inserts the text below ("p") or above ("P") the line where the cursor is. 1146Otherwise Vim inserts the text after ("p") or before ("P") the cursor. With 1147the ":put" command, Vim always inserts the text in the next line. You can 1148exchange two characters with the command sequence "xp". You can exchange two 1149lines with the command sequence "ddp". You can exchange two words with the 1150command sequence "deep" (start with the cursor in the blank space before the 1151first word). You can use the |']| or |`]| command after the put command to 1152move the cursor to the end of the inserted text, or use |'[| or |`[| to move 1153the cursor to the start. 1154 1155 *put-Visual-mode* 1156When using a put command like |p| or |P| in Visual mode, Vim will try to 1157replace the selected text with the contents of the register. How this 1158works depends on the type of selection and the text. With blockwise selection 1159it also depends on the size of the block and whether the corners are on an 1160existing character. (Implementation detail: it actually works by first 1161putting the register after the selection and then deleting the selection.) 1162 *v_p* 1163|p| in Visual mode puts text and sets the default register (unnamed, 1164selection, or clipboard) to the previously-selected text. Useful if you want 1165to put that text somewhere else. But you cannot repeat the same change. 1166 *v_P* 1167|P| in Visual mode puts text without setting the default register. You can 1168repeat the change, but the deleted text cannot be used. If you do need it you 1169can use |p| with another register. E.g., yank the text to copy, Visually 1170select the text to replace and use "0p . You can repeat this as many times as 1171you like, and the unnamed register will be changed each time. 1172 *blockwise-put* 1173When a register contains text from one line (characterwise), using a 1174blockwise Visual selection, putting that register will paste that text 1175repeatedly in each of the selected lines, thus replacing the blockwise 1176selected region by multiple copies of the register text. For example: 1177 - yank the word "TEXT" into a register with `yw` 1178 - select a visual block, marked with "v" in this text: 1179 aaavvaaa 1180 bbbvvbbb 1181 cccvvccc 1182 - press `p`, results in: 1183 aaaTEXTaaa 1184 bbbTEXTbbb 1185 cccTEXTccc 1186 1187 *blockwise-register* 1188If you use a blockwise Visual mode command to get the text into the register, 1189the block of text will be inserted before ("P") or after ("p") the cursor 1190column in the current and next lines. Vim makes the whole block of text start 1191in the same column. Thus the inserted text looks the same as when it was 1192yanked or deleted. Vim may replace some <Tab> characters with spaces to make 1193this happen. However, if the width of the block is not a multiple of a <Tab> 1194width and the text after the inserted block contains <Tab>s, that text may be 1195misaligned. 1196 1197Use |zP|/|zp| to paste a blockwise yanked register without appending trailing 1198spaces. 1199 1200Note that after a charwise yank command, Vim leaves the cursor on the first 1201yanked character that is closest to the start of the buffer. This means that 1202"yl" doesn't move the cursor, but "yh" moves the cursor one character left. 1203Rationale: In Vi the "y" command followed by a backwards motion would 1204 sometimes not move the cursor to the first yanked character, 1205 because redisplaying was skipped. In Vim it always moves to 1206 the first character, as specified by Posix. 1207With a linewise yank command the cursor is put in the first line, but the 1208column is unmodified, thus it may not be on the first yanked character. 1209 1210============================================================================= 1211Registers *registers* *{register}* *E354* 1212 1213There are ten types of registers: 12141. The unnamed register "" 12152. 10 numbered registers "0 to "9 12163. The small delete register "- 12174. 26 named registers "a to "z or "A to "Z 12185. Three read-only registers ":, "., "% 12196. Alternate buffer register "# 12207. The expression register "= 12218. The selection registers "* and "+ 12229. The black hole register "_ 122310. Last search pattern register "/ 1224 1225----------------------------------------------------------------------------- 12261. Unnamed register "" *quote_quote* *quotequote* 1227 1228Vim fills this register with text deleted with the "d", "c", "s", "x" commands 1229or copied with the yank "y" command, regardless of whether or not a specific 1230register was used (e.g. "xdd). This is like the unnamed register is pointing 1231to the last used register. Thus when appending using an uppercase register 1232name, the unnamed register contains the same text as the named register. 1233An exception is the '_' register: "_dd does not store the deleted text in any 1234register. 1235Vim uses the contents of the unnamed register for any put command (p or P) 1236which does not specify a register. Additionally you can access it with the 1237name '"'. This means you have to type two double quotes. Writing to the "" 1238register writes to register "0. 1239 1240----------------------------------------------------------------------------- 12412. Numbered registers "0 to "9 *quote_number* 1242 *quote0* *quote1* *quote2* *quote3* *quote4* *quote9* 1243 1244Vim fills these registers with text from yank and delete commands. 1245Numbered register 0 contains the text from the most recent yank command, 1246unless the command specified another register with ["x]. 1247 1248Numbered register 1 contains the text deleted by the most recent delete or 1249change command (even when the command specified another register), unless the 1250text is less than one line (the small delete register is used then). An 1251exception is made for the delete operator with these movement commands: |%|, 1252|(|, |)|, |`|, |/|, |?|, |n|, |N|, |{| and |}|. 1253 1254Register "1 is always used then (this is Vi compatible). The "- register is 1255used as well if the delete is within a line. Note that these characters may 1256be mapped. E.g. |%| is mapped by the matchit plugin. 1257 1258With each successive deletion or change, Vim shifts the previous contents 1259of register 1 into register 2, 2 into 3, and so forth, losing the previous 1260contents of register 9. 1261 *yankring* 1262To also store yanks (not only deletions) in registers 1-9, try this: >lua 1263 -- Yank-ring: store yanked text in registers 1-9. 1264 vim.api.nvim_create_autocmd('TextYankPost', { 1265 callback = function() 1266 if vim.v.event.operator == 'y' then 1267 for i = 9, 1, -1 do -- Shift all numbered registers. 1268 vim.fn.setreg(tostring(i), vim.fn.getreg(tostring(i - 1))) 1269 end 1270 end 1271 end, 1272 }) 1273< 1274----------------------------------------------------------------------------- 12753. Small delete register "- *quote_-* *quote-* 1276 1277This register contains text from commands that delete less than one line, 1278except when the command specifies a register with ["x]. 1279 1280----------------------------------------------------------------------------- 12814. Named registers "a to "z or "A to "Z *quote_alpha* *quotea* 1282 1283Vim fills these registers only when you say so. Specify them as lowercase 1284letters to replace their previous contents or as uppercase letters to append 1285to their previous contents. When the '>' flag is present in 'cpoptions' then 1286a line break is inserted before the appended text. 1287 1288----------------------------------------------------------------------------- 12895. Read-only registers ":, ". and "% 1290 1291These are '%', ':' and '.'. You can use them only with the "p", "P", 1292and ":put" commands and with CTRL-R. 1293 1294 *quote_.* *quote.* *E29* 1295 - ". Contains the last inserted text (the same as what is inserted 1296 with the insert mode commands CTRL-A and CTRL-@). Note: this doesn't 1297 work with CTRL-R on the command-line. It works a bit differently, 1298 like inserting the text instead of putting it ('textwidth' and other 1299 options affect what is inserted). 1300 *quote_%* *quote%* 1301 - "% Contains the name of the current file. 1302 *quote_:* *quote:* *E30* 1303 - ": Contains the most recent executed command-line. Example: Use 1304 "@:" to repeat the previous command-line command. The command-line is 1305 only stored in this register when at least one character of it was 1306 typed. Thus it remains unchanged if the command was executed 1307 completely from a mapping. 1308 1309----------------------------------------------------------------------------- 13106. Alternate file register "# *quote_#* *quote#* *@#* 1311 1312Contains the |alternate-file| name for current window. 1313This register is writable with `:let` and |setreg()|. This changes which 1314buffer CTRL-^ enters. You can't yank or delete into this register. 1315A String is matched against existing buffer names, like |:buffer|: > 1316 let @# = 'buffer_name' 1317Also supports using buffer number and |file-pattern|. 1318 1319Throws 1320 |E86| when the buffer number does not exist 1321 |E93| when more than one buffer matches 1322 |E94| when none match 1323 1324Clear the register with empty String: > 1325 let @# = '' 1326 1327----------------------------------------------------------------------------- 13287. Expression register "= *quote_=* *quote=* *@=* 1329 1330This is not really a register that stores text, but is a way to use an 1331expression in commands which use a register. The expression register is 1332read-write. 1333 1334- When typing the '=' after " or CTRL-R the cursor moves to the command-line, 1335 where you can enter any expression (see |expression|). All normal 1336 command-line editing commands are available, including a special history for 1337 expressions. When you end the command-line by typing <CR>, Vim computes the 1338 result of the expression. If you end it with <Esc>, Vim abandons the 1339 expression. If you do not enter an expression, Vim uses the previous 1340 expression (like with the "/" command). 1341- The expression must evaluate to a String. A Number is always automatically 1342 converted to a String. For the "p" and ":put" command, if the result is 1343 a Float it's converted into a String. If the result is a List each element 1344 is turned into a String and used as a line. A Dictionary is converted into 1345 a String. A Funcref results in an error message (use string() to convert). 1346- If the "= register is used for the "p" command, the String is split up at 1347 <NL> characters. If the String ends in a <NL>, it is regarded as a linewise 1348 register. 1349 1350----------------------------------------------------------------------------- 13518. Selection registers "* and "+ 1352 1353Use these registers for storing and retrieving the selected text for the GUI. 1354See |quotestar| and |quoteplus|. When the clipboard is not available or not 1355working, the unnamed register is used instead. For Unix systems and Mac OS X, 1356see |primary-selection|. 1357 1358----------------------------------------------------------------------------- 13599. Black hole register "_ *quote_* 1360 1361When writing to this register, nothing happens. This can be used to delete 1362text without affecting the normal registers. When reading from this 1363register, nothing is returned. 1364 1365----------------------------------------------------------------------------- 136610. Last search pattern register "/ *quote_/* *quote/* 1367 1368Contains the most recent search-pattern. This is used for "n" and 'hlsearch'. 1369This register is writable with `:let` and |setreg()|. You can change it to have 1370'hlsearch' highlight other matches without actually searching. You can't yank 1371or delete into this register. The search direction is available in 1372|v:searchforward|. 1373Note that the value is restored when returning from a function 1374|function-search-undo|. 1375 1376 *@/* 1377You can write to a register with a `:let` command |:let-@|. Example: > 1378 :let @/ = "the" 1379 1380If you use a put command without specifying a register, Vim uses the register 1381that was last filled (this is also the contents of the unnamed register). If 1382you are confused, use the `:dis` command to find out what Vim will put (this 1383command displays all named and numbered registers; the unnamed register is 1384labelled '"'). 1385 1386The next three commands always work on whole lines. 1387 1388:[range]co[py] {address} *:co* *:copy* 1389 Copy the lines given by [range] to below the line 1390 given by {address}. 1391 1392 *:t* 1393:t Synonym for copy. 1394 1395:[range]m[ove] {address} *:m* *:mo* *:move* *E134* 1396 Move the lines given by [range] to below the line 1397 given by {address}. 1398 1399============================================================================== 14006. Formatting text *formatting* 1401 1402:[range]ce[nter] [width] *:ce* *:center* 1403 Center lines in [range] between [width] columns 1404 (default 'textwidth' or 80 when 'textwidth' is 0). 1405 1406:[range]ri[ght] [width] *:ri* *:right* 1407 Right-align lines in [range] at [width] columns 1408 (default 'textwidth' or 80 when 'textwidth' is 0). 1409 1410 *:le* *:left* 1411:[range]le[ft] [indent] 1412 Left-align lines in [range]. Sets the indent in the 1413 lines to [indent] (default 0). 1414 1415 *gq* 1416gq{motion} Format the lines that {motion} moves over. 1417 Formatting is done with one of three methods: 1418 1. If 'formatexpr' is not empty the expression is 1419 evaluated. This can differ for each buffer. 1420 2. If 'formatprg' is not empty an external program 1421 is used. 1422 3. Otherwise formatting is done internally. 1423 1424 In the third case the 'textwidth' option controls the 1425 length of each formatted line (see below). 1426 If the 'textwidth' option is 0, the formatted line 1427 length is the screen width (with a maximum width of 1428 79). 1429 The 'formatoptions' option controls the type of 1430 formatting |fo-table|. 1431 The cursor is left on the first non-blank of the last 1432 formatted line. 1433 1434gqgq *gqgq* *gqq* 1435gqq Format the current line. With a count format that 1436 many lines. 1437 1438 *v_gq* 1439{Visual}gq Format the highlighted text. (for {Visual} see 1440 |Visual-mode|). 1441 1442 *gw* 1443gw{motion} Format the lines that {motion} moves over. Similar to 1444 |gq| but puts the cursor back at the same position in 1445 the text. However, 'formatprg' and 'formatexpr' are 1446 not used. 1447 1448gwgw *gwgw* *gww* 1449gww Format the current line as with "gw". 1450 1451 *v_gw* 1452{Visual}gw Format the highlighted text as with "gw". (for 1453 {Visual} see |Visual-mode|). 1454 1455Example: To format the current paragraph use: *gqap* > 1456 gqap 1457 1458The "gq" command leaves the cursor in the line where the motion command takes 1459the cursor. This allows you to repeat formatting repeated with ".". This 1460works well with "gqj" (format current and next line) and "gq}" (format until 1461end of paragraph). Note: When 'formatprg' is set, "gq" leaves the cursor on 1462the first formatted line (as with using a filter command). 1463 1464If you want to format the current paragraph and continue where you were, use: > 1465 gwap 1466If you always want to keep paragraphs formatted you may want to add the 'a' 1467flag to 'formatoptions'. See |auto-format|. 1468 1469If the 'autoindent' option is on, Vim uses the indent of the first line for 1470the following lines. 1471 1472Formatting does not change empty lines (but it does change lines with only 1473white space!). 1474 1475The 'joinspaces' option is used when lines are joined together. 1476 1477You can set the 'formatexpr' option to an expression or the 'formatprg' option 1478to the name of an external program for Vim to use for text formatting. The 1479'textwidth' and other options have no effect on formatting by an external 1480program. 1481 1482 *format-formatexpr* 1483The 'formatexpr' option can be set to a Vim script function that performs 1484reformatting of the buffer. This should usually happen in an |ftplugin|, 1485since formatting is highly dependent on the type of file. It makes 1486sense to use an |autoload| script, so the corresponding script is only loaded 1487when actually needed and the script should be called <filetype>format.vim. 1488 1489For example, the XML filetype plugin distributed with Vim in the 1490$VIMRUNTIME/ftplugin directory, sets the 'formatexpr' option to: > 1491 1492 setlocal formatexpr=xmlformat#Format() 1493 1494That means, you will find the corresponding script, defining the 1495xmlformat#Format() function, in the file `$VIMRUNTIME/autoload/xmlformat.vim` 1496 1497Here is an example that removes trailing whitespace from the selected text. 1498Put it in your |ftplugin|: 1499>lua 1500 vim.bo.formatexpr = function() 1501 -- only reformat on explicit gq command 1502 if vim.fn.mode() ~= 'n' then 1503 -- fall back to the internal reformatting 1504 return 1 1505 end 1506 local lines = vim.fn.getline(vim.v.lnum, vim.v.lnum + vim.v.count - 1) 1507 for i, val in ipairs(lines) do 1508 lines[i] = (val:gsub('%s+$', '')) 1509 end 1510 vim.fn.setline('.', lines) 1511 1512 -- do not run internal formatter! 1513 return 0 1514 end 1515 1516Note: this function explicitly returns non-zero when called from insert mode 1517(which basically means, text is inserted beyond the 'textwidth' limit). This 1518causes Vim to fall back to reformat the text by using the internal formatter. 1519 1520However, if the |gq| command is used to reformat the text, the function 1521will receive the selected lines, trim trailing whitespace from those lines and 1522put them back in place. If you are going to split single lines into multiple 1523lines, be careful not to overwrite anything. 1524 1525If you want to allow reformatting of text from insert or replace mode, one has 1526to be very careful, because the function might be called recursively. For 1527debugging it helps to set the 'debug' option. 1528 1529 *right-justify* 1530There is no command in Vim to right justify text. You can do it with 1531an external command, like "par" (e.g.: `:.,}!par` to format until the end of the 1532paragraph) or set 'formatprg' to "par". 1533 1534 *format-comments* 1535An overview of comment formatting is in section |30.6| of the user manual. 1536 1537Vim can automatically insert and format comments in a special way. Vim 1538recognizes a comment by a specific string at the start of the line (ignoring 1539white space). Three types of comments can be used: 1540 1541- A comment string that repeats at the start of each line. An example is the 1542 type of comment used in shell scripts, starting with "#". 1543- A comment string that occurs only in the first line, not in the following 1544 lines. An example is this list with dashes. 1545- Three-piece comments that have a start string, an end string, and optional 1546 lines in between. The strings for the start, middle and end are different. 1547 An example is the C style comment: > 1548 /* 1549 * this is a C comment 1550 */ 1551 1552The 'comments' option is a comma-separated list of parts. Each part defines a 1553type of comment string. A part consists of: 1554 {flags}:{string} 1555 1556{string} is the literal text that must appear. 1557 1558{flags}: 1559 n Nested comment. Nesting with mixed parts is allowed. If 'comments' 1560 is "n:),n:>" a line starting with "> ) >" is a comment. 1561 1562 b Blank (<Space>, <Tab> or <EOL>) required after {string}. 1563 1564 f Only the first line has the comment string. Do not repeat comment on 1565 the next line, but preserve indentation (e.g., a bullet-list). 1566 1567 s Start of three-piece comment 1568 1569 m Middle of a three-piece comment 1570 1571 e End of a three-piece comment 1572 1573 l Left align. Used together with 's' or 'e', the leftmost character of 1574 start or end will line up with the leftmost character from the middle. 1575 This is the default and can be omitted. See below for more details. 1576 1577 r Right align. Same as above but rightmost instead of leftmost. See 1578 below for more details. 1579 1580 O Don't consider this comment for the "O" command. 1581 1582 x Allows three-piece comments to be ended by just typing the last 1583 character of the end-comment string as the first action on a new 1584 line when the middle-comment string has been inserted automatically. 1585 See below for more details. 1586 1587 {digits} 1588 When together with 's' or 'e': add {digit} amount of offset to an 1589 automatically inserted middle or end comment leader. The offset 1590 begins from a left alignment. See below for more details. 1591 1592 -{digits} 1593 Like {digits} but reduce the indent. This only works when there is 1594 some indent for the start or end part that can be removed. 1595 1596When a string has none of the 'f', 's', 'm' or 'e' flags, Vim assumes the 1597comment string repeats at the start of each line. The {flags} field may be 1598empty. 1599 1600Any blank space in the text before and after the {string} is part of the 1601{string}, so do not include leading or trailing blanks unless the blanks are a 1602required part of the comment string. 1603 1604When one comment leader is part of another, specify the part after the whole. 1605For example, to include both "-" and "->", use > 1606 :set comments=f:->,f:- 1607 1608A three-piece comment must always be given as start,middle,end, with no other 1609parts in between. An example of a three-piece comment is > 1610 sr:/*,mb:*,ex:*/ 1611for C-comments. To avoid recognizing "*ptr" as a comment, the middle string 1612includes the 'b' flag. For three-piece comments, Vim checks the text after 1613the start and middle strings for the end string. If Vim finds the end string, 1614the comment does not continue on the next line. Three-piece comments must 1615have a middle string because otherwise Vim can't recognize the middle lines. 1616 1617Notice the use of the "x" flag in the above three-piece comment definition. 1618When you hit Return in a C-comment, Vim will insert the middle comment leader 1619for the new line: " * ". To close this comment you just have to type "/" 1620before typing anything else on the new line. This will replace the 1621middle-comment leader with the end-comment leader and apply any specified 1622alignment, leaving just `" */"`. There is no need to hit Backspace first. 1623 1624When there is a match with a middle part, but there also is a matching end 1625part which is longer, the end part is used. This makes a C style comment work 1626without requiring the middle part to end with a space. 1627 1628Here is an example of alignment flags at work to make a comment stand out 1629(kind of looks like a 1 too). Consider comment string: >vim 1630 :set comments=sr:/***,m:**,ex-2:******/ 1631> 1632 /*** 1633 **<--right aligned from "r" flag 1634 ** 1635 offset 2 spaces for the "-2" flag-->** 1636 ******/ 1637In this case, the first comment was typed, then return was pressed 4 times, 1638then "/" was pressed to end the comment. 1639 1640Here are some finer points of three part comments. There are three times when 1641alignment and offset flags are taken into consideration: opening a new line 1642after a start-comment, opening a new line before an end-comment, and 1643automatically ending a three-piece comment. The end alignment flag has a 1644backwards perspective; the result is that the same alignment flag used with 1645"s" and "e" will result in the same indent for the starting and ending pieces. 1646Only one alignment per comment part is meant to be used, but an offset number 1647will override the "r" and "l" flag. 1648 1649Enabling 'cindent' will override the alignment flags in many cases. 1650Reindenting using a different method like |gq| or |=| will not consult 1651alignment flags either. The same behaviour can be defined in those other 1652formatting options. One consideration is that 'cindent' has additional 1653options for context based indenting of comments but cannot replicate many 1654three piece indent alignments. However, 'indentexpr' has the ability to work 1655better with three piece comments. 1656 1657Other examples: > 1658 "b:*" Includes lines starting with "*", but not if the "*" is 1659 followed by a non-blank. This avoids a pointer dereference 1660 like "*str" to be recognized as a comment. 1661 "n:>" Includes a line starting with ">", ">>", ">>>", etc. 1662 "fb:-" Format a list that starts with "- ". 1663 1664By default, "b:#" is included. This means that a line that starts with 1665"#include" is not recognized as a comment line. But a line that starts with 1666"# define" is recognized. This is a compromise. 1667 1668 *fo-table* 1669You can use the 'formatoptions' option to influence how Vim formats text. 1670'formatoptions' is a string that can contain any of the letters below. You 1671can separate the option letters with commas for readability. 1672 1673letter meaning when present in 'formatoptions' ~ 1674 *fo-t* 1675t Auto-wrap text using 'textwidth' 1676 *fo-c* 1677c Auto-wrap comments using 'textwidth', inserting the current comment 1678 leader automatically. 1679 *fo-r* 1680r Automatically insert the current comment leader after hitting 1681 <Enter> in Insert mode. 1682 *fo-o* 1683o Automatically insert the current comment leader after hitting 'o' or 1684 'O' in Normal mode. In case comment is unwanted in a specific place 1685 use CTRL-U to quickly delete it. |i_CTRL-U| 1686 *fo-/* 1687/ When 'o' is included: do not insert the comment leader for a // 1688 comment after a statement, only when // is at the start of the line. 1689 *fo-q* 1690q Allow formatting of comments with "gq". 1691 Note that formatting will not change blank lines or lines containing 1692 only the comment leader. A new paragraph starts after such a line, 1693 or when the comment leader changes. 1694 *fo-w* 1695w Trailing white space indicates a paragraph continues in the next line. 1696 A line that ends in a non-white character ends a paragraph. 1697 *fo-a* 1698a Automatic formatting of paragraphs. Every time text is inserted or 1699 deleted the paragraph will be reformatted. See |auto-format|. 1700 When the 'c' flag is present this only happens for recognized 1701 comments. 1702 *fo-n* 1703n When formatting text, recognize numbered lists. This actually uses 1704 the 'formatlistpat' option, thus any kind of list can be used. The 1705 indent of the text after the number is used for the next line. The 1706 default is to find a number, optionally followed by '.', ':', ')', 1707 ']' or '}'. Note that 'autoindent' must be set too. Doesn't work 1708 well together with "2". 1709 Example: > 1710 1. the first item 1711 wraps 1712 2. the second item 1713< *fo-2* 17142 When formatting text, use the indent of the second line of a paragraph 1715 for the rest of the paragraph, instead of the indent of the first 1716 line. This supports paragraphs in which the first line has a 1717 different indent than the rest. Note that 'autoindent' must be set 1718 too. Example: > 1719 first line of a paragraph 1720 second line of the same paragraph 1721 third line. 1722< This also works inside comments, ignoring the comment leader. 1723 *fo-v* 1724v Vi-compatible auto-wrapping in insert mode: Only break a line at a 1725 blank that you have entered during the current insert command. (Note: 1726 this is not 100% Vi compatible. Vi has some "unexpected features" or 1727 bugs in this area. It uses the screen column instead of the line 1728 column.) 1729 *fo-b* 1730b Like 'v', but only auto-wrap if you enter a blank at or before 1731 the wrap margin. If the line was longer than 'textwidth' when you 1732 started the insert, or you do not enter a blank in the insert before 1733 reaching 'textwidth', Vim does not perform auto-wrapping. 1734 *fo-l* 1735l Long lines are not broken in insert mode: When a line was longer than 1736 'textwidth' when the insert command started, Vim does not 1737 automatically format it. 1738 *fo-m* 1739m Also break at a multibyte character above 255. This is useful for 1740 Asian text where every character is a word on its own. Note that 1741 line breaks may also be added after punctuation characters such as 1742 colons to match the CJK linebreaking rules. 1743 *fo-M* 1744M When joining lines, don't insert a space before or after a multibyte 1745 character. Overrules the 'B' flag. 1746 *fo-B* 1747B When joining lines, don't insert a space between two multibyte 1748 characters. Overruled by the 'M' flag. 1749 *fo-1* 17501 Don't break a line after a one-letter word. It's broken before it 1751 instead (if possible). 1752 *fo-]* 1753] Respect 'textwidth' rigorously. With this flag set, no line can be 1754 longer than 'textwidth', unless line-break-prohibition rules make this 1755 impossible. Mainly for CJK scripts and works only if 'encoding' is 1756 "utf-8". 1757 *fo-j* 1758j Where it makes sense, remove a comment leader when joining lines. For 1759 example, joining: 1760 int i; // the index ~ 1761 // in the list ~ 1762 Becomes: 1763 int i; // the index in the list ~ 1764 *fo-p* 1765p Don't break lines at single spaces that follow periods. This is 1766 intended to complement 'joinspaces' and |cpo-J|, for prose with 1767 sentences separated by two spaces. For example, with 'textwidth' set 1768 to 28: > 1769 Surely you're joking, Mr. Feynman! 1770< Becomes: > 1771 Surely you're joking, 1772 Mr. Feynman! 1773< Instead of: > 1774 Surely you're joking, Mr. 1775 Feynman! 1776 1777 1778With 't' and 'c' you can specify when Vim performs auto-wrapping: 1779value action ~ 1780"" no automatic formatting (you can use "gq" for manual formatting) 1781"t" automatic formatting of text, but not comments 1782"c" automatic formatting for comments, but not text (good for C code) 1783"tc" automatic formatting for text and comments 1784 1785Note that when 'textwidth' is 0, Vim does no automatic formatting anyway (but 1786does insert comment leaders according to the 'comments' option). An exception 1787is when the 'a' flag is present. |auto-format| 1788 1789Note that 'textwidth' can be non-zero even if Vim never performs 1790auto-wrapping; 'textwidth' is still useful for formatting with "gq". 1791 1792If the 'comments' option includes "/*", "*" and/or "*/", then Vim has some 1793built in stuff to treat these types of comments a bit more cleverly. 1794Opening a new line before or after "/*" or "*/" (with 'r' or 'o' present in 1795'formatoptions') gives the correct start of the line automatically. The same 1796happens with formatting and auto-wrapping. Opening a line after a line 1797starting with "/*" or "*" and containing "*/", will cause no comment leader to 1798be inserted, and the indent of the new line is taken from the line containing 1799the start of the comment. 1800E.g.: > 1801 /* 1802 * Your typical comment. 1803 */ 1804 The indent on this line is the same as the start of the above 1805 comment. 1806 1807All of this should be really cool, especially in conjunction with the new 1808:autocmd command to prepare different settings for different types of file. 1809 1810Some examples: 1811 for C code (only format comments): > 1812 :set fo=croq 1813< for Mail/news (format all, don't start comment with "o" command): > 1814 :set fo=tcrq 1815< 1816 1817Automatic formatting *auto-format* *autoformat* 1818 1819When the 'a' flag is present in 'formatoptions' text is formatted 1820automatically when inserting text or deleting text. This works nicely for 1821editing text paragraphs. A few hints on how to use this: 1822 1823- You need to properly define paragraphs. The simplest is paragraphs that are 1824 separated by a blank line. When there is no separating blank line, consider 1825 using the 'w' flag and adding a space at the end of each line in the 1826 paragraphs except the last one. 1827 1828- You can set the 'formatoptions' based on the type of file |filetype| or 1829 specifically for one file with a |modeline|. 1830 1831- Set 'formatoptions' to "aw2tq" to make text with indents like this: 1832 1833 bla bla foobar bla 1834 bla foobar bla foobar bla 1835 bla bla foobar bla 1836 bla foobar bla bla foobar 1837 1838- Add the 'c' flag to only auto-format comments. Useful in source code. 1839 1840- Set 'textwidth' to the desired width. If it is zero then 79 is used, or the 1841 width of the screen if this is smaller. 1842 1843And a few warnings: 1844 1845- When part of the text is not properly separated in paragraphs, making 1846 changes in this text will cause it to be formatted anyway. Consider doing > 1847 1848 :set fo-=a 1849 1850- When using the 'w' flag (trailing space means paragraph continues) and 1851 deleting the last line of a paragraph with |dd|, the paragraph will be 1852 joined with the next one. 1853 1854- Changed text is saved for undo. Formatting is also a change. Thus each 1855 format action saves text for undo. This may consume quite a lot of memory. 1856 1857- Formatting a long paragraph and/or with complicated indenting may be slow. 1858 1859============================================================================== 18607. Sorting text *sorting* 1861 1862Vim has a sorting function and a sorting command. The sorting function can be 1863found here: |sort()|, |uniq()|. 1864Also see |:uniq|. 1865 1866 *:sor* *:sort* 1867:[range]sor[t][!] [b][f][i][l][n][o][r][u][x] [/{pattern}/] 1868 Sort lines in [range]. When no range is given all 1869 lines are sorted. 1870 1871 With [!] the order is reversed. 1872 1873 With [i] case is ignored. 1874 *:sort-l* 1875 With [l] sort uses the current collation locale. 1876 Implementation details: strcoll() is used to compare 1877 strings. See |:language| to check or set the collation 1878 locale. Example: > 1879 :language collate en_US.UTF-8 1880 :%sort l 1881< |v:collate| can also used to check the current locale. 1882 Sorting using the locale typically ignores case. 1883 This does not work properly on Mac. 1884 1885 Options [n][f][x][o][b] are mutually exclusive. 1886 1887 With [n] sorting is done on the first decimal number 1888 in the line (after or inside a {pattern} match). 1889 One leading '-' is included in the number. 1890 1891 With [f] sorting is done on the Float in the line. 1892 The value of Float is determined similar to passing 1893 the text (after or inside a {pattern} match) to 1894 str2float() function. 1895 1896 With [x] sorting is done on the first hexadecimal 1897 number in the line (after or inside a {pattern} 1898 match). A leading "0x" or "0X" is ignored. 1899 One leading '-' is included in the number. 1900 1901 With [o] sorting is done on the first octal number in 1902 the line (after or inside a {pattern} match). 1903 1904 With [b] sorting is done on the first binary number in 1905 the line (after or inside a {pattern} match). 1906 *:sort-u* *:sort-uniq* 1907 With [u] (u stands for unique) only keep the first of 1908 a sequence of identical lines (ignoring case when [i] 1909 is used). Without this flag, a sequence of identical 1910 lines will be kept in their original order. 1911 Note that leading and trailing white space may cause 1912 lines to be different. 1913 When you just want to make things unique, use |:uniq|. 1914 1915 When /{pattern}/ is specified and there is no [r] flag 1916 the text matched with {pattern} is skipped, so that 1917 you sort on what comes after the match. 1918 'ignorecase' applies to the pattern, but 'smartcase' 1919 is not used. 1920 Instead of the slash any non-letter can be used. 1921 For example, to sort on the second comma-separated 1922 field: > 1923 :sort /[^,]*,/ 1924< To sort on the text at virtual column 10 (thus 1925 ignoring the difference between tabs and spaces): > 1926 :sort /.*\%10v/ 1927< To sort on the first number in the line, no matter 1928 what is in front of it: > 1929 :sort /.\{-}\ze\d/ 1930< (Explanation: ".\{-}" matches any text, "\ze" sets the 1931 end of the match and \d matches a digit.) 1932 With [r] sorting is done on the matching {pattern} 1933 instead of skipping past it as described above. 1934 For example, to sort on only the first three letters 1935 of each line: > 1936 :sort /\a\a\a/ r 1937 1938< If a {pattern} is used, any lines which don't have a 1939 match for {pattern} are kept in their current order, 1940 but separate from the lines which do match {pattern}. 1941 If you sorted in reverse, they will be in reverse 1942 order after the sorted lines, otherwise they will be 1943 in their original order, right before the sorted 1944 lines. 1945 1946 If {pattern} is empty (e.g. // is specified), the 1947 last search pattern is used. This allows trying out 1948 a pattern first. 1949 1950Note that using `:sort` with `:global` doesn't sort the matching lines, it's 1951quite useless. 1952 1953`:sort` does not use the current locale unless the l flag is used. 1954Vim does do a "stable" sort. 1955 1956The sorting can be interrupted, but if you interrupt it too late in the 1957process you may end up with duplicated lines. This also depends on the system 1958library function used. 1959 1960============================================================================== 19618. Deduplicating text *deduplicating* *unique* 1962 1963Vim has a deduplicating function and a deduplicating command. The 1964deduplicating function can be found here: |uniq()|. 1965Also see |:sort-uniq|. 1966 1967 *:uni* *:uniq* 1968:[range]uni[q][!] [i][l][r][u] [/{pattern}/] 1969 Remove duplicate lines that are adjacent to each other 1970 in [range]. When no range is given, all lines are 1971 processed. 1972 1973 With [i] case is ignored when comparing lines. 1974 1975 With [l] comparison uses the current collation locale. 1976 See |:sort-l| for more details. 1977 1978 With [r] comparison is done on the text that matches 1979 /{pattern}/ instead of the full line. 1980 1981 With [u] only keep lines that do not repeat (i.e., are 1982 not immediately followed by the same line). 1983 1984 With [!] only keep lines that are immediately followed 1985 by a duplicate. 1986 1987 If both [!] and [u] are given, [u] is ignored and [!] 1988 takes effect. 1989 1990 When /{pattern}/ is specified and [r] is not used, the 1991 text matched with {pattern} is skipped and comparison 1992 is done on what comes after the match. 1993 'ignorecase' applies to the pattern, but 'smartcase' 1994 is not used. 1995 Instead of the slash any non-letter can be used. 1996 1997 For example, to remove adjacent duplicate lines based 1998 on the second comma-separated field: > 1999 :uniq /[^,]*,/ 2000< Or to keep only unique lines ignoring the first 5 2001 characters: > 2002 :uniq u /.\{5}/ 2003< If {pattern} is empty (e.g. // is used), the last 2004 search pattern is used. 2005 2006 Note that leading and trailing white space may cause 2007 lines to be considered different. 2008 To remove all duplicates regardless of position, use 2009 |:sort-u| or external tools. 2010 2011 vim:tw=78:ts=8:noet:ft=help:norl: