blob: 5e45ec7ac9fae101ce9071b22a62e3852538f6f8 [file] [log] [blame]
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001*change.txt* For Vim version 7.0aa. Last change: 2004 Nov 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
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
121. Deleting text |deleting|
132. Delete and insert |delete-insert|
143. Simple changes |simple-change| *changing*
154. Complex changes |complex-change|
Bram Moolenaar47136d72004-10-12 20:02:24 +000016 4.1 Filter commands |filter|
17 4.2 Substitute |:substitute|
18 4.3 Search and replace |search-replace|
19 4.4 Changing tabs |change-tabs|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205. Copying and moving text |copy-move|
216. Formatting text |formatting|
22
23For inserting text see |insert.txt|.
24
25==============================================================================
261. Deleting text *deleting* *E470*
27
28["x]<Del> or *<Del>* *x* *dl*
29["x]x Delete [count] characters under and after the cursor
30 [into register x] (not |linewise|). Does the same as
31 "dl".
32 The <Del> key does not take a [count]. Instead, it
33 deletes the last character of the count.
34 See |:fixdel| if the <Del> key does not do what you
35 want. See |'whichwrap'| for deleting a line break
36 (join lines). {Vi does not support <Del>}
37
38 *X* *dh*
39["x]X Delete [count] characters before the cursor [into
40 register x] (not |linewise|). Does the same as "dh".
41 Also see |'whichwrap'|.
42
43 *d*
44["x]d{motion} Delete text that {motion} moves over [into register
45 x]. See below for exceptions.
46
47 *dd*
48["x]dd Delete [count] lines [into register x] |linewise|.
49
50 *D*
51["x]D Delete the characters under the cursor until the end
52 of the line and [count]-1 more lines [into register
53 x]; synonym for "d$".
54 (not |linewise|)
55
56{Visual}["x]x or *v_x* *v_d* *v_<Del>*
57{Visual}["x]d or
58{Visual}["x]<Del> Delete the highlighted text [into register x] (for
59 {Visual} see |Visual-mode|). {not in Vi}
60
61{Visual}["x]CTRL-H or *v_CTRL-H* *v_<BS>*
62{Visual}["x]<BS> When in Select mode: Delete the highlighted text [into
63 register x].
64
65{Visual}["x]X or *v_X* *v_D* *v_b_D*
66{Visual}["x]D Delete the highlighted lines [into register x] (for
67 {Visual} see |Visual-mode|). In Visual block mode,
68 "D" deletes the highlighted text plus all text until
69 the end of the line. {not in Vi}
70
71 *:d* *:de* *:del* *:delete*
72:[range]d[elete] [x] Delete [range] lines (default: current line) [into
73 register x].
74
75:[range]d[elete] [x] {count}
76 Delete {count} lines, starting with [range]
77 (default: current line |cmdline-ranges|) [into
78 register x].
79
80These commands delete text. You can repeat them with the "." command
81(except ":d") and undo them. Use Visual mode to delete blocks of text. See
82|registers| for an explanation of registers.
83
84An exception for the d{motion} command: If the motion is not linewise, the
85start and end of the motion are not in the same line, and there are only
86blanks before the start and after the end of the motion, the delete becomes
87linewise. This means that the delete also removes the line of blanks that you
88might expect to remain.
89
90Trying to delete an empty region of text (e.g., "d0" in the first column)
91is an error when 'cpoptions' includes the 'E' flag.
92
93 *J*
94J Join [count] lines, with a minimum of two lines.
95 Remove the indent and insert up to two spaces (see
96 below).
97
98 *v_J*
99{Visual}J Join the highlighted lines, with a minimum of two
100 lines. Remove the indent and insert up to two spaces
101 (see below). {not in Vi}
102
103 *gJ*
104gJ Join [count] lines, with a minimum of two lines.
105 Don't insert or remove any spaces. {not in Vi}
106
107 *v_gJ*
108{Visual}gJ Join the highlighted lines, with a minimum of two
109 lines. Don't insert or remove any spaces. {not in
110 Vi}
111
112 *:j* *:join*
113:[range]j[oin][!] Join [range] lines. Same as "J", except with [!]
114 the join does not insert or delete any spaces.
115 If a [range] has equal start and end values, this
116 command does nothing. The default behavior is to
117 join the current line with the line below it.
118 {not in Vi: !}
119
120:[range]j[oin][!] {count}
121 Join {count} lines, starting with [range] (default:
122 current line |cmdline-ranges|). Same as "J", except
123 with [!] the join does not insert or delete any
124 spaces.
125 {not in Vi: !}
126
127These commands delete the <EOL> between lines. This has the effect of joining
128multiple lines into one line. You can repeat these commands (except ":j") and
129undo them.
130
131These commands, except "gJ", insert one space in place of the <EOL> unless
132there is trailing white space or the next line starts with a ')'. These
133commands, except "gJ", delete any leading white space on the next line. If
134the 'joinspaces' option is on, these commands insert two spaces after a '.',
135'!' or '?' (but if 'cpoptions' includes the 'j' flag, they insert two spaces
136only after a '.').
137The 'B' and 'M' flags in 'formatoptions' change the behavior for inserting
138spaces before and after a multi-byte character |fo-table|.
139
140
141==============================================================================
1422. Delete and insert *delete-insert* *replacing*
143
144 *R*
145R Enter Replace mode: Each character you type replaces
146 an existing character, starting with the character
147 under the cursor. Repeat the entered text [count]-1
148 times. See |Replace-mode| for more details.
149
150 *gR*
151gR Enter Virtual Replace mode: Each character you type
152 replaces existing characters in screen space. So a
153 <Tab> may replace several characters at once.
154 Repeat the entered text [count]-1 times. See
155 |Virtual-Replace-mode| for more details.
156 {not available when compiled without the +vreplace
157 feature}
158
159 *c*
160["x]c{motion} Delete {motion} text [into register x] and start
161 insert. When 'cpoptions' includes the 'E' flag and
162 there is no text to delete (e.g., with "cTx" when the
163 cursor is just after an 'x'), an error occurs and
164 insert mode does not start (this is Vi compatible).
165 When 'cpoptions' does not include the 'E' flag, the
166 "c" command always starts insert mode, even if there
167 is no text to delete.
168
169 *cc*
170["x]cc Delete [count] lines [into register x] and start
171 insert |linewise|. If 'autoindent' is on, preserve
172 the indent of the first line.
173
174 *C*
175["x]C Delete from the cursor position to the end of the
176 line and [count]-1 more lines [into register x], and
177 start insert. Synonym for c$ (not |linewise|).
178
179 *s*
180["x]s Delete [count] characters [into register x] and start
181 insert (s stands for Substitute). Synonym for "cl"
182 (not |linewise|).
183
184 *S*
185["x]S Delete [count] lines [into register x] and start
186 insert. Synonym for "cc" |linewise|.
187
188{Visual}["x]c or *v_c* *v_s*
189{Visual}["x]s Delete the highlighted text [into register x] and
190 start insert (for {Visual} see |Visual-mode|). {not
191 in Vi}
192
193 *v_r*
194{Visual}["x]r{char} Replace all selected characters by {char}.
195
196 *v_C*
197{Visual}["x]C Delete the highlighted lines [into register x] and
198 start insert. In Visual block mode it works
199 differently |v_b_C|. {not in Vi}
200 *v_S*
201{Visual}["x]S Delete the highlighted lines [into register x] and
202 start insert (for {Visual} see |Visual-mode|). {not
203 in Vi}
204 *v_R*
205{Visual}["x]R Currently just like {Visual}["x]S. In a next version
206 it might work differently. {not in Vi}
207
208Notes:
209- You can end Insert and Replace mode with <Esc>.
210- See the section "Insert and Replace mode" |mode-ins-repl| for the other
211 special characters in these modes.
212- The effect of [count] takes place after Vim exits Insert or Replace mode.
213- When the 'cpoptions' option contains '$' and the change is within one line,
214 Vim continues to show the text to be deleted and puts a '$' at the last
215 deleted character.
216
217See |registers| for an explanation of registers.
218
219Replace mode is just like Insert mode, except that every character you enter
220deletes one character. If you reach the end of a line, Vim appends any
221further characters (just like Insert mode). In Replace mode, the backspace
222key restores the original text (if there was any). (See section "Insert and
223Replace mode" |mode-ins-repl|).
224
225 *cw* *cW*
226Special case: "cw" and "cW" work the same as "ce" and "cE" if the cursor is
227on a non-blank. This is because Vim interprets "cw" as change-word, and a
228word does not include the following white space. {Vi: "cw" when on a blank
229followed by other blanks changes only the first blank; this is probably a
230bug, because "dw" deletes all the blanks; use the 'w' flag in 'cpoptions' to
231make it work like Vi anyway}
232
233If you prefer "cw" to include the space after a word, use this mapping: >
234 :map cw dwi
235<
236 *:c* *:ch* *:change*
237:{range}c[hange] Replace lines of text with some different text.
238 Type a line containing only "." to stop replacing.
239 Without {range}, this command changes only the current
240 line.
241
242==============================================================================
2433. Simple changes *simple-change*
244
245 *r*
246r{char} Replace the character under the cursor with {char}.
247 If {char} is a <CR> or <NL>, a line break replaces the
248 character. To replace with a real <CR>, use CTRL-V
249 <CR>. CTRL-V <NL> replaces with a <Nul>.
250 {Vi: CTRL-V <CR> still replaces with a line break,
251 cannot replace something with a <CR>}
252 If you give a [count], Vim replaces [count] characters
253 with [count] {char}s. When {char} is a <CR> or <NL>,
254 however, Vim inserts only one <CR>: "5r<CR>" replaces
255 five characters with a single line break.
256 When {char} is a <CR> or <NL>, Vim performs
257 autoindenting. This works just like deleting the
258 characters that are replaced and then doing
259 "i<CR><Esc>".
260 {char} can be entered as a digraph |digraph-arg|.
261 |:lmap| mappings apply to {char}. The CTRL-^ command
262 in Insert mode can be used to switch this on/off
263 |i_CTRL-^|. See |utf-8-char-arg| about using
264 composing characters when 'encoding' is Unicode.
265
266 *gr*
267gr{char} Replace the virtual characters under the cursor with
268 {char}. This replaces in screen space, not file
269 space. See |gR| and |Virtual-Replace-mode| for more
270 details. As with |r| a count may be given.
271 {char} can be entered like with |r|.
272 {not available when compiled without the +vreplace
273 feature}
274
275 *digraph-arg*
276The argument for Normal mode commands like |r| and |t| is a single character.
277When 'cpo' doesn't contain the 'D' flag, this character can also be entered
278like |digraphs|. First type CTRL-K and then the two digraph characters.
279{not available when compiled without the |+digraphs| feature}
280
281 *case*
282The following commands change the case of letters. The currently active
283|locale| is used. See |:language|. The LC_CTYPE value matters here.
284
285 *~*
286~ 'notildeop' option: Switch case of the character
287 under the cursor and move the cursor to the right.
288 If a [count] is given, do that many characters. {Vi:
289 no count}
290
291~{motion} 'tildeop' option: switch case of {motion} text. {Vi:
292 tilde cannot be used as an operator}
293
294 *g~*
295g~{motion} Switch case of {motion} text. {not in Vi}
296
297g~g~ *g~g~* *g~~*
298g~~ Switch case of current line. {not in Vi}.
299
300 *v_~*
301{Visual}~ Switch case of highlighted text (for {Visual} see
302 |Visual-mode|). {not in Vi}
303
304 *v_U*
305{Visual}U Make highlighted text uppercase (for {Visual} see
306 |Visual-mode|). {not in Vi}
307
308 *gU* *uppercase*
309gU{motion} Make {motion} text uppercase. {not in Vi}
310 Example: >
311 :map! <C-F> <Esc>gUiw`]a
312< This works in Insert mode: press CTRL-F to make the
313 word before the cursor uppercase. Handy to type
314 words in lowercase and then make them uppercase.
315
316
317gUgU *gUgU* *gUU*
318gUU Make current line uppercase. {not in Vi}.
319
320 *v_u*
321{Visual}u Make highlighted text lowercase (for {Visual} see
322 |Visual-mode|). {not in Vi}
323
324 *gu* *lowercase*
325gu{motion} Make {motion} text lowercase. {not in Vi}
326
327gugu *gugu* *guu*
328guu Make current line lowercase. {not in Vi}.
329
330 *g?* *rot13*
331g?{motion} Rot13 encode {motion} text. {not in Vi}
332
333 *v_g?*
334{Visual}g? Rot13 encode the highlighted text (for {Visual} see
335 |Visual-mode|). {not in Vi}
336
337g?g? *g?g?* *g??*
338g?? Rot13 encode current line. {not in Vi}.
339
340
341Adding and subtracting ~
342 *CTRL-A*
343CTRL-A Add [count] to the number or alphabetic character at
344 or after the cursor. {not in Vi}
345
346 *CTRL-X*
347CTRL-X Subtract [count] from the number or alphabetic
348 character at or after the cursor. {not in Vi}
349
350The CTRL-A and CTRL-X commands work for (signed) decimal numbers, unsigned
351octal and hexadecimal numbers and alphabetic characters. This depends on the
352'nrformats' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353- When 'nrformats' includes "octal", Vim considers numbers starting with a '0'
354 to be octal. Other numbers are decimal and may have a preceding minus sign.
355 If the cursor is on a number, the commands apply to that number; otherwise
356 Vim uses the number to the right of the cursor.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000357- When 'nrformats' includes "hex", Vim assumes numbers starting with '0x' or
358 '0X' are hexadecimal. The case of the rightmost letter in the number
359 determines the case of the resulting hexadecimal number. If there is no
360 letter in the current number, Vim uses the previously detected case.
361- When 'nrformats' includes "alpha", Vim will change the alphabetic character
362 under or after the cursor. This is useful to make lists with an alphabetic
363 index.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
365For numbers with leading zeros (including all octal and hexadecimal numbers),
366Vim preserves the number of characters in the number when possible. CTRL-A on
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000367"0077" results in "0100", CTRL-X on "0x100" results in "0x0ff".
368
369Note that when 'nrformats' includes "octal", decimal numbers with leading
370zeros are impossible because they are indistinguishable from octal numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371
372The CTRL-A command is very useful in a macro. Example: Use the following
373steps to make a numbered list.
374
3751. Create the first list entry, make sure it starts with a number.
3762. qa - start recording into buffer 'a'
3773. Y - yank the entry
3784. p - put a copy of the entry below the first one
3795. CTRL-A - increment the number
3806. q - stop recording
3817. <count>@a - repeat the yank, put and increment <count> times
382
383
384SHIFTING LINES LEFT OR RIGHT *shift-left-right*
385
386 *<*
387<{motion} Shift {motion} lines one 'shiftwidth' leftwards.
388
389 *<<*
390<< Shift [count] lines one 'shiftwidth' leftwards.
391
392 *v_<*
393{Visual}[count]< Shift the highlighted lines [count] 'shiftwidth'
394 leftwards (for {Visual} see |Visual-mode|). {not in
395 Vi}
396
397 *>*
398 >{motion} Shift {motion} lines one 'shiftwidth' rightwards.
399
400 *>>*
401 >> Shift [count] lines one 'shiftwidth' rightwards.
402
403 *v_>*
404{Visual}[count]> Shift the highlighted lines [count] 'shiftwidth'
405 rightwards (for {Visual} see |Visual-mode|). {not in
406 Vi}
407
408 *:<*
409:[range]< Shift [range] lines one 'shiftwidth' left. Repeat '<'
410 for shifting multiple 'shiftwidth's.
411
412:[range]< {count} Shift {count} lines one 'shiftwidth' left, starting
413 with [range] (default current line |cmdline-ranges|).
414 Repeat '<' for shifting multiple 'shiftwidth's.
415
416:[range]le[ft] [indent] left align lines in [range]. Sets the indent in the
417 lines to [indent] (default 0). {not in Vi}
418
419 *:>*
420:[range]> Shift {count} [range] lines one 'shiftwidth' right.
421 Repeat '>' for shifting multiple 'shiftwidth's.
422
423:[range]> {count} Shift {count} lines one 'shiftwidth' right, starting
424 with [range] (default current line |cmdline-ranges|).
425 Repeat '>' for shifting multiple 'shiftwidth's.
426
427The ">" and "<" commands are handy for changing the indentation within
428programs. Use the 'shiftwidth' option to set the size of the white space
429which these commands insert or delete. Normally the 'shiftwidth' option is 8,
430but you can set it to, say, 3 to make smaller indents. The shift leftwards
431stops when there is no indent. The shift right does not affect empty lines.
432
433If the 'shiftround' option is on, the indent is rounded to a multiple of
434'shiftwidth'.
435
436If the 'smartindent' option is on, or 'cindent' is on and 'cinkeys' contains
437'#', shift right does not affect lines starting with '#' (these are supposed
438to be C preprocessor lines that must stay in column 1).
439
440When the 'expandtab' option is off (this is the default) Vim uses <Tab>s as
441much as possible to make the indent. You can use ">><<" to replace an indent
442made out of spaces with the same indent made out of <Tab>s (and a few spaces
443if necessary). If the 'expandtab' option is on, Vim uses only spaces. Then
444you can use ">><<" to replace <Tab>s in the indent by spaces (or use
445":retab!").
446
447To move a line several 'shiftwidth's, use Visual mode or the ":" commands.
448For example: >
449 Vjj4> move three lines 4 indents to the right
450 :<<< move current line 3 indents to the left
451 :>> 5 move 5 lines 2 indents to the right
452 :5>> move line 5 2 indents to the right
453
454==============================================================================
4554. Complex changes *complex-change*
456
Bram Moolenaar47136d72004-10-12 20:02:24 +00004574.1 Filter commands *filter*
458
459A filter is a program that accepts text at standard input, changes it in some
460way, and sends it to standard output. You can use the commands below to send
461some text through a filter, so that it is replace by the filter output.
462Examples of filters are "sort", which sorts lines alphabetically, and
463"indent", which formats C program files (you need a version of indent that
464works like a filter; not all versions do). The 'shell' option specifies the
465shell Vim uses to execute the filter command (See also the 'shelltype'
466option). You can repeat filter commands with ".". Vim does not recognize a
467comment (starting with '"') after the ":!" command.
468
469 *!*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470!{motion}{filter} Filter {motion} text lines through the external
471 program {filter}.
472
473 *!!*
474!!{filter} Filter [count] lines through the external program
475 {filter}.
476
477 *v_!*
478{Visual}!{filter} Filter the highlighted lines through the external
479 program {filter} (for {Visual} see |Visual-mode|).
480 {not in Vi}
481
482:{range}![!]{filter} [!][arg] *:range!*
483 Filter {range} lines through the external program
484 {filter}. Vim replaces the optional bangs with the
485 latest given command and appends the optional [arg].
486 Vim saves the output of the filter command in a
487 temporary file and then reads the file into the
488 buffer. Vim uses the 'shellredir' option to redirect
489 the filter output to the temporary file.
490 When the 'R' flag is included in 'cpoptions' marks in
491 the filtered lines are deleted, unless the
492 |:keepmarks| command is used. Example: >
493 :keepmarks '<,'>!sort
494< When the number of lines after filtering is less than
495 before, marks in the missing lines are deleted anyway.
496
497 *=*
498={motion} Filter {motion} lines through the external program
499 given with the 'equalprg' option. When the 'equalprg'
500 option is empty (this is the default), use the
501 internal formatting function |C-indenting|. But when
502 'indentexpr' is not empty, it will be used instead
503 |indent-expression|.
504
505 *==*
506== Filter [count] lines like with ={motion}.
507
508 *v_=*
509{Visual}= Filter the highlighted lines like with ={motion}.
510 {not in Vi}
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar47136d72004-10-12 20:02:24 +00005134.2 Substitute *:substitute*
514 *:s* *:su*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515:[range]s[ubstitute]/{pattern}/{string}/[&][c][e][g][p][r][i][I] [count]
516 For each line in [range] replace a match of {pattern}
517 with {string}.
518 For the {pattern} see |pattern|.
519 {string} can be a literal string, or something
520 special; see |sub-replace-special|.
521 When [range] and [count] are omitted, replace in the
522 current line only.
523 When [count] is given, replace in [count] lines,
524 starting with the last line in [range]. When [range]
525 is omitted start in the current line.
526 Also see |cmdline-ranges|.
527 See |:s_flags| for the flags.
528
529:[range]s[ubstitute] [c][e][g][p][r][i][I] [count]
530:[range]&[&][c][e][g][p][r][i][I] [count] *:&*
531 Repeat last :substitute with same search pattern and
532 substitute string, but without the same flags. You
533 may add extra flags (see |:s_flags|).
534 Note that after ":substitute" the '&' flag can't be
535 used, it's recognized as a pattern separator.
536 The space between ":substitute" and the 'c', 'g' and
537 'r' flags isn't required, but in scripts it's a good
538 idea to keep it to avoid confusion.
539
540:[range]~[&][c][e][g][p][r][i][I] [count] *:~*
541 Repeat last substitute with same substitute string
542 but with last used search pattern. This is like
543 ":&r". See |:s_flags| for the flags.
544
545 *&*
546& Synonym for ":s//~/" (repeat last substitute). Note
547 that the flags are not remembered, thus it might
548 actually work differently. You can use ":&&" to keep
549 the flags.
550
551 *g&*
552g& Synonym for ":%s//~/&" (repeat last substitute on all
553 lines with the same flags).
554 Mnemonic: global substitute. {not in Vi}
555
556 *:snomagic* *:sno*
557:[range]sno[magic] ... Same as ":substitute", but always use 'nomagic'.
558 {not in Vi}
559
560 *:smagic* *:sm*
561:[range]sm[agic] ... Same as ":substitute", but always use 'magic'.
562 {not in Vi}
563
564 *:s_flags*
565The flags that you can use for the substitute commands:
566
567[&] Must be the first one: Keep the flags from the previous substitute
568 command. Examples: >
569 :&&
570 :s/this/that/&
571< Note that ":s" and ":&" don't keep the flags.
572 {not in Vi}
573
574[c] Confirm each substitution. Vim highlights the matching string (with
575 |hl-IncSearch|). You can type: *:s_c*
576 'y' to substitute this match
577 'l' to substitute this match and then quit ("last")
578 'n' to skip this match
579 <Esc> to quit substituting
580 'a' to substitute this and all remaining matches {not in Vi}
581 'q' to quit substituting {not in Vi}
582 CTRL-E to scroll the screen up {not in Vi, not available when
583 compiled without the +insert_expand feature}
584 CTRL-Y to scroll the screen down {not in Vi, not available when
585 compiled without the +insert_expand feature}
586 If the 'edcompatible' option is on, Vim remembers the [c] flag and
587 toggles it each time you use it, but resets it when you give a new
588 search pattern.
589 {not in Vi: highlighting of the match, other responses than 'y' or 'n'}
590
591[e] When the search pattern fails, do not issue an error message and, in
592 particular, continue in maps as if no error occurred. This is most
593 useful to prevent the "No match" error from breaking a mapping. Vim
594 does not suppress the following error messages, however:
595 Regular expressions can't be delimited by letters
596 \ should be followed by /, ? or &
597 No previous substitute regular expression
598 Trailing characters
599 Interrupted
600 {not in Vi}
601
602[g] Replace all occurrences in the line. Without this argument,
603 replacement occurs only for the first occurrence in each line. If
604 the 'edcompatible' option is on, Vim remembers this flag and toggles
605 it each time you use it, but resets it when you give a new search
606 pattern. If the 'gdefault' option is on, this flag is on by default
607 and the [g] argument switches it off.
608
609[i] Ignore case for the pattern. The 'ignorecase' and 'smartcase' options
610 are not used.
611 {not in Vi}
612
613[I] Don't ignore case for the pattern. The 'ignorecase' and 'smartcase'
614 options are not used.
615 {not in Vi}
616
617[p] Print the line containing the last substitute.
618 {not in Vi}
619
620[r] Only useful in combination with ":&" or ":s" without arguments. ":&r"
621 works the same way as ":~": When the search pattern is empty, use the
622 previously used search pattern instead of the search pattern from the
623 last substitute or ":global". If the last command that did a search
624 was a substitute or ":global", there is no effect. If the last
625 command was a search command such as "/", use the pattern from that
626 command.
627 For ":s" with an argument this already happens: >
628 :s/blue/red/
629 /green
630 :s//red/ or :~ or :&r
631< The last commands will replace "green" with "red". >
632 :s/blue/red/
633 /green
634 :&
635< The last command will replace "blue" with "red".
636 {not in Vi}
637
638Note that there is no flag to change the "magicness" of the pattern. A
639different command is used instead. The reason is that the flags can only be
640found by skipping the pattern, and in order to skip the pattern the
641"magicness" must be known. Catch 22!
642
643If the {pattern} for the substitute command is empty, the command uses the
644pattern from the last substitute or ":global" command. With the [r] flag, the
645command uses the pattern from the last substitute, ":global", or search
646command.
647
648For compatibility with Vi these two exceptions are allowed:
649"\/{string}/" and "\?{string}?" do the same as "//{string}/r".
650"\&{string}&" does the same as "//{string}/".
651 *E146*
652Instead of the '/' which surrounds the pattern and replacement string, you
653can use any other character, but not an alphanumeric character, '\', '"' or
654'|'. This is useful if you want to include a '/' in the search pattern or
655replacement string. Example: >
656 :s+/+//+
657
658For the definition of a pattern, see |pattern|.
659
660 *sub-replace-special* *:s\=*
661When the {string} starts with "\=" it is evaluated as an expression, see
662|sub-replace-expression|. Otherwise these characters in {string} have a
663special meaning:
664
665magic nomagic action ~
666 & \& replaced with the whole matched pattern *s/\&*
667 \& & replaced with &
668 \0 replaced with the whole matched pattern *\0* *s/\0*
669 \1 replaced with the matched pattern in the first
670 pair of () *s/\1*
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000671 \2 replaced with the matched pattern in the second
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 pair of () *s/\2*
673 .. .. *s/\3*
674 \9 replaced with the matched pattern in the ninth
675 pair of () *s/\9*
676 ~ \~ replaced with the {string} of the previous
677 substitute *s~*
678 \~ ~ replaced with ~ *s/\~*
679 \u next character made uppercase *s/\u*
680 \U following characters made uppercase, until \E *s/\U*
681 \l next character made lowercase *s/\l*
682 \L following characters made lowercase, until \E *s/\L*
683 \e end of \u, \U, \l and \L (NOTE: not <Esc>!) *s/\e*
684 \E end of \u, \U, \l and \L *s/\E*
685 <CR> split line in two at this point
686 (Type the <CR> as CTRL-V <Enter>) *s<CR>*
687 \r idem *s/\r*
688 \<CR> insert a carriage-return (CTRL-M)
689 (Type the <CR> as CTRL-V <Enter>) *s/\<CR>*
690 \n insert a <NL> (<NUL> in the file)
691 (does NOT break the line) *s/\n*
692 \b insert a <BS> *s/\b*
693 \t insert a <Tab> *s/\t*
694 \\ insert a single backslash *s/\\*
695 \x where x is any character not mentioned above:
696 Reserved for future expansion
697
698Examples: >
699 :s/a\|b/xxx\0xxx/g modifies "a b" to "xxxaxxx xxxbxxx"
700 :s/\([abc]\)\([efg]\)/\2\1/g modifies "af fa bg" to "fa fa gb"
701 :s/abcde/abc^Mde/ modifies "abcde" to "abc", "de" (two lines)
702 :s/$/\^M/ modifies "abcde" to "abcde^M"
703
704Note: In previous versions CTRL-V was handled in a special way. Since this is
705not Vi compatible, this was removed. Use a backslash instead.
706
707command text result ~
708:s/aa/a^Ma/ aa a<line-break>a
709:s/aa/a\^Ma/ aa a^Ma
710:s/aa/a\\^Ma/ aa a\<line-break>a
711
712(you need to type CTRL-V <CR> to get a ^M here)
713
714The numbering of "\1", "\2" etc. is done based on which "\(" comes first in
715the pattern (going left to right). When a parentheses group matches several
716times, the last one will be used for "\1", "\2", etc. Example: >
717 :s/\(\(a[a-d] \)*\)/\2/ modifies "aa ab x" to "ab x"
718
719When using parentheses in combination with '|', like in \([ab]\)\|\([cd]\),
720either the first or second pattern in parentheses did not match, so either
721\1 or \2 is empty. Example: >
722 :s/\([ab]\)\|\([cd]\)/\1x/g modifies "a b c d" to "ax bx x x"
723<
724
725Substitute with an expression *sub-replace-expression*
726
727When the substitute string starts with "\=" the remainer is interpreted as an
728expression. This does not work recursively: a substitute() function inside
729the expression cannot use "\=" for the substitute string.
730
731The special meaning for characters as mentioned at |sub-replace-special| does
732not apply except "<CR>", "\<CR>" and "\\". Thus in the result of the
733expression you need to use two backslashes get one, put a backslash before a
734<CR> you want to insert and use a <CR> without a backslash where you want to
735break the line.
736
737For convenience a <NL> character is also used as a line break. Prepend a
738backslash to get a real <NL> character (which will be a NUL in the file).
739
740The whole matched text can be accessed with "submatch(0)". The text matched
741with the first pair of () with "submatch(1)". Likewise for further
742sub-matches in ().
743
744Be careful: The separation character must not appear in the expression!
745Consider using a character like "@" or ":". There is no problem if the result
746of the expression contains the separation character.
747
748Example: >
749 :s@\n@\="\r" . expand("$HOME") . "\r"@
750This replaces an end-of-line with a new line containing the value of $HOME.
751
752
Bram Moolenaar47136d72004-10-12 20:02:24 +00007534.3 Search and replace *search-replace*
754
755 *:pro* *:promptfind*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756:promptf[ind] [string]
757 Put up a Search dialog. When [string] is given, it is
758 used as the initial search string.
759 {only for Win32, Motif and GTK GUI}
760
761 *:promptr* *:promptrepl*
762:promptr[epl] [string]
763 Put up a Search/Replace dialog. When [string] is
764 given, it is used as the initial search string.
765 {only for Win32, Motif and GTK GUI}
766
Bram Moolenaar47136d72004-10-12 20:02:24 +0000767
7684.4 Changing tabs *change-tabs*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 *:ret* *:retab*
770:[range]ret[ab][!] [new_tabstop]
771 Replace all sequences of white-space containing a
772 <Tab> with new strings of white-space using the new
773 tabstop value given. If you do not specify a new
774 tabstop size or it is zero, Vim uses the current value
775 of 'tabstop'.
776 The current value of 'tabstop' is always used to
777 compute the width of existing tabs.
778 With !, Vim also replaces strings of only normal
779 spaces with tabs where appropriate.
780 With 'expandtab' on, Vim replaces all tabs with the
781 appropriate number of spaces.
782 This command sets 'tabstop' to the new value given,
783 and if performed on the whole file, which is default,
784 should not make any visible change.
785 Careful: This command modifies any <Tab> characters
786 inside of strings in a C program. Use "\t" to avoid
787 this (that's a good habit anyway).
788 ":retab!" may also change a sequence of spaces by
789 <Tab> characters, which can mess up a printf().
790 {not in Vi}
791 Not available when |+ex_extra| feature was disabled at
792 compile time.
793
794 *retab-example*
795Example for using autocommands and ":retab" to edit a file which is stored
796with tabstops at 8 but edited with tabstops set at 4. Warning: white space
797inside of strings can change! Also see 'softtabstop' option. >
798
799 :auto BufReadPost *.xx retab! 4
800 :auto BufWritePre *.xx retab! 8
801 :auto BufWritePost *.xx retab! 4
802 :auto BufNewFile *.xx set ts=4
803
804==============================================================================
8055. Copying and moving text *copy-move*
806
807 *quote*
808"{a-zA-Z0-9.%#:-"} Use register {a-zA-Z0-9.%#:-"} for next delete, yank
809 or put (use uppercase character to append with
810 delete and yank) ({.%#:} only work with put).
811
812 *:reg* *:registers*
813:reg[isters] Display the contents of all numbered and named
814 registers. {not in Vi}
815
816:reg[isters] {arg} Display the contents of the numbered and named
817 registers that are mentioned in {arg}. For example: >
818 :dis 1a
819< to display registers '1' and 'a'. Spaces are allowed
820 in {arg}. {not in Vi}
821
822 *:di* *:display*
823:di[splay] [arg] Same as :registers. {not in Vi}
824
825 *y* *yank*
826["x]y{motion} Yank {motion} text [into register x]. When no
827 characters are to be yanked (e.g., "y0" in column 1),
828 this is an error when 'cpoptions' includes the 'E'
829 flag.
830
831 *yy*
832["x]yy Yank [count] lines [into register x] |linewise|.
833
834 *Y*
835["x]Y yank [count] lines [into register x] (synonym for
836 yy, |linewise|). If you like "Y" to work from the
837 cursor to the end of line (which is more logical,
838 but not Vi-compatible) use ":map Y y$".
839
840 *v_y*
841{Visual}["x]y Yank the highlighted text [into register x] (for
842 {Visual} see |Visual-mode|). {not in Vi}
843
844 *v_Y*
845{Visual}["x]Y Yank the highlighted lines [into register x] (for
846 {Visual} see |Visual-mode|). {not in Vi}
847
848 *:y* *:yank*
849:[range]y[ank] [x] Yank [range] lines [into register x].
850
851:[range]y[ank] [x] {count}
852 Yank {count} lines, starting with last line number
853 in [range] (default: current line |cmdline-ranges|),
854 [into register x].
855
856 *p* *put* *E353*
857["x]p Put the text [from register x] after the cursor
858 [count] times. {Vi: no count}
859
860 *P*
861["x]P Put the text [from register x] before the cursor
862 [count] times. {Vi: no count}
863
864 *<MiddleMouse>*
865["x]<MiddleMouse> Put the text from a register before the cursor [count]
866 times. Uses the "* register, unless another is
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000867 specified.
868 Leaves the cursor at the end of the new text.
869 Using the mouse only works when 'mouse' contains 'n'
870 or 'a'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 {not in Vi}
872 If you have a scrollwheel and often accidentally paste
873 text, you can use these mappings to disable the
874 pasting with the middle mouse button: >
875 :map <MiddleMouse> <Nop>
876 :imap <MiddleMouse> <Nop>
877< You might want to disable the multi-click versions
878 too, see |double-click|.
879
880 *gp*
881["x]gp Just like "p", but leave the cursor just after the new
882 text. {not in Vi}
883
884 *gP*
885["x]gP Just like "P", but leave the cursor just after the new
886 text. {not in Vi}
887
888 *:pu* *:put*
889:[line]pu[t] [x] Put the text [from register x] after [line] (default
890 current line). This always works |linewise|, thus
891 this command can be used to put a yanked block as new
892 lines.
893 The register can also be '=' followed by an optional
894 expression. The expression continues until the end of
895 the command. You need to escape the '|' and '"'
896 characters to prevent them from terminating the
897 command. Example: >
898 :put ='path' . \",/test\"
899< If there is no expression after '=', Vim uses the
900 previous expression. You can see it with ":dis =".
901
902:[line]pu[t]! [x] Put the text [from register x] before [line] (default
903 current line).
904
905["x]]p or *]p* *]<MiddleMouse>*
906["x]]<MiddleMouse> Like "p", but adjust the indent to the current line.
907 Using the mouse only works when 'mouse' contains 'n'
908 or 'a'. {not in Vi}
909
910["x][P or *[P*
911["x]]P or *]P*
912["x][p or *[p* *[<MiddleMouse>*
913["x][<MiddleMouse> Like "P", but adjust the indent to the current line.
914 Using the mouse only works when 'mouse' contains 'n'
915 or 'a'. {not in Vi}
916
917You can use these commands to copy text from one place to another. Do this
918by first getting the text into a register with a yank, delete or change
919command, then inserting the register contents with a put command. You can
920also use these commands to move text from one file to another, because Vim
921preserves all registers when changing buffers (the CTRL-^ command is a quick
922way to toggle between two files).
923
924 *linewise-register* *characterwise-register*
925You can repeat the put commands with "." (except for :put) and undo them. If
926the command that was used to get the text into the register was |linewise|,
927Vim inserts the text below ("p") or above ("P") the line where the cursor is.
928Otherwise Vim inserts the text after ("p") or before ("P") the cursor. With
929the ":put" command, Vim always inserts the text in the next line. You can
930exchange two characters with the command sequence "xp". You can exchange two
931lines with the command sequence "ddp". You can exchange two words with the
932command sequence "deep" (start with the cursor in the blank space before the
933first word). You can use the "']" or "`]" command after the put command to
934move the cursor to the end of the inserted text, or use "'[" or "`[" to move
935the cursor to the start.
936
937 *put-Visual-mode* *v_p* *v_P*
938When using a put command like |p| or |P| in Visual mode, Vim will try to
939replace the selected text with the contents of the register. Whether this
940works well depends on the type of selection and the type of the text in the
941register. With blockwise selection it also depends on the size of the block
942and whether the corners are on an existing character. (implementation detail:
943it actually works by first putting the register after the selection and then
944deleting the selection).
945
946 *blockwise-register*
947If you use a blockwise Visual mode command to get the text into the register,
948the block of text will be inserted before ("P") or after ("p") the cursor
949column in the current and next lines. Vim makes the whole block of text start
950in the same column. Thus the inserted text looks the same as when it was
951yanked or deleted. Vim may replace some <Tab> characters with spaces to make
952this happen. However, if the width of the block is not a multiple of a <Tab>
953width and the text after the inserted block contains <Tab>s, that text may be
954misaligned.
955
956Note that after a characterwise yank command, Vim leaves the cursor on the
957first yanked character that is closest to the start of the buffer. This means
958that "yl" doesn't move the cursor, but "yh" moves the cursor one character
959left.
960Rationale: In Vi the "y" command followed by a backwards motion would
961 sometimes not move the cursor to the first yanked character,
962 because redisplaying was skipped. In Vim it always moves to
963 the first character, as specified by Posix.
964With a linewise yank command the cursor is put in the first line, but the
965column is unmodified, thus it may not be on the first yanked character.
966
967There are nine types of registers: *registers* *E354*
9681. The unnamed register ""
9692. 10 numbered registers "0 to "9
9703. The small delete register "-
9714. 26 named registers "a to "z or "A to "Z
9725. four read-only registers ":, "., "% and "#
9736. the expression register "=
9747. The selection and drop registers "*, "+ and "~
9758. The black hole register "_
9769. Last search pattern register "/
977
9781. Unnamed register "" *quote_quote* *quotequote*
979Vim fills this register with text deleted with the "d", "c", "s", "x" commands
980or copied with the yank "y" command, regardless of whether or not a specific
Bram Moolenaared203462004-06-16 11:19:22 +0000981register was used (e.g. "xdd). This is like the unnamed register is pointing
982to the last used register. An exception is the '_' register: "_dd does not
983store the deleted text in any register. Vim uses the contents of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984register for any put command (p or P) which does not specify a register.
985Additionally you can access it with the name '"'. This means you have to type
986two double quotes. Writing to the "" register writes to register "0.
987{Vi: register contents are lost when changing files, no '"'}
988
9892. Numbered registers "0 to "9 *quote_number* *quote0* *quote1*
990 *quote2* *quote3* *quote4* *quote9*
991Vim fills these registers with text from yank and delete commands.
992 Numbered register 0 contains the text from the most recent yank command,
993unless the command specified another register with ["x].
994 Numbered register 1 contains the text deleted by the most recent delete or
995change command, unless the command specified another register or the text is
996less than one line (the small delete register is used then). An exception is
997made for these commands: |%|, |(|, |)|, |`|, |/|, |?|, |n|, |N|, |{| and |}|.
998Register "1 is always used then (this is Vi compatible). The "- register is
999used as well if the delete is within a line.
1000 With each successive deletion or change, Vim shifts the previous contents
1001of register 1 into register 2, 2 into 3, and so forth, losing the previous
1002contents of register 9.
1003{Vi: numbered register contents are lost when changing files; register 0 does
1004not exist}
1005
10063. Small delete register "- *quote_-* *quote-*
1007This register contains text from commands that delete less than one line,
1008except when the command specifies a register with ["x].
1009{not in Vi}
1010
10114. Named registers "a to "z or "A to "Z *quote_alpha* *quotea*
1012Vim fills these registers only when you say so. Specify them as lowercase
1013letters to replace their previous contents or as uppercase letters to append
1014to their previous contents.
1015
10165. Read-only registers ":, "., "% and "#
1017These are '%', '#', ':' and '.'. You can use them only with the "p", "P",
1018and ":put" commands and with CTRL-R. {not in Vi}
1019 *quote_.* *quote.* *E29*
1020 ". Contains the last inserted text (the same as what is inserted
1021 with the insert mode commands CTRL-A and CTRL-@). Note: this
1022 doesn't work with CTRL-R on the command-line. It works a bit
1023 differently, like inserting the text instead of putting it
1024 ('textwidth' and other options affect what is inserted).
1025 *quote_%* *quote%*
1026 "% Contains the name of the current file.
1027 *quote_#* *quote#*
1028 "# Contains the name of the alternate file.
1029 *quote_:* *quote:* *E30*
1030 ": Contains the most recent executed command-line. Example: Use
1031 "@:" to repeat the previous command-line command.
1032 The command-line is only stored in this register when at least
1033 one character of it was typed. Thus it remains unchanged if
1034 the command was completely from a mapping.
1035 {not available when compiled without the |+cmdline_hist|
1036 feature}
1037
10386. Expression register "= *quote_=* *quote=*
1039This is not really a register that stores text, but is a way to use an
1040expression in commands which use a register. The expression register is
1041read-only; you cannot put text into it. After the '=', the cursor moves to
1042the command-line, where you can enter any expression (see |expression|). All
1043normal command-line editing commands are available, including a special
1044history for expressions. When you end the command-line by typing <CR>, Vim
1045computes the result of the expression. If you end it with <Esc>, Vim abandons
1046the expression. If you do not enter an expression, Vim uses the previous
1047expression (like with the "/" command). If the "= register is used for the
1048"p" command, the string is split up at <NL> characters. If the string ends in
1049a <NL>, it is regarded as a linewise register. {not in Vi}
1050
10517. Selection and drop registers "*, "+ and "~
1052Use these register for storing and retrieving the selected text for the GUI.
1053See |quotestar| and |quoteplus|. When the clipboard is not available or not
1054working, the unnamed register is used instead. {not in Vi}
1055
1056Note that there is only a distinction between "* and "+ for X11 systems. For
1057an explanation of the difference, see |x11-selection|. Under MS-Windows, use
1058of "* and "+ is actually synonymous and refers to the |gui-clipboard|.
1059
1060 *quote_~* *quote~* *<Drop>*
1061The read-only "~ register stores the dropped text from the last drag'n'drop
1062operation. When something has been dropped onto Vim, the "~ register is
1063filled in and the <Drop> pseudo key is sent for notification. You can remap
1064this key if you want; the default action (for all modes) is to insert the
1065contents of the "~ register at the cursor position. {not in Vi}
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001066{only available when compiled with the |+dnd| feature, currently only with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067GTK GUI}
1068
1069Note: The "~ register is only used when dropping plain text onto Vim.
1070Drag'n'drop of URI lists is handled internally.
1071
10728. Black hole register "_ *quote_*
1073When writing to this register, nothing happens. This can be used to delete
1074text without affecting the normal registers. When reading from this register,
1075nothing is returned. {not in Vi}
1076
10779. Last search pattern register "/ *quote_/* *quote/*
1078Contains the most recent search-pattern. This is used for "n" and 'hlsearch'.
1079It is writable with ":let", you can change it to have 'hlsearch' highlight
1080other matches without actually searching. You can't yank or delete into this
1081register. {not in Vi}
1082
1083 *@/*
1084You can write to a register with a ":let" command |:let-@|. Example: >
1085 :let @/ = "the"
1086
1087If you use a put command without specifying a register, Vim uses the register
1088that was last filled (this is also the contents of the unnamed register). If
1089you are confused, use the ":dis" command to find out what Vim will put (this
1090command displays all named and numbered registers; the unnamed register is
1091labelled '"').
1092
1093The next three commands always work on whole lines.
1094
1095:[range]co[py] {address} *:co* *:copy*
1096 Copy the lines given by [range] to below the line
1097 given by {address}.
1098
1099 *:t*
1100:t Synonym for copy.
1101
1102:[range]m[ove] {address} *:m* *:mo* *:move* *E134*
1103 Move the lines given by [range] to below the line
1104 given by {address}.
1105
1106==============================================================================
11076. Formatting text *formatting*
1108
1109:[range]ce[nter] [width] *:ce* *:center*
1110 Center lines in [range] between [width] columns
1111 (default 'textwidth' or 80 when 'textwidth' is 0).
1112 {not in Vi}
1113 Not available when |+ex_extra| feature was disabled at
1114 compile time.
1115
1116:[range]ri[ght] [width] *:ri* *:right*
1117 Right-align lines in [range] at [width] columns
1118 (default 'textwidth' or 80 when 'textwidth' is 0).
1119 {not in Vi}
1120 Not available when |+ex_extra| feature was disabled at
1121 compile time.
1122
1123 *:le* *:left*
1124:[range]le[ft] [indent]
1125 Left-align lines in [range]. Sets the indent in the
1126 lines to [indent] (default 0). {not in Vi}
1127 Not available when |+ex_extra| feature was disabled at
1128 compile time.
1129
1130 *gq*
1131gq{motion} Format the lines that {motion} moves over. The
1132 'textwidth' option controls the length of each
1133 formatted line (see below). If the 'textwidth' option
1134 is 0, the formatted line length is the screen width
1135 (with a maximum width of 79). {not in Vi}
1136 The 'formatoptions' option controls the type of
1137 formatting |fo-table|.
1138 NOTE: The "Q" command formerly performed this
1139 function. If you still want to use "Q" for
1140 formatting, use this mapping: >
1141 :nnoremap Q gq
1142
1143gqgq *gqgq* *gqq*
1144gqq Format the current line. {not in Vi}
1145
1146 *v_gq*
1147{Visual}gq Format the highlighted text. (for {Visual} see
1148 |Visual-mode|). {not in Vi}
1149
1150 *gw*
1151gw{motion} Format the lines that {motion} moves over. Similar to
1152 |gq| but puts the cursor back at the same position in
1153 the text. However, 'formatprg' is not used.
1154 {not in Vi}
1155
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001156gwgw *gwgw* *gww*
1157gww Format the current line as with "gw". {not in Vi}
1158
1159 *v_gw*
1160{Visual}gw Format the highlighted text as with "gw". (for
1161 {Visual} see |Visual-mode|). {not in Vi}
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163Example: To format the current paragraph use: *gqap* >
1164 gqap
1165
1166The "gq" command leaves the cursor in the line where the motion command takes
1167the cursor. This allows you to repeat formatting repeated with ".". This
1168works well with "gqj" (format current and next line) and "gq}" (format until
1169end of paragraph). Note: When 'formatprg' is set, "gq" leaves the cursor on
1170the first formatted line (as with using a filter command).
1171
1172If you want to format the current paragraph and continue where you were, use: >
1173 gwap
1174If you always want to keep paragraphs formatted you may want to add the 'a'
1175flag to 'formatoptions'. See |auto-format|.
1176
1177If the 'autoindent' option is on, Vim uses the indent of the first line for
1178the following lines.
1179
1180Formatting does not change empty lines (but it does change lines with only
1181white space!).
1182
1183The 'joinspaces' option is used when lines are joined together.
1184
1185You can set the 'formatprg' option to the name of an external program for Vim
1186to use for text formatting. The 'textwidth' and other options have no effect
1187on formatting by an external program.
1188
1189 *right-justify*
1190There is no command in Vim to right justify text. You can do it with
1191an external command, like "par" (e.g.: "!}par" to format until the end of the
1192paragraph) or set 'formatprg' to "par".
1193
1194 *format-comments*
1195Vim can format comments in a special way. Vim recognizes a comment by a
1196specific string at the start of the line (ignoring white space). Three types
1197of comments can be used:
1198
1199- A comment string that repeats at the start of each line. An example is the
1200 type of comment used in shell scripts, starting with "#".
1201- A comment string that occurs only in the first line, not in the following
1202 lines. An example is this list with dashes.
1203- Three-piece comments that have a start string, an end string, and optional
1204 lines in between. The strings for the start, middle and end are different.
1205 An example is the C-style comment:
1206 /*
1207 * this is a C comment
1208 */
1209
1210The 'comments' option is a comma-separated list of parts. Each part defines a
1211type of comment string. A part consists of:
1212 {flags}:{string}
1213
1214{string} is the literal text that must appear.
1215
1216{flags}:
1217 n Nested comment. Nesting with mixed parts is allowed. If 'comments'
1218 is "n:),n:>" a line starting with "> ) >" is a comment.
1219
1220 b Blank (<Space>, <Tab> or <EOL>) required after {string}.
1221
1222 f Only the first line has the comment string. Do not repeat comment on
1223 the next line, but preserve indentation (e.g., a bullet-list).
1224
1225 s Start of three-piece comment
1226
1227 m Middle of a three-piece comment
1228
1229 e End of a three-piece comment
1230
1231 l Left adjust middle with start or end (default). Only recognized when
1232 used together with 's' or 'e'.
1233
1234 r Right adjust middle with start or end. Only recognized when used
1235 together with 's' or 'e'.
1236
1237 O Don't use this one for the "O" command.
1238
1239 x Allows three-piece comments to be ended by just typing the last
1240 character of the end-comment string as the first character on a new
1241 line, when the middle-comment string has already been inserted
1242 automatically. See below for more details.
1243
1244 {digits}
1245 When together with 's' or 'e': add extra indent for the middle part.
1246 This can be used to left-align the middle part with the start or end
1247 and then add an offset.
1248
1249 -{digits}
1250 Like {digits} but reduce the indent. This only works when there is
1251 some indent for the start or end part that can be removed.
1252
1253When a string has none of the 'f', 's', 'm' or 'e' flags, Vim assumes the
1254comment string repeats at the start of each line. The flags field may be
1255empty.
1256
1257Any blank space in the text before and after the {string} is part of the
1258{string}, so do not include leading or trailing blanks unless the blanks are a
1259required part of the comment string.
1260
1261When one comment leader is part of another, specify the part after the whole.
1262For example, to include both "-" and "->", use >
1263 :set comments=f:->,f:-
1264
1265A three-piece comment must always be given as start,middle,end, with no other
1266parts in between. An example of a three-piece comment is >
1267 sr:/*,mb:*,ex:*/
1268for C-comments. To avoid recognizing "*ptr" as a comment, the middle string
1269includes the 'b' flag. For three-piece comments, Vim checks the text after
1270the start and middle strings for the end string. If Vim finds the end string,
1271the comment does not continue on the next line. Three-piece comments must
1272have a middle string because otherwise Vim can't recognize the middle lines.
1273
1274Notice the use of the "x" flag in the above three-piece comment definition.
1275When you hit Return in a C-comment, Vim will insert the middle comment leader
1276for the new line, e.g. " * ". To close this comment you just have to type "/"
1277before typing anything else on the new line. This will replace the
1278middle-comment leader with the end-comment leader, leaving just " */". There
1279is no need to hit BackSpace first.
1280
1281Examples: >
1282 "b:*" Includes lines starting with "*", but not if the "*" is
1283 followed by a non-blank. This avoids a pointer dereference
1284 like "*str" to be recognized as a comment.
1285 "n:>" Includes a line starting with ">", ">>", ">>>", etc.
1286 "fb:-" Format a list that starts with "- ".
1287
1288By default, "b:#" is included. This means that a line that starts with
1289"#include" is not recognized as a comment line. But a line that starts with
1290"# define" is recognized. This is a compromise.
1291
1292Often the alignment can be changed from right alignment to a left alignment
1293with an additional space. For example, for Javadoc comments, this can be
1294used (insert a backslash before the space when using ":set"): >
1295 s1:/*,mb:*,ex:*/
1296Note that an offset is included with start, so that the middle part is left
1297aligned with the start and then an offset of one character added. This makes
1298it possible to left align the start and middle for this construction: >
1299 /**
1300 * comment
1301 */
1302
1303{not available when compiled without the |+comments| feature}
1304
1305 *fo-table*
1306You can use the 'formatoptions' option to influence how Vim formats text.
1307'formatoptions' is a string that can contain any of the letters below. The
1308default setting is "tcq". You can separate the option letters with commas for
1309readability.
1310
1311letter meaning when present in 'formatoptions' ~
1312
1313t Auto-wrap text using textwidth (does not apply to comments)
1314c Auto-wrap comments using textwidth, inserting the current comment
1315 leader automatically.
1316r Automatically insert the current comment leader after hitting
1317 <Enter> in Insert mode.
1318o Automatically insert the current comment leader after hitting 'o' or
1319 'O' in Normal mode.
1320q Allow formatting of comments with "gq".
1321 Note that formatting will not change blank lines or lines containing
1322 only the comment leader. A new paragraph starts after such a line,
1323 or when the comment leader changes.
1324w Trailing white space indicates a paragraph continues in the next line.
1325 A line that ends in a non-white character ends a paragraph.
1326a Automatic formatting of paragraphs. Every time text is inserted or
1327 deleted the paragraph will be reformatted. See |auto-format|.
1328 When the 'c' flag is present this only happens for recognized
1329 comments.
1330n When formatting text, recognize numbered lists. The indent of the
1331 text after the number is used for the next line. The number may
1332 optionally be followed by '.', ':', ')', ']' or '}'. Note that
1333 'autoindent' must be set too. Doesn't work well together with "2".
1334 Example: >
1335 1. the first item
1336 wraps
1337 2. the second item
13382 When formatting text, use the indent of the second line of a paragraph
1339 for the rest of the paragraph, instead of the indent of the first
1340 line. This supports paragraphs in which the first line has a
1341 different indent than the rest. Note that 'autoindent' must be set
1342 too. Example: >
1343 first line of a paragraph
1344 second line of the same paragraph
1345 third line.
1346v Vi-compatible auto-wrapping in insert mode: Only break a line at a
1347 blank that you have entered during the current insert command. (Note:
1348 this is not 100% Vi compatible. Vi has some "unexpected features" or
1349 bugs in this area. It uses the screen column instead of the line
1350 column.)
1351b Like 'v', but only auto-wrap if you enter a blank at or before
1352 the wrap margin. If the line was longer than 'textwidth' when you
1353 started the insert, or you do not enter a blank in the insert before
1354 reaching 'textwidth', Vim does not perform auto-wrapping.
1355l Long lines are not broken in insert mode: When a line was longer than
1356 'textwidth' when the insert command started, Vim does not
1357 automatically format it.
1358m Also break at a multi-byte character above 255. This is useful for
1359 Asian text where every character is a word on its own.
1360M When joining lines, don't insert a space before or after a multi-byte
1361 character. Overrules the 'B' flag.
1362B When joining lines, don't insert a space between two multi-byte
1363 characters. Overruled by the 'M' flag.
13641 Don't break a line after a one-letter word. It's broken before it
1365 instead (if possible).
1366
1367
1368With 't' and 'c' you can specify when Vim performs auto-wrapping:
1369value action ~
1370"" no automatic formatting (you can use "gq" for manual formatting)
1371"t" automatic formatting of text, but not comments
1372"c" automatic formatting for comments, but not text (good for C code)
1373"tc" automatic formatting for text and comments
1374
1375Note that when 'textwidth' is 0, Vim does no formatting anyway (but does
1376insert comment leaders according to the 'comments' option).
1377
1378Note that when 'paste' is on, Vim does no formatting at all.
1379
1380Note that 'textwidth' can be non-zero even if Vim never performs auto-wrapping;
1381'textwidth' is still useful for formatting with "gq".
1382
1383If the 'comments' option includes "/*", "*" and/or "*/", then Vim has some
1384built in stuff to treat these types of comments a bit more cleverly.
1385Opening a new line before or after "/*" or "*/" (with 'r' or 'o' present in
1386'formatoptions') gives the correct start of the line automatically. The same
1387happens with formatting and auto-wrapping. Opening a line after a line
1388starting with "/*" or "*" and containing "*/", will cause no comment leader to
1389be inserted, and the indent of the new line is taken from the line containing
1390the start of the comment.
1391E.g.:
1392 /* ~
1393 * Your typical comment. ~
1394 */ ~
1395 The indent on this line is the same as the start of the above
1396 comment.
1397
1398All of this should be really cool, especially in conjunction with the new
1399:autocmd command to prepare different settings for different types of file.
1400
1401Some examples:
1402 for C code (only format comments): >
1403 :set fo=croq
1404< for Mail/news (format all, don't start comment with "o" command): >
1405 :set fo=tcrq
1406<
1407
1408Automatic formatting *auto-format*
1409
1410When the 'a' flag is present in 'formatoptions' text is formatted
1411automatically when inserting text or deleting text. This works nice for
1412editing text paragraphs. A few hints on how to use this:
1413
1414- You need to properly define paragraphs. The simplest is paragraphs that are
1415 separated by a blank line. When there is no separating blank line, consider
1416 using the 'w' flag and adding a space at the end of each line in the
1417 paragraphs except the last one.
1418
1419- You can set the 'formatoptions' based on the type of file |filetype| or
1420 specifically for one file with a |modeline|.
1421
1422- Set 'formatoptions' to "aw2tq" to make text with indents like this:
1423
1424 bla bla foobar bla
1425 bla foobar bla foobar bla
1426 bla bla foobar bla
1427 bla foobar bla bla foobar
1428
1429- Add the 'c' flag to only auto-format comments. Useful in source code.
1430
1431And a few warnings:
1432
1433- When part of the text is not properly separated in paragraphs, making
1434 changes in this text will cause it to be formatted anyway. Consider doing >
1435
1436 :set fo-=a
1437
1438- When using the 'w' flag (trailing space means paragraph continues) and
1439 deleting the last line of a paragraph with |dd|, the paragraph will be
1440 joined with the next one.
1441
1442- Changed text is saved for undo. Formatting is also a change. Thus each
1443 format action saves text for undo. This may consume quite a lot of memory.
1444
1445- Formatting a long paragraph and/or with complicated indenting may be slow.
1446
1447
1448 vim:tw=78:ts=8:ft=help:norl: