change.txt (81162B)
1 *change.txt* Nvim 2 3 4 VIM REFERENCE MANUAL by Bram Moolenaar 5 6 7 This file describes commands that delete or change text. In this context, 8 changing text means deleting the text and replacing it with other text using 9 one command. You can undo all of these commands. You can repeat the non-Ex 10 commands with the "." command. 11 12 For inserting text see |insert.txt|. 13 14 Type |gO| to see the table of contents. 15 16 ============================================================================== 17 1. 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 82 These 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* 86 An exception for the d{motion} command: If the motion is not linewise, the 87 start and end of the motion are not in the same line, and there are only 88 blanks before the start and there are no non-blanks after the end of the 89 motion, the delete becomes linewise. This means that the delete also removes 90 the line of blanks that you might expect to remain. Use the |o_v| operator to 91 force the motion to be charwise. 92 93 Trying to delete an empty region of text (e.g., "d0" in the first column) 94 is an error when 'cpoptions' includes the 'E' flag. 95 96 *J* 97 J 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* 109 gJ 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 132 These commands delete the <EOL> between lines. This has the effect of joining 133 multiple lines into one line. You can repeat these commands (except `:j`) and 134 undo them. 135 136 These commands, except "gJ", insert one space in place of the <EOL> unless 137 there is trailing white space or the next line starts with a ')'. These 138 commands, except "gJ", delete any leading white space on the next line. If 139 the 'joinspaces' option is on, these commands insert two spaces after a '.', 140 '!' or '?'. 141 The 'B' and 'M' flags in 'formatoptions' change the behavior for inserting 142 spaces before and after a multibyte character |fo-table|. 143 144 The |'[| mark is set at the end of the first line that was joined, |']| at the 145 end of the resulting line. 146 147 148 ============================================================================== 149 2. Delete and insert *delete-insert* *replacing* 150 151 *R* 152 R 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* 158 gR 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 212 Notes: 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 221 See |registers| for an explanation of registers. 222 223 Replace mode is just like Insert mode, except that every character you enter 224 deletes one character. If you reach the end of a line, Vim appends any 225 further characters (just like Insert mode). In Replace mode, the backspace 226 key restores the original text (if there was any). (See section "Insert and 227 Replace mode" |mode-ins-repl|). 228 229 *cw* *cW* 230 Special case: When the cursor is in a word, "cw" and "cW" do not include the 231 white space after a word, they only change up to the end of the word. This is 232 because Vim interprets "cw" as change-word, and a word does not include the 233 following white space. 234 235 If you prefer "cw" to include the space after a word, see |cpo-_| to change 236 the 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 ============================================================================== 247 3. Simple changes *simple-change* *changing* 248 249 *r* 250 r{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* 275 gr{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* 291 The argument for Normal mode commands like |r| and |t| is a single character. 292 When 'cpo' doesn't contain the 'D' flag, this character can also be entered 293 like |digraphs|. First type CTRL-K and then the two digraph characters. 294 295 *case* 296 The 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~* 307 g~{motion} Switch case of {motion} text. 308 309 g~g~ *g~g~* *g~~* 310 g~~ 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* 321 gU{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 329 gUgU *gUgU* *gUU* 330 gUU 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* 337 gu{motion} Make {motion} text lowercase. 338 339 gugu *gugu* *guu* 340 guu Make current line lowercase. 341 342 *g?* *rot13* 343 g?{motion} Rot13 encode {motion} text. 344 345 *v_g?* 346 {Visual}g? Rot13 encode the highlighted text (for {Visual} see 347 |Visual-mode|). 348 349 g?g? *g?g?* *g??* 350 g?? Rot13 encode current line. 351 352 To turn one line into title caps, make every first letter of a word 353 uppercase: > 354 :s/\v<(.)(\w*)/\u\1\L\2/g 355 356 357 Adding and subtracting ~ 358 *CTRL-A* 359 CTRL-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* 385 CTRL-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 399 The CTRL-A and CTRL-X commands work for (signed) decimal numbers, unsigned 400 binary/octal/hexadecimal numbers and alphabetic characters. 401 402 This 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 418 For decimals a leading negative sign is considered for incrementing or 419 decrementing, for binary, octal and hex values, it won't be considered. To 420 ignore the sign Visually select the number before using CTRL-A or CTRL-X. 421 422 For numbers with leading zeros (including all octal and hexadecimal numbers), 423 Vim 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". 425 There is one exception: When a number that starts with a zero is found not to 426 be octal (it contains a '8' or '9'), but 'nrformats' does include "octal", 427 leading zeros are removed to avoid that the result may be recognized as an 428 octal number. 429 430 Note that when 'nrformats' includes "octal", decimal numbers with leading 431 zeros cause mistakes, because they can be confused with octal numbers. 432 433 Note similarly, when 'nrformats' includes both "bin" and "hex", binary numbers 434 with a leading '0x' or '0X' can be interpreted as hexadecimal rather than 435 binary since '0b' are valid hexadecimal digits. CTRL-A on "0x0b11" results in 436 "0x0b12", not "0x0b100". 437 When 'nrformats' includes "bin" and doesn't include "hex", CTRL-A on "0b11" in 438 "0x0b11" results in "0x0b100". 439 440 When the number under the cursor is too big to fit into 64 bits, it will be 441 rounded off to the nearest number that can be represented, and the 442 addition/subtraction is skipped. E.g. CTRL-X on 18446744073709551616 results 443 in 18446744073709551615. Same for larger numbers, such as 18446744073709551618. 444 445 The CTRL-A command is very useful in a macro. Example: Use the following 446 steps to make a numbered list. 447 448 1. Create the first list entry, make sure it starts with a number. 449 2. qa - start recording into register 'a' 450 3. Y - yank the entry 451 4. p - put a copy of the entry below the first one 452 5. CTRL-A - increment the number 453 6. q - stop recording 454 7. <count>@a - repeat the yank, put and increment <count> times 455 456 457 SHIFTING 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 507 The ">" and "<" commands are handy for changing the indentation within 508 programs. Use the 'shiftwidth' option to set the size of the white space 509 which these commands insert or delete. Normally the 'shiftwidth' option is 8, 510 but you can set it to, say, 3 to make smaller indents. The shift leftwards 511 stops when there is no indent. The shift right does not affect empty lines. 512 513 If the 'shiftround' option is on, the indent is rounded to a multiple of 514 'shiftwidth'. 515 516 If 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). 519 This can be changed with the 'cino' option, see |cino-#|. 520 521 When the 'expandtab' option is off (this is the default) Vim uses <Tab>s as 522 much as possible to make the indent. You can use ">><<" to replace an indent 523 made out of spaces with the same indent made out of <Tab>s (and a few spaces 524 if necessary). If the 'expandtab' option is on, Vim uses only spaces. Then 525 you can use ">><<" to replace <Tab>s in the indent by spaces (or use 526 `:retab!`). 527 528 To move a line several 'shiftwidth's, use Visual mode or the `:` commands. 529 For 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 ============================================================================== 536 4. Complex changes *complex-change* 537 538 4.1 Filter commands *filter* 539 540 A filter is a program that accepts text at standard input, changes it in some 541 way, and sends it to standard output. You can use the commands below to send 542 some text through a filter, so that it is replaced by the filter output. 543 Examples of filters are "sort", which sorts lines alphabetically, and 544 "indent", which formats C program files (you need a version of indent that 545 works like a filter; not all versions do). The 'shell' option specifies the 546 shell Vim uses to execute the filter command. You can repeat filter commands 547 with ".". 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* 597 Nvim uses temporary files for filtering and generating diffs. Plugins also 598 commonly use |tempname()| for their own purposes. On the first request for 599 a temporary file, Nvim creates a common directory (the "Nvim tempdir"), to 600 serve as storage for all temporary files (including `stdpath("run")` files 601 |$XDG_RUNTIME_DIR|) in the current session. 602 603 The 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 607 On unix the tempdir is created with permissions 0700 (only accessible by the 608 current user) to avoid security problems (e.g. symlink attacks). On exit, 609 Nvim deletes the tempdir and its contents. 610 *E5431* 611 If you see an error or |log| message like: > 612 E5431: tempdir disappeared (2 times) 613 this means an external process on your system deleted the Nvim tempdir. 614 Typically this is caused by "antivirus" or a misconfigured cleanup service. 615 616 If Nvim has the setuid bit set this may cause problems: the temp file 617 is owned by the setuid user but the filter command probably runs as the 618 original user. 619 620 621 4.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&* 670 g& 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* 684 The 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 760 Note that there is no flag to change the "magicness" of the pattern. A 761 different command is used instead, or you can use |/\v| and friends. The 762 reason is that the flags can only be found by skipping the pattern, and in 763 order to skip the pattern the "magicness" must be known. Catch 22! 764 765 If the {pattern} for the substitute command is empty, the command uses the 766 pattern from the last substitute or `:global` command. If there is none, but 767 there is a previous search pattern, that one is used. With the [r] flag, the 768 command uses the pattern from the last substitute, `:global`, or search 769 command. 770 771 If the {string} is omitted the substitute is done as if it's empty. Thus the 772 matched pattern is deleted. The separator after {pattern} can also be left 773 out then. Example: > 774 :%s/TESTING 775 This deletes "TESTING" from all lines, but only one per line. 776 777 For 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* 781 Instead of the '/' which surrounds the pattern and replacement string, you can 782 use 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 786 You can use most characters, but not an alphanumeric character, '\', '"' or 787 '|'. 788 789 For 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. 791 Otherwise it works on whole lines anyway. 792 793 *sub-replace-special* *:s\=* 794 When the {string} starts with "\=" it is evaluated as an expression, see 795 |sub-replace-expression|. You can use that for complex replacement or special 796 characters. 797 798 The substitution is limited in recursion to 4 levels. *E1290* 799 800 Otherwise these characters in {string} have a special meaning: 801 802 magic 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 835 The special meaning is also used inside the third argument {sub} of 836 the |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 843 Examples: > 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 851 Note: "\L\u" can be used to capitalize the first letter of a word. This is 852 not compatible with Vi and older versions of Vim, where the "\u" would cancel 853 out the "\L". Same for "\U\l". 854 855 Note: In previous versions CTRL-V was handled in a special way. Since this is 856 not Vi compatible, this was removed. Use a backslash instead. 857 858 command 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 865 The numbering of "\1", "\2" etc. is done based on which "\(" comes first in 866 the pattern (going left to right). When a parentheses group matches several 867 times, the last one will be used for "\1", "\2", etc. Example: > 868 :s/\(\(a[a-d] \)*\)/\2/ modifies "aa ab x" to "ab x" 869 The "\2" is for "\(a[a-d] \)". At first it matches "aa ", secondly "ab ". 870 871 When using parentheses in combination with '|', like in \([ab]\)\|\([cd]\), 872 either 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* 882 2-letter and 3-letter :substitute commands ~ 883 884 These commands repeat the previous `:substitute` command with the given flags. 885 The first letter is always "s", followed by one or two of the possible flag 886 characters. For example `:sce` works like `:s///ce`. The table lists the 887 possible combinations, not all flags are possible, because the command is 888 short 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 902 Exceptions: 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 913 Substitute with an expression *sub-replace-expression* 914 *sub-replace-\=* *s/\=* 915 When the substitute string starts with "\=" the remainder is interpreted as an 916 expression. 917 918 The special meaning for characters as mentioned at |sub-replace-special| does 919 not apply except for "<CR>". A <NL> character is used as a line break, you 920 can get one with a double-quote string: "\n". Prepend a backslash to get a 921 real <NL> character (which will be a NUL in the file). 922 923 The "\=" notation can also be used inside the third argument {sub} of 924 |substitute()| function. In this case, the special meaning for characters as 925 mentioned 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 927 new-line respectively. 928 929 When the result is a |List| then the items are joined with separating line 930 breaks. Thus each item becomes a line, except that they can contain line 931 breaks themselves. 932 933 The |submatch()| function can be used to obtain matched text. The whole 934 matched text can be accessed with "submatch(0)". The text matched with the 935 first pair of () with "submatch(1)". Likewise for further sub-matches in (). 936 937 Be careful: The separation character must not appear in the expression! 938 Consider using a character like "@" or ":". There is no problem if the result 939 of the expression contains the separation character. 940 941 Examples: > 942 :s@\n@\="\r" .. expand("$HOME") .. "\r"@ 943 This replaces an end-of-line with a new line containing the value of $HOME. > 944 945 s/E/\="\<Char-0x20ac>"/g 946 This replaces each 'E' character with a euro sign. Read more in |<Char->|. 947 948 949 4.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* 981 Example for using autocommands and ":retab" to edit a file which is stored 982 with tabstops at 8 but edited with tabstops set at 4. Warning: white space 983 inside 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 ============================================================================== 991 5. 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 1135 You can use these commands to copy text from one place to another. Do this 1136 by first getting the text into a register with a yank, delete or change 1137 command, then inserting the register contents with a put command. You can 1138 also use these commands to move text from one file to another, because Vim 1139 preserves all registers when changing buffers (the CTRL-^ command is a quick 1140 way to toggle between two files). 1141 1142 *linewise-register* *charwise-register* 1143 You can repeat the put commands with "." (except for :put) and undo them. If 1144 the command that was used to get the text into the register was |linewise|, 1145 Vim inserts the text below ("p") or above ("P") the line where the cursor is. 1146 Otherwise Vim inserts the text after ("p") or before ("P") the cursor. With 1147 the ":put" command, Vim always inserts the text in the next line. You can 1148 exchange two characters with the command sequence "xp". You can exchange two 1149 lines with the command sequence "ddp". You can exchange two words with the 1150 command sequence "deep" (start with the cursor in the blank space before the 1151 first word). You can use the |']| or |`]| command after the put command to 1152 move the cursor to the end of the inserted text, or use |'[| or |`[| to move 1153 the cursor to the start. 1154 1155 *put-Visual-mode* 1156 When using a put command like |p| or |P| in Visual mode, Vim will try to 1157 replace the selected text with the contents of the register. How this 1158 works depends on the type of selection and the text. With blockwise selection 1159 it also depends on the size of the block and whether the corners are on an 1160 existing character. (Implementation detail: it actually works by first 1161 putting 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, 1164 selection, or clipboard) to the previously-selected text. Useful if you want 1165 to 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 1168 repeat the change, but the deleted text cannot be used. If you do need it you 1169 can use |p| with another register. E.g., yank the text to copy, Visually 1170 select the text to replace and use "0p . You can repeat this as many times as 1171 you like, and the unnamed register will be changed each time. 1172 *blockwise-put* 1173 When a register contains text from one line (characterwise), using a 1174 blockwise Visual selection, putting that register will paste that text 1175 repeatedly in each of the selected lines, thus replacing the blockwise 1176 selected 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* 1188 If you use a blockwise Visual mode command to get the text into the register, 1189 the block of text will be inserted before ("P") or after ("p") the cursor 1190 column in the current and next lines. Vim makes the whole block of text start 1191 in the same column. Thus the inserted text looks the same as when it was 1192 yanked or deleted. Vim may replace some <Tab> characters with spaces to make 1193 this happen. However, if the width of the block is not a multiple of a <Tab> 1194 width and the text after the inserted block contains <Tab>s, that text may be 1195 misaligned. 1196 1197 Use |zP|/|zp| to paste a blockwise yanked register without appending trailing 1198 spaces. 1199 1200 Note that after a charwise yank command, Vim leaves the cursor on the first 1201 yanked 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. 1203 Rationale: 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. 1207 With a linewise yank command the cursor is put in the first line, but the 1208 column is unmodified, thus it may not be on the first yanked character. 1209 1210 ============================================================================= 1211 Registers *registers* *{register}* *E354* 1212 1213 There are ten types of registers: 1214 1. The unnamed register "" 1215 2. 10 numbered registers "0 to "9 1216 3. The small delete register "- 1217 4. 26 named registers "a to "z or "A to "Z 1218 5. Three read-only registers ":, "., "% 1219 6. Alternate buffer register "# 1220 7. The expression register "= 1221 8. The selection registers "* and "+ 1222 9. The black hole register "_ 1223 10. Last search pattern register "/ 1224 1225 ----------------------------------------------------------------------------- 1226 1. Unnamed register "" *quote_quote* *quotequote* 1227 1228 Vim fills this register with text deleted with the "d", "c", "s", "x" commands 1229 or copied with the yank "y" command, regardless of whether or not a specific 1230 register was used (e.g. "xdd). This is like the unnamed register is pointing 1231 to the last used register. Thus when appending using an uppercase register 1232 name, the unnamed register contains the same text as the named register. 1233 An exception is the '_' register: "_dd does not store the deleted text in any 1234 register. 1235 Vim uses the contents of the unnamed register for any put command (p or P) 1236 which does not specify a register. Additionally you can access it with the 1237 name '"'. This means you have to type two double quotes. Writing to the "" 1238 register writes to register "0. 1239 1240 ----------------------------------------------------------------------------- 1241 2. Numbered registers "0 to "9 *quote_number* 1242 *quote0* *quote1* *quote2* *quote3* *quote4* *quote9* 1243 1244 Vim fills these registers with text from yank and delete commands. 1245 Numbered register 0 contains the text from the most recent yank command, 1246 unless the command specified another register with ["x]. 1247 1248 Numbered register 1 contains the text deleted by the most recent delete or 1249 change command (even when the command specified another register), unless the 1250 text is less than one line (the small delete register is used then). An 1251 exception is made for the delete operator with these movement commands: |%|, 1252 |(|, |)|, |`|, |/|, |?|, |n|, |N|, |{| and |}|. 1253 1254 Register "1 is always used then (this is Vi compatible). The "- register is 1255 used as well if the delete is within a line. Note that these characters may 1256 be mapped. E.g. |%| is mapped by the matchit plugin. 1257 1258 With each successive deletion or change, Vim shifts the previous contents 1259 of register 1 into register 2, 2 into 3, and so forth, losing the previous 1260 contents of register 9. 1261 *yankring* 1262 To 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 ----------------------------------------------------------------------------- 1275 3. Small delete register "- *quote_-* *quote-* 1276 1277 This register contains text from commands that delete less than one line, 1278 except when the command specifies a register with ["x]. 1279 1280 ----------------------------------------------------------------------------- 1281 4. Named registers "a to "z or "A to "Z *quote_alpha* *quotea* 1282 1283 Vim fills these registers only when you say so. Specify them as lowercase 1284 letters to replace their previous contents or as uppercase letters to append 1285 to their previous contents. When the '>' flag is present in 'cpoptions' then 1286 a line break is inserted before the appended text. 1287 1288 ----------------------------------------------------------------------------- 1289 5. Read-only registers ":, ". and "% 1290 1291 These are '%', ':' and '.'. You can use them only with the "p", "P", 1292 and ":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 ----------------------------------------------------------------------------- 1310 6. Alternate file register "# *quote_#* *quote#* 1311 1312 Contains the name of the alternate file for the current window. It will 1313 change how the |CTRL-^| command works. This register is writable, mainly to 1314 allow for restoring it after a plugin has 1315 changed it. 1316 1317 It accepts buffer number: > 1318 let altbuf = bufnr(@#) 1319 ... 1320 let @# = altbuf 1321 1322 It will give error |E86| if you pass buffer number and this buffer does not 1323 exist. 1324 1325 It can also accept a match with an existing buffer name: > 1326 let @# = 'buffer_name' 1327 1328 Error |E93| if there is more than one buffer matching the given name or 1329 |E94| if none of buffers matches the given name. 1330 1331 ----------------------------------------------------------------------------- 1332 7. Expression register "= *quote_=* *quote=* *@=* 1333 1334 This is not really a register that stores text, but is a way to use an 1335 expression in commands which use a register. The expression register is 1336 read-write. 1337 1338 - When typing the '=' after " or CTRL-R the cursor moves to the command-line, 1339 where you can enter any expression (see |expression|). All normal 1340 command-line editing commands are available, including a special history for 1341 expressions. When you end the command-line by typing <CR>, Vim computes the 1342 result of the expression. If you end it with <Esc>, Vim abandons the 1343 expression. If you do not enter an expression, Vim uses the previous 1344 expression (like with the "/" command). 1345 - The expression must evaluate to a String. A Number is always automatically 1346 converted to a String. For the "p" and ":put" command, if the result is 1347 a Float it's converted into a String. If the result is a List each element 1348 is turned into a String and used as a line. A Dictionary is converted into 1349 a String. A Funcref results in an error message (use string() to convert). 1350 - If the "= register is used for the "p" command, the String is split up at 1351 <NL> characters. If the String ends in a <NL>, it is regarded as a linewise 1352 register. 1353 1354 ----------------------------------------------------------------------------- 1355 8. Selection registers "* and "+ 1356 1357 Use these registers for storing and retrieving the selected text for the GUI. 1358 See |quotestar| and |quoteplus|. When the clipboard is not available or not 1359 working, the unnamed register is used instead. For Unix systems and Mac OS X, 1360 see |primary-selection|. 1361 1362 ----------------------------------------------------------------------------- 1363 9. Black hole register "_ *quote_* 1364 1365 When writing to this register, nothing happens. This can be used to delete 1366 text without affecting the normal registers. When reading from this 1367 register, nothing is returned. 1368 1369 ----------------------------------------------------------------------------- 1370 10. Last search pattern register "/ *quote_/* *quote/* 1371 1372 Contains the most recent search-pattern. This is used for "n" and 'hlsearch'. 1373 It is writable with `:let`, you can change it to have 'hlsearch' highlight 1374 other matches without actually searching. You can't yank or delete into this 1375 register. The search direction is available in |v:searchforward|. Note that 1376 the value is restored when returning from a function |function-search-undo|. 1377 1378 *@/* 1379 You can write to a register with a `:let` command |:let-@|. Example: > 1380 :let @/ = "the" 1381 1382 If you use a put command without specifying a register, Vim uses the register 1383 that was last filled (this is also the contents of the unnamed register). If 1384 you are confused, use the `:dis` command to find out what Vim will put (this 1385 command displays all named and numbered registers; the unnamed register is 1386 labelled '"'). 1387 1388 The next three commands always work on whole lines. 1389 1390 :[range]co[py] {address} *:co* *:copy* 1391 Copy the lines given by [range] to below the line 1392 given by {address}. 1393 1394 *:t* 1395 :t Synonym for copy. 1396 1397 :[range]m[ove] {address} *:m* *:mo* *:move* *E134* 1398 Move the lines given by [range] to below the line 1399 given by {address}. 1400 1401 ============================================================================== 1402 6. Formatting text *formatting* 1403 1404 :[range]ce[nter] [width] *:ce* *:center* 1405 Center lines in [range] between [width] columns 1406 (default 'textwidth' or 80 when 'textwidth' is 0). 1407 1408 :[range]ri[ght] [width] *:ri* *:right* 1409 Right-align lines in [range] at [width] columns 1410 (default 'textwidth' or 80 when 'textwidth' is 0). 1411 1412 *:le* *:left* 1413 :[range]le[ft] [indent] 1414 Left-align lines in [range]. Sets the indent in the 1415 lines to [indent] (default 0). 1416 1417 *gq* 1418 gq{motion} Format the lines that {motion} moves over. 1419 Formatting is done with one of three methods: 1420 1. If 'formatexpr' is not empty the expression is 1421 evaluated. This can differ for each buffer. 1422 2. If 'formatprg' is not empty an external program 1423 is used. 1424 3. Otherwise formatting is done internally. 1425 1426 In the third case the 'textwidth' option controls the 1427 length of each formatted line (see below). 1428 If the 'textwidth' option is 0, the formatted line 1429 length is the screen width (with a maximum width of 1430 79). 1431 The 'formatoptions' option controls the type of 1432 formatting |fo-table|. 1433 The cursor is left on the first non-blank of the last 1434 formatted line. 1435 NOTE: The "Q" command formerly performed this 1436 function. If you still want to use "Q" for 1437 formatting, use this mapping: > 1438 :nnoremap Q gq 1439 1440 gqgq *gqgq* *gqq* 1441 gqq Format the current line. With a count format that 1442 many lines. 1443 1444 *v_gq* 1445 {Visual}gq Format the highlighted text. (for {Visual} see 1446 |Visual-mode|). 1447 1448 *gw* 1449 gw{motion} Format the lines that {motion} moves over. Similar to 1450 |gq| but puts the cursor back at the same position in 1451 the text. However, 'formatprg' and 'formatexpr' are 1452 not used. 1453 1454 gwgw *gwgw* *gww* 1455 gww Format the current line as with "gw". 1456 1457 *v_gw* 1458 {Visual}gw Format the highlighted text as with "gw". (for 1459 {Visual} see |Visual-mode|). 1460 1461 Example: To format the current paragraph use: *gqap* > 1462 gqap 1463 1464 The "gq" command leaves the cursor in the line where the motion command takes 1465 the cursor. This allows you to repeat formatting repeated with ".". This 1466 works well with "gqj" (format current and next line) and "gq}" (format until 1467 end of paragraph). Note: When 'formatprg' is set, "gq" leaves the cursor on 1468 the first formatted line (as with using a filter command). 1469 1470 If you want to format the current paragraph and continue where you were, use: > 1471 gwap 1472 If you always want to keep paragraphs formatted you may want to add the 'a' 1473 flag to 'formatoptions'. See |auto-format|. 1474 1475 If the 'autoindent' option is on, Vim uses the indent of the first line for 1476 the following lines. 1477 1478 Formatting does not change empty lines (but it does change lines with only 1479 white space!). 1480 1481 The 'joinspaces' option is used when lines are joined together. 1482 1483 You can set the 'formatexpr' option to an expression or the 'formatprg' option 1484 to the name of an external program for Vim to use for text formatting. The 1485 'textwidth' and other options have no effect on formatting by an external 1486 program. 1487 1488 *format-formatexpr* 1489 The 'formatexpr' option can be set to a Vim script function that performs 1490 reformatting of the buffer. This should usually happen in an |ftplugin|, 1491 since formatting is highly dependent on the type of file. It makes 1492 sense to use an |autoload| script, so the corresponding script is only loaded 1493 when actually needed and the script should be called <filetype>format.vim. 1494 1495 For example, the XML filetype plugin distributed with Vim in the 1496 $VIMRUNTIME/ftplugin directory, sets the 'formatexpr' option to: > 1497 1498 setlocal formatexpr=xmlformat#Format() 1499 1500 That means, you will find the corresponding script, defining the 1501 xmlformat#Format() function, in the file `$VIMRUNTIME/autoload/xmlformat.vim` 1502 1503 Here is an example script that removes trailing whitespace from the selected 1504 text. Put it in your autoload directory, e.g. ~/.vim/autoload/format.vim: 1505 >vim 1506 func! format#Format() 1507 " only reformat on explicit gq command 1508 if mode() != 'n' 1509 " fall back to Vim's internal reformatting 1510 return 1 1511 endif 1512 let lines = getline(v:lnum, v:lnum + v:count - 1) 1513 call map(lines, {key, val -> substitute(val, '\s\+$', '', 'g')}) 1514 call setline('.', lines) 1515 1516 " do not run internal formatter! 1517 return 0 1518 endfunc 1519 1520 You can then enable the formatting by executing: > 1521 setlocal formatexpr=format#Format() 1522 1523 Note: this function explicitly returns non-zero when called from insert mode 1524 (which basically means, text is inserted beyond the 'textwidth' limit). This 1525 causes Vim to fall back to reformat the text by using the internal formatter. 1526 1527 However, if the |gq| command is used to reformat the text, the function 1528 will receive the selected lines, trim trailing whitespace from those lines and 1529 put them back in place. If you are going to split single lines into multiple 1530 lines, be careful not to overwrite anything. 1531 1532 If you want to allow reformatting of text from insert or replace mode, one has 1533 to be very careful, because the function might be called recursively. For 1534 debugging it helps to set the 'debug' option. 1535 1536 *right-justify* 1537 There is no command in Vim to right justify text. You can do it with 1538 an external command, like "par" (e.g.: `:.,}!par` to format until the end of the 1539 paragraph) or set 'formatprg' to "par". 1540 1541 *format-comments* 1542 An overview of comment formatting is in section |30.6| of the user manual. 1543 1544 Vim can automatically insert and format comments in a special way. Vim 1545 recognizes a comment by a specific string at the start of the line (ignoring 1546 white space). Three types of comments can be used: 1547 1548 - A comment string that repeats at the start of each line. An example is the 1549 type of comment used in shell scripts, starting with "#". 1550 - A comment string that occurs only in the first line, not in the following 1551 lines. An example is this list with dashes. 1552 - Three-piece comments that have a start string, an end string, and optional 1553 lines in between. The strings for the start, middle and end are different. 1554 An example is the C style comment: > 1555 /* 1556 * this is a C comment 1557 */ 1558 1559 The 'comments' option is a comma-separated list of parts. Each part defines a 1560 type of comment string. A part consists of: 1561 {flags}:{string} 1562 1563 {string} is the literal text that must appear. 1564 1565 {flags}: 1566 n Nested comment. Nesting with mixed parts is allowed. If 'comments' 1567 is "n:),n:>" a line starting with "> ) >" is a comment. 1568 1569 b Blank (<Space>, <Tab> or <EOL>) required after {string}. 1570 1571 f Only the first line has the comment string. Do not repeat comment on 1572 the next line, but preserve indentation (e.g., a bullet-list). 1573 1574 s Start of three-piece comment 1575 1576 m Middle of a three-piece comment 1577 1578 e End of a three-piece comment 1579 1580 l Left align. Used together with 's' or 'e', the leftmost character of 1581 start or end will line up with the leftmost character from the middle. 1582 This is the default and can be omitted. See below for more details. 1583 1584 r Right align. Same as above but rightmost instead of leftmost. See 1585 below for more details. 1586 1587 O Don't consider this comment for the "O" command. 1588 1589 x Allows three-piece comments to be ended by just typing the last 1590 character of the end-comment string as the first action on a new 1591 line when the middle-comment string has been inserted automatically. 1592 See below for more details. 1593 1594 {digits} 1595 When together with 's' or 'e': add {digit} amount of offset to an 1596 automatically inserted middle or end comment leader. The offset 1597 begins from a left alignment. See below for more details. 1598 1599 -{digits} 1600 Like {digits} but reduce the indent. This only works when there is 1601 some indent for the start or end part that can be removed. 1602 1603 When a string has none of the 'f', 's', 'm' or 'e' flags, Vim assumes the 1604 comment string repeats at the start of each line. The {flags} field may be 1605 empty. 1606 1607 Any blank space in the text before and after the {string} is part of the 1608 {string}, so do not include leading or trailing blanks unless the blanks are a 1609 required part of the comment string. 1610 1611 When one comment leader is part of another, specify the part after the whole. 1612 For example, to include both "-" and "->", use > 1613 :set comments=f:->,f:- 1614 1615 A three-piece comment must always be given as start,middle,end, with no other 1616 parts in between. An example of a three-piece comment is > 1617 sr:/*,mb:*,ex:*/ 1618 for C-comments. To avoid recognizing "*ptr" as a comment, the middle string 1619 includes the 'b' flag. For three-piece comments, Vim checks the text after 1620 the start and middle strings for the end string. If Vim finds the end string, 1621 the comment does not continue on the next line. Three-piece comments must 1622 have a middle string because otherwise Vim can't recognize the middle lines. 1623 1624 Notice the use of the "x" flag in the above three-piece comment definition. 1625 When you hit Return in a C-comment, Vim will insert the middle comment leader 1626 for the new line: " * ". To close this comment you just have to type "/" 1627 before typing anything else on the new line. This will replace the 1628 middle-comment leader with the end-comment leader and apply any specified 1629 alignment, leaving just `" */"`. There is no need to hit Backspace first. 1630 1631 When there is a match with a middle part, but there also is a matching end 1632 part which is longer, the end part is used. This makes a C style comment work 1633 without requiring the middle part to end with a space. 1634 1635 Here is an example of alignment flags at work to make a comment stand out 1636 (kind of looks like a 1 too). Consider comment string: >vim 1637 :set comments=sr:/***,m:**,ex-2:******/ 1638 > 1639 /*** 1640 **<--right aligned from "r" flag 1641 ** 1642 offset 2 spaces for the "-2" flag-->** 1643 ******/ 1644 In this case, the first comment was typed, then return was pressed 4 times, 1645 then "/" was pressed to end the comment. 1646 1647 Here are some finer points of three part comments. There are three times when 1648 alignment and offset flags are taken into consideration: opening a new line 1649 after a start-comment, opening a new line before an end-comment, and 1650 automatically ending a three-piece comment. The end alignment flag has a 1651 backwards perspective; the result is that the same alignment flag used with 1652 "s" and "e" will result in the same indent for the starting and ending pieces. 1653 Only one alignment per comment part is meant to be used, but an offset number 1654 will override the "r" and "l" flag. 1655 1656 Enabling 'cindent' will override the alignment flags in many cases. 1657 Reindenting using a different method like |gq| or |=| will not consult 1658 alignment flags either. The same behaviour can be defined in those other 1659 formatting options. One consideration is that 'cindent' has additional 1660 options for context based indenting of comments but cannot replicate many 1661 three piece indent alignments. However, 'indentexpr' has the ability to work 1662 better with three piece comments. 1663 1664 Other examples: > 1665 "b:*" Includes lines starting with "*", but not if the "*" is 1666 followed by a non-blank. This avoids a pointer dereference 1667 like "*str" to be recognized as a comment. 1668 "n:>" Includes a line starting with ">", ">>", ">>>", etc. 1669 "fb:-" Format a list that starts with "- ". 1670 1671 By default, "b:#" is included. This means that a line that starts with 1672 "#include" is not recognized as a comment line. But a line that starts with 1673 "# define" is recognized. This is a compromise. 1674 1675 *fo-table* 1676 You can use the 'formatoptions' option to influence how Vim formats text. 1677 'formatoptions' is a string that can contain any of the letters below. You 1678 can separate the option letters with commas for readability. 1679 1680 letter meaning when present in 'formatoptions' ~ 1681 *fo-t* 1682 t Auto-wrap text using 'textwidth' 1683 *fo-c* 1684 c Auto-wrap comments using 'textwidth', inserting the current comment 1685 leader automatically. 1686 *fo-r* 1687 r Automatically insert the current comment leader after hitting 1688 <Enter> in Insert mode. 1689 *fo-o* 1690 o Automatically insert the current comment leader after hitting 'o' or 1691 'O' in Normal mode. In case comment is unwanted in a specific place 1692 use CTRL-U to quickly delete it. |i_CTRL-U| 1693 *fo-/* 1694 / When 'o' is included: do not insert the comment leader for a // 1695 comment after a statement, only when // is at the start of the line. 1696 *fo-q* 1697 q Allow formatting of comments with "gq". 1698 Note that formatting will not change blank lines or lines containing 1699 only the comment leader. A new paragraph starts after such a line, 1700 or when the comment leader changes. 1701 *fo-w* 1702 w Trailing white space indicates a paragraph continues in the next line. 1703 A line that ends in a non-white character ends a paragraph. 1704 *fo-a* 1705 a Automatic formatting of paragraphs. Every time text is inserted or 1706 deleted the paragraph will be reformatted. See |auto-format|. 1707 When the 'c' flag is present this only happens for recognized 1708 comments. 1709 *fo-n* 1710 n When formatting text, recognize numbered lists. This actually uses 1711 the 'formatlistpat' option, thus any kind of list can be used. The 1712 indent of the text after the number is used for the next line. The 1713 default is to find a number, optionally followed by '.', ':', ')', 1714 ']' or '}'. Note that 'autoindent' must be set too. Doesn't work 1715 well together with "2". 1716 Example: > 1717 1. the first item 1718 wraps 1719 2. the second item 1720 < *fo-2* 1721 2 When formatting text, use the indent of the second line of a paragraph 1722 for the rest of the paragraph, instead of the indent of the first 1723 line. This supports paragraphs in which the first line has a 1724 different indent than the rest. Note that 'autoindent' must be set 1725 too. Example: > 1726 first line of a paragraph 1727 second line of the same paragraph 1728 third line. 1729 < This also works inside comments, ignoring the comment leader. 1730 *fo-v* 1731 v Vi-compatible auto-wrapping in insert mode: Only break a line at a 1732 blank that you have entered during the current insert command. (Note: 1733 this is not 100% Vi compatible. Vi has some "unexpected features" or 1734 bugs in this area. It uses the screen column instead of the line 1735 column.) 1736 *fo-b* 1737 b Like 'v', but only auto-wrap if you enter a blank at or before 1738 the wrap margin. If the line was longer than 'textwidth' when you 1739 started the insert, or you do not enter a blank in the insert before 1740 reaching 'textwidth', Vim does not perform auto-wrapping. 1741 *fo-l* 1742 l Long lines are not broken in insert mode: When a line was longer than 1743 'textwidth' when the insert command started, Vim does not 1744 automatically format it. 1745 *fo-m* 1746 m Also break at a multibyte character above 255. This is useful for 1747 Asian text where every character is a word on its own. Note that 1748 line breaks may also be added after punctuation characters such as 1749 colons to match the CJK linebreaking rules. 1750 *fo-M* 1751 M When joining lines, don't insert a space before or after a multibyte 1752 character. Overrules the 'B' flag. 1753 *fo-B* 1754 B When joining lines, don't insert a space between two multibyte 1755 characters. Overruled by the 'M' flag. 1756 *fo-1* 1757 1 Don't break a line after a one-letter word. It's broken before it 1758 instead (if possible). 1759 *fo-]* 1760 ] Respect 'textwidth' rigorously. With this flag set, no line can be 1761 longer than 'textwidth', unless line-break-prohibition rules make this 1762 impossible. Mainly for CJK scripts and works only if 'encoding' is 1763 "utf-8". 1764 *fo-j* 1765 j Where it makes sense, remove a comment leader when joining lines. For 1766 example, joining: 1767 int i; // the index ~ 1768 // in the list ~ 1769 Becomes: 1770 int i; // the index in the list ~ 1771 *fo-p* 1772 p Don't break lines at single spaces that follow periods. This is 1773 intended to complement 'joinspaces' and |cpo-J|, for prose with 1774 sentences separated by two spaces. For example, with 'textwidth' set 1775 to 28: > 1776 Surely you're joking, Mr. Feynman! 1777 < Becomes: > 1778 Surely you're joking, 1779 Mr. Feynman! 1780 < Instead of: > 1781 Surely you're joking, Mr. 1782 Feynman! 1783 1784 1785 With 't' and 'c' you can specify when Vim performs auto-wrapping: 1786 value action ~ 1787 "" no automatic formatting (you can use "gq" for manual formatting) 1788 "t" automatic formatting of text, but not comments 1789 "c" automatic formatting for comments, but not text (good for C code) 1790 "tc" automatic formatting for text and comments 1791 1792 Note that when 'textwidth' is 0, Vim does no automatic formatting anyway (but 1793 does insert comment leaders according to the 'comments' option). An exception 1794 is when the 'a' flag is present. |auto-format| 1795 1796 Note that 'textwidth' can be non-zero even if Vim never performs 1797 auto-wrapping; 'textwidth' is still useful for formatting with "gq". 1798 1799 If the 'comments' option includes "/*", "*" and/or "*/", then Vim has some 1800 built in stuff to treat these types of comments a bit more cleverly. 1801 Opening a new line before or after "/*" or "*/" (with 'r' or 'o' present in 1802 'formatoptions') gives the correct start of the line automatically. The same 1803 happens with formatting and auto-wrapping. Opening a line after a line 1804 starting with "/*" or "*" and containing "*/", will cause no comment leader to 1805 be inserted, and the indent of the new line is taken from the line containing 1806 the start of the comment. 1807 E.g.: > 1808 /* 1809 * Your typical comment. 1810 */ 1811 The indent on this line is the same as the start of the above 1812 comment. 1813 1814 All of this should be really cool, especially in conjunction with the new 1815 :autocmd command to prepare different settings for different types of file. 1816 1817 Some examples: 1818 for C code (only format comments): > 1819 :set fo=croq 1820 < for Mail/news (format all, don't start comment with "o" command): > 1821 :set fo=tcrq 1822 < 1823 1824 Automatic formatting *auto-format* *autoformat* 1825 1826 When the 'a' flag is present in 'formatoptions' text is formatted 1827 automatically when inserting text or deleting text. This works nicely for 1828 editing text paragraphs. A few hints on how to use this: 1829 1830 - You need to properly define paragraphs. The simplest is paragraphs that are 1831 separated by a blank line. When there is no separating blank line, consider 1832 using the 'w' flag and adding a space at the end of each line in the 1833 paragraphs except the last one. 1834 1835 - You can set the 'formatoptions' based on the type of file |filetype| or 1836 specifically for one file with a |modeline|. 1837 1838 - Set 'formatoptions' to "aw2tq" to make text with indents like this: 1839 1840 bla bla foobar bla 1841 bla foobar bla foobar bla 1842 bla bla foobar bla 1843 bla foobar bla bla foobar 1844 1845 - Add the 'c' flag to only auto-format comments. Useful in source code. 1846 1847 - Set 'textwidth' to the desired width. If it is zero then 79 is used, or the 1848 width of the screen if this is smaller. 1849 1850 And a few warnings: 1851 1852 - When part of the text is not properly separated in paragraphs, making 1853 changes in this text will cause it to be formatted anyway. Consider doing > 1854 1855 :set fo-=a 1856 1857 - When using the 'w' flag (trailing space means paragraph continues) and 1858 deleting the last line of a paragraph with |dd|, the paragraph will be 1859 joined with the next one. 1860 1861 - Changed text is saved for undo. Formatting is also a change. Thus each 1862 format action saves text for undo. This may consume quite a lot of memory. 1863 1864 - Formatting a long paragraph and/or with complicated indenting may be slow. 1865 1866 ============================================================================== 1867 7. Sorting text *sorting* 1868 1869 Vim has a sorting function and a sorting command. The sorting function can be 1870 found here: |sort()|, |uniq()|. 1871 Also see |:uniq|. 1872 1873 *:sor* *:sort* 1874 :[range]sor[t][!] [b][f][i][l][n][o][r][u][x] [/{pattern}/] 1875 Sort lines in [range]. When no range is given all 1876 lines are sorted. 1877 1878 With [!] the order is reversed. 1879 1880 With [i] case is ignored. 1881 *:sort-l* 1882 With [l] sort uses the current collation locale. 1883 Implementation details: strcoll() is used to compare 1884 strings. See |:language| to check or set the collation 1885 locale. Example: > 1886 :language collate en_US.UTF-8 1887 :%sort l 1888 < |v:collate| can also used to check the current locale. 1889 Sorting using the locale typically ignores case. 1890 This does not work properly on Mac. 1891 1892 Options [n][f][x][o][b] are mutually exclusive. 1893 1894 With [n] sorting is done on the first decimal number 1895 in the line (after or inside a {pattern} match). 1896 One leading '-' is included in the number. 1897 1898 With [f] sorting is done on the Float in the line. 1899 The value of Float is determined similar to passing 1900 the text (after or inside a {pattern} match) to 1901 str2float() function. 1902 1903 With [x] sorting is done on the first hexadecimal 1904 number in the line (after or inside a {pattern} 1905 match). A leading "0x" or "0X" is ignored. 1906 One leading '-' is included in the number. 1907 1908 With [o] sorting is done on the first octal number in 1909 the line (after or inside a {pattern} match). 1910 1911 With [b] sorting is done on the first binary number in 1912 the line (after or inside a {pattern} match). 1913 *:sort-u* *:sort-uniq* 1914 With [u] (u stands for unique) only keep the first of 1915 a sequence of identical lines (ignoring case when [i] 1916 is used). Without this flag, a sequence of identical 1917 lines will be kept in their original order. 1918 Note that leading and trailing white space may cause 1919 lines to be different. 1920 When you just want to make things unique, use |:uniq|. 1921 1922 When /{pattern}/ is specified and there is no [r] flag 1923 the text matched with {pattern} is skipped, so that 1924 you sort on what comes after the match. 1925 'ignorecase' applies to the pattern, but 'smartcase' 1926 is not used. 1927 Instead of the slash any non-letter can be used. 1928 For example, to sort on the second comma-separated 1929 field: > 1930 :sort /[^,]*,/ 1931 < To sort on the text at virtual column 10 (thus 1932 ignoring the difference between tabs and spaces): > 1933 :sort /.*\%10v/ 1934 < To sort on the first number in the line, no matter 1935 what is in front of it: > 1936 :sort /.\{-}\ze\d/ 1937 < (Explanation: ".\{-}" matches any text, "\ze" sets the 1938 end of the match and \d matches a digit.) 1939 With [r] sorting is done on the matching {pattern} 1940 instead of skipping past it as described above. 1941 For example, to sort on only the first three letters 1942 of each line: > 1943 :sort /\a\a\a/ r 1944 1945 < If a {pattern} is used, any lines which don't have a 1946 match for {pattern} are kept in their current order, 1947 but separate from the lines which do match {pattern}. 1948 If you sorted in reverse, they will be in reverse 1949 order after the sorted lines, otherwise they will be 1950 in their original order, right before the sorted 1951 lines. 1952 1953 If {pattern} is empty (e.g. // is specified), the 1954 last search pattern is used. This allows trying out 1955 a pattern first. 1956 1957 Note that using `:sort` with `:global` doesn't sort the matching lines, it's 1958 quite useless. 1959 1960 `:sort` does not use the current locale unless the l flag is used. 1961 Vim does do a "stable" sort. 1962 1963 The sorting can be interrupted, but if you interrupt it too late in the 1964 process you may end up with duplicated lines. This also depends on the system 1965 library function used. 1966 1967 ============================================================================== 1968 8. Deduplicating text *deduplicating* *unique* 1969 1970 Vim has a deduplicating function and a deduplicating command. The 1971 deduplicating function can be found here: |uniq()|. 1972 Also see |:sort-uniq|. 1973 1974 *:uni* *:uniq* 1975 :[range]uni[q][!] [i][l][r][u] [/{pattern}/] 1976 Remove duplicate lines that are adjacent to each other 1977 in [range]. When no range is given, all lines are 1978 processed. 1979 1980 With [i] case is ignored when comparing lines. 1981 1982 With [l] comparison uses the current collation locale. 1983 See |:sort-l| for more details. 1984 1985 With [r] comparison is done on the text that matches 1986 /{pattern}/ instead of the full line. 1987 1988 With [u] only keep lines that do not repeat (i.e., are 1989 not immediately followed by the same line). 1990 1991 With [!] only keep lines that are immediately followed 1992 by a duplicate. 1993 1994 If both [!] and [u] are given, [u] is ignored and [!] 1995 takes effect. 1996 1997 When /{pattern}/ is specified and [r] is not used, the 1998 text matched with {pattern} is skipped and comparison 1999 is done on what comes after the match. 2000 'ignorecase' applies to the pattern, but 'smartcase' 2001 is not used. 2002 Instead of the slash any non-letter can be used. 2003 2004 For example, to remove adjacent duplicate lines based 2005 on the second comma-separated field: > 2006 :uniq /[^,]*,/ 2007 < Or to keep only unique lines ignoring the first 5 2008 characters: > 2009 :uniq u /.\{5}/ 2010 < If {pattern} is empty (e.g. // is used), the last 2011 search pattern is used. 2012 2013 Note that leading and trailing white space may cause 2014 lines to be considered different. 2015 To remove all duplicates regardless of position, use 2016 |:sort-u| or external tools. 2017 2018 vim:tw=78:ts=8:noet:ft=help:norl: