blob: 579068c5865dfd060ee1c78e0d65d96a9385a176 [file] [log] [blame]
Bram Moolenaar06fb4352005-01-05 22:10:30 +00001*insert.txt* For Vim version 7.0aa. Last change: 2005 Jan 05
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7 *Insert* *Insert-mode*
8Inserting and replacing text *mode-ins-repl*
9
10Most of this file is about Insert and Replace mode. At the end are a few
11commands for inserting text in other ways.
12
13An overview of the most often used commands can be found in chapter 24 of the
14user manual |usr_24.txt|.
15
161. Special keys |ins-special-keys|
172. Special special keys |ins-special-special|
183. 'textwidth' and 'wrapmargin' options |ins-textwidth|
194. 'expandtab', 'smarttab' and 'softtabstop' options |ins-expandtab|
205. Replace mode |Replace-mode|
216. Virtual Replace mode |Virtual-Replace-mode|
227. Insert mode completion |ins-completion|
238. Insert mode commands |inserting|
249. Ex insert commands |inserting-ex|
2510. Inserting a file |inserting-file|
26
27Also see 'virtualedit', for moving the cursor to positions where there is no
28character. Useful for editing a table.
29
30==============================================================================
311. Special keys *ins-special-keys*
32
33In Insert and Replace mode, the following characters have a special meaning;
34other characters are inserted directly. To insert one of these special
35characters into the buffer, precede it with CTRL-V. To insert a <Nul>
36character use "CTRL-V CTRL-@" or "CTRL-V 000". On some systems, you have to
37use "CTRL-V 003" to insert a CTRL-C. Note: When CTRL-V is mapped you can
38often use CTRL-Q instead |i_CTRL-Q|.
39
40If you are working in a special language mode when inserting text, see the
41'langmap' option, |'langmap'|, on how to avoid switching this mode on and off
42all the time.
43
44If you have 'insertmode' set, <Esc> and a few other keys get another meaning.
45See |'insertmode'|.
46
47char action ~
48-----------------------------------------------------------------------
49 *i_CTRL-[* *i_<Esc>*
50<Esc> or CTRL-[ End insert or Replace mode, go back to Normal mode. Finish
51 abbreviation.
52 Note: If your <Esc> key is hard to hit on your keyboard, train
53 yourself to use CTRL-[.
54 *i_CTRL-C*
55CTRL-C Quit insert mode, go back to Normal mode. Do not check for
56 abbreviations.
57
58 *i_CTRL-@*
59CTRL-@ Insert previously inserted text and stop insert. {Vi: only
60 when typed as first char, only up to 128 chars}
61 *i_CTRL-A*
62CTRL-A Insert previously inserted text. {not in Vi}
63
64 *i_CTRL-H* *i_<BS>* *i_BS*
65<BS> or CTRL-H Delete the character before the cursor (see |i_backspacing|
66 about joining lines).
67 See |:fixdel| if your <BS> key does not do what you want.
68 {Vi: does not delete autoindents}
69 *i_<Del>* *i_DEL*
70<Del> Delete the character under the cursor. If the cursor is at
71 the end of the line, and the 'backspace' option includes
72 "eol", delete the <EOL>; the next line is appended after the
73 current one.
74 See |:fixdel| if your <Del> key does not do what you want.
75 {not in Vi}
76 *i_CTRL-W*
77CTRL-W Delete the word before the cursor (see |i_backspacing| about
78 joining lines). See the section "word motions",
79 |word-motions|, for the definition of a word.
80 *i_CTRL-U*
81CTRL-U Delete all entered characters in the current line (see
82 |i_backspacing| about joining lines).
83
84 *i_CTRL-I* *i_<Tab>* *i_Tab*
85<Tab> or CTRL-I Insert a tab. If the 'expandtab' option is on, the
86 equivalent number of spaces is inserted (use CTRL-V <Tab> to
87 avoid the expansion; use CTRL-Q <Tab> if CTRL-V is mapped
88 |i_CTRL-Q|). See also the 'smarttab' option and
89 |ins-expandtab|.
90 *i_CTRL-J* *i_<NL>*
91<NL> or CTRL-J Begin new line.
92 *i_CTRL-M* *i_<CR>*
93<CR> or CTRL-M Begin new line.
94 *i_CTRL-K*
95CTRL-K {char1} [char2]
96 Enter digraph (see |digraphs|). When {char1} is a special
97 key, the code for that key is inserted in <> form. For
98 example, the string "<S-Space>" can be entered by typing
99 <C-K><S-Space> (two keys). Neither char is considered for
100 mapping. {not in Vi}
101
102CTRL-N Find next keyword (see |i_CTRL-N|). {not in Vi}
103CTRL-P Find previous keyword (see |i_CTRL-P|). {not in Vi}
104
105CTRL-R {0-9a-z"%#*+:.-=} *i_CTRL-R*
106 Insert the contents of a register. Between typing CTRL-R and
107 the second character, '"' will be displayed to indicate that
108 you are expected to enter the name of a register.
109 The text is inserted as if you typed it, but mappings and
110 abbreviations are not used. If you have options like
111 'textwidth', 'formatoptions', or 'autoindent' set, this will
112 influence what will be inserted. This is different from what
113 happens with the "p" command and pasting with the mouse.
114 Special registers:
115 '"' the unnamed register, containing the text of
116 the last delete or yank
117 '%' the current file name
118 '#' the alternate file name
119 '*' the clipboard contents (X11: primary selection)
120 '+' the clipboard contents
121 '/' the last search pattern
122 ':' the last command-line
123 '.' the last inserted text
124 '-' the last small (less than a line) delete
125 '=' the expression register: you are prompted to
126 enter an expression (see |expression|)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000127 Note that 0x80 (128 decimal) is used for
128 special keys, use CTRL-R CTRL-R to insert it
129 literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 See |registers| about registers. {not in Vi}
131
132CTRL-R CTRL-R {0-9a-z"%#*+/:.-=} *i_CTRL-R_CTRL-R*
133 Insert the contents of a register. Works like using a single
134 CTRL-R, but the text is inserted literally, not as if typed.
135 This differs when the register contains characters like <BS>.
136 Example, where register a contains "ab^Hc": >
137 CTRL-R a results in "ac".
138 CTRL-R CTRL-R a results in "ab^Hc".
139< Options 'textwidth', 'formatoptions', etc. still apply. If
140 you also want to avoid these, use "<C-R><C-O>r", see below.
141 The '.' register (last inserted text) is still inserted as
142 typed. {not in Vi}
143
144CTRL-R CTRL-O {0-9a-z"%#*+/:.-=} *i_CTRL-R_CTRL-O*
145 Insert the contents of a register literally and don't
146 auto-indent. Does the same as pasting with the mouse
147 |<MiddleMouse>|.
148 Does not replace characters!
149 The '.' register (last inserted text) is still inserted as
150 typed. {not in Vi}
151
152CTRL-R CTRL-P {0-9a-z"%#*+/:.-=} *i_CTRL-R_CTRL-P*
153 Insert the contents of a register literally and fix the
154 indent, like |[<MiddleMouse>|.
155 Does not replace characters!
156 The '.' register (last inserted text) is still inserted as
157 typed. {not in Vi}
158
159 *i_CTRL-T*
160CTRL-T Insert one shiftwidth of indent at the start of the current
161 line. The indent is always rounded to a 'shiftwidth' (this is
162 vi compatible). {Vi: only when in indent}
163 *i_CTRL-D*
164CTRL-D Delete one shiftwidth of indent at the start of the current
165 line. The indent is always rounded to a 'shiftwidth' (this is
166 vi compatible). {Vi: CTRL-D works only when used after
167 autoindent}
168 *i_0_CTRL-D*
1690 CTRL-D Delete all indent in the current line. {Vi: CTRL-D works
170 only when used after autoindent}
171 *i_^_CTRL-D*
172^ CTRL-D Delete all indent in the current line. The indent is
173 restored in the next line. This is useful when inserting a
174 label. {Vi: CTRL-D works only when used after autoindent}
175
176 *i_CTRL-V*
177CTRL-V Insert next non-digit literally. For special keys, the
178 terminal code is inserted. It's also possible to enter the
179 decimal, octal or hexadecimal value of a character
180 |i_CTRL-V_digit|.
181 The characters typed right after CTRL-V are not considered for
182 mapping. {Vi: no decimal byte entry}
183 Note: When CTRL-V is mapped (e.g., to paste text) you can
184 often use CTRL-Q instead |i_CTRL-Q|.
185
186 *i_CTRL-Q*
187CTRL-Q Same as CTRL-V.
188 Note: Some terminal connections may eat CTRL-Q, it doesn't
189 work then. It does work in the GUI.
190
191CTRL-X Enter CTRL-X mode. This is a sub-mode where commands can
192 be given to complete words or scroll the window. See
193 |i_CTRL-X| and |ins-completion|. {not in Vi}
194
195 *i_CTRL-E*
196CTRL-E Insert the character which is below the cursor. {not in Vi}
197 *i_CTRL-Y*
198CTRL-Y Insert the character which is above the cursor. {not in Vi}
199 Note that for CTRL-E and CTRL-Y 'textwidth' is not used, to be
200 able to copy characters from a long line.
201
202 *i_CTRL-_*
203CTRL-_ Switch between languages, as follows:
204 - When in a rightleft window, revins and nohkmap are toggled,
205 since English will likely be inserted in this case.
206 - When in a norightleft window, revins and hkmap are toggled,
207 since Hebrew will likely be inserted in this case.
208
209 CTRL-_ moves the cursor to the end of the typed text.
210
211 This command is only available when the 'allowrevins' option
212 is set.
213 Please refer to |rileft.txt| for more information about
214 right-to-left mode.
215 {not in Vi}
216 Only if compiled with the |+rightleft| feature (which is not
217 the default).
218 *i_CTRL-^*
219CTRL-^ Toggle the use of typing language characters.
220 When language |:lmap| mappings are defined:
221 - If 'iminsert' is 1 (langmap mappings used) it becomes 0 (no
222 langmap mappings used).
223 - If 'iminsert' has another value it becomes 1, thus langmap
224 mappings are enabled.
225 When no language mappings are defined:
226 - If 'iminsert' is 2 (Input Method used) it becomes 0 (no
227 Input Method used).
228 - If 'iminsert' has another value it becomes 2, thus the Input
229 Method is enabled.
230 When set to 1, the value of the "b:keymap_name" variable, the
231 'keymap' option or "<lang>" appears in the status line.
232 The language mappings are normally used to type characters
233 that are different from what the keyboard produces. The
234 'keymap' option can be used to install a whole number of them.
235 {not in Vi}
236
237 *i_CTRL-]*
238CTRL-] Trigger abbreviation, without inserting a character. {not in
239 Vi}
240
241 *i_<Insert>*
242<Insert> Toggle between Insert and Replace mode. {not in Vi}
243-----------------------------------------------------------------------
244
245 *i_backspacing*
246The effect of the <BS>, CTRL-W, and CTRL-U depend on the 'backspace' option
247(unless 'revins' is set). This is a comma separated list of items:
248
249item action ~
250indent allow backspacing over autoindent
251eol allow backspacing over end-of-line (join lines)
252start allow backspacing over the start position of insert; CTRL-W and
253 CTRL-U stop once at the start position
254
255When 'backspace' is empty, Vi compatible backspacing is used. You cannot
256backspace over autoindent, before column 1 or before where insert started.
257
258For backwards compatibility the values "0", "1" and "2" are also allowed, see
259|'backspace'|.
260
261If the 'backspace' option does contain "eol" and the cursor is in column 1
262when one of the three keys is used, the current line is joined with the
263previous line. This effectively deletes the <EOL> in front of the cursor.
264{Vi: does not cross lines, does not delete past start position of insert}
265
266 *i_CTRL-V_digit*
267With CTRL-V the decimal, octal or hexadecimal value of a character can be
268entered directly. This way you can enter any character, except a line break
269(<NL>, value 10). There are five ways to enter the character value:
270
271first char mode max nr of chars max value ~
272(none) decimal 3 255
273o or O octal 3 255
274x or X hexadecimal 2 ff (255)
275u hexadecimal 4 ffff (65535)
276U hexadecimal 8 7fffffff (2147483647)
277
278Normally you would type the maximum number of characters. Thus to enter a
279space (value 32) you would type <C-V>032. You can omit the leading zero, in
280which case the character typed after the number must be a non-digit. This
281happens for the other modes as well: As soon as you type a character that is
282invalid for the mode, the value before it will be used and the "invalid"
283character is dealt with in the normal way.
284
285If you enter a value of 10, it will end up in the file as a 0. The 10 is a
286<NL>, which is used internally to represent the <Nul> character. When writing
287the buffer to a file, the <NL> character is translated into <Nul>. The <NL>
288character is written at the end of each line. Thus if you want to insert a
289<NL> character in a file you will have to make a line break.
290
291 *i_CTRL-X* *insert_expand*
292CTRL-X enters a sub-mode where several commands can be used. Most of these
293commands do keyword completion; see |ins-completion|. These are not available
294when Vim was compiled without the |+insert_expand| feature.
295
296Two commands can be used to scroll the window up or down, without exiting
297insert mode:
298
299 *i_CTRL-X_CTRL-E*
300CTRL-X CTRL-E scroll window one line up.
301
302 *i_CTRL-X_CTRL-Y*
303CTRL-X CTRL-Y scroll window one line down.
304
305After CTRL-X is pressed, each CTRL-E (CTRL-Y) scrolls the window up (down) by
306one line unless that would cause the cursor to move from its current position
307in the file. As soon as another key is pressed, CTRL-X mode is exited and
308that key is interpreted as in Insert mode.
309
310
311==============================================================================
3122. Special special keys *ins-special-special*
313
314The following keys are special. They stop the current insert, do something,
315and then restart insertion. This means you can do something without getting
316out of Insert mode. This is very handy if you prefer to use the Insert mode
317all the time, just like editors that don't have a separate Normal mode. You
318may also want to set the 'backspace' option to "indent,eol,start" and set the
319'insertmode' option. You can use CTRL-O if you want to map a function key to
320a command.
321
322The changes (inserted or deleted characters) before and after these keys can
323be undone separately. Only the last change can be redone and always behaves
324like an "i" command.
325
326char action ~
327-----------------------------------------------------------------------
328<Up> cursor one line up *i_<Up>*
329<Down> cursor one line down *i_<Down>*
330CTRL-G <Up> cursor one line up, insert start column *i_CTRL-G_<Up>*
331CTRL-G k cursor one line up, insert start column *i_CTRL-G_k*
332CTRL-G CTRL-K cursor one line up, insert start column *i_CTRL-G_CTRL-K*
333CTRL-G <Down> cursor one line down, insert start column *i_CTRL-G_<Down>*
334CTRL-G j cursor one line down, insert start column *i_CTRL-G_j*
335CTRL-G CTRL-J cursor one line down, insert start column *i_CTRL-G_CTRL-J*
336<Left> cursor one character left *i_<Left>*
337<Right> cursor one character right *i_<Right>*
338<S-Left> cursor one word back (like "b" command) *i_<S-Left>*
339<C-Left> cursor one word back (like "b" command) *i_<C-Left>*
340<S-Right> cursor one word forward (like "w" command) *i_<S-Right>*
341<C-Right> cursor one word forward (like "w" command) *i_<C-Right>*
342<Home> cursor to first char in the line *i_<Home>*
343<End> cursor to after last char in the line *i_<End>*
344<C-Home> cursor to first char in the file *i_<C-Home>*
345<C-End> cursor to after last char in the file *i_<C-End>*
346<LeftMouse> cursor to position of mouse click *i_<LeftMouse>*
347<S-Up> move window one page up *i_<S-Up>*
348<PageUp> move window one page up *i_<PageUp>*
349<S-Down> move window one page down *i_<S-Down>*
350<PageDown> move window one page down *i_<PageDown>*
351<MouseDown> scroll three lines down *i_<MouseDown>*
352<S-MouseDown> scroll a full page down *i_<S-MouseDown>*
353<MouseUp> scroll three lines up *i_<MouseUp>*
354<S-MouseUp> scroll a full page up *i_<S-MouseUp>*
355CTRL-O execute one command, return to Insert mode *i_CTRL-O*
356CTRL-G u break undo sequence, start new change *i_CTRL-G_u*
357-----------------------------------------------------------------------
358
359Note: If the cursor keys take you out of Insert mode, check the 'noesckeys'
360option.
361
362The CTRL-O command sometimes has a side effect: If the cursor was beyond the
363end of the line, it will be put on the last character in the line. In
364mappings it's often better to use <Esc> (first put an "x" in the text, <Esc>
365will then always put the cursor on it).
366
367The shifted cursor keys are not available on all terminals.
368
369Another side effect is that a count specified before the "i" or "a" command is
370ignored. That is because repeating the effect of the command after CTRL-O is
371too complicated.
372
373An example for using CTRL-G u: >
374
375 :inoremap <C-H> <C-G>u<C-H>
376
377This redefines the backspace key to start a new undo sequence. You can now
378undo the effect of the backspace key, without changing what you typed before
379that, with CTRL-O u.
380
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000381Using CTRL-O splits undo: the text typed before and after it is undone
382separately. If you want to avoid this (e.g., in a mapping) you might be able
383to use CTRL-R = |i_CTRL-R|. E.g., to call a function: >
384 :imap <F2> <C-R>=MyFunc()<CR>
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386When the 'whichwrap' option is set appropriately, the <Left> and <Right>
387keys on the first/last character in the line make the cursor wrap to the
388previous/next line.
389
390The CTRL-G j and CTRL-G k commands can be used to insert text in front of a
391column. Example: >
392 int i;
393 int j;
394Position the cursor on the first "int", type "istatic<C-G>j ". The
395result is: >
396 static int i;
397 int j;
398When inserting the same text in front of the column in every line, use the
399Visual blockwise command "I" |v_b_I|.
400
401==============================================================================
4023. 'textwidth' and 'wrapmargin' options *ins-textwidth*
403
404The 'textwidth' option can be used to automatically break a line before it
405gets too long. Set the 'textwidth' option to the desired maximum line
406length. If you then type more characters (not spaces or tabs), the
407last word will be put on a new line (unless it is the only word on the
408line). If you set 'textwidth' to 0, this feature is disabled.
409
410The 'wrapmargin' option does almost the same. The difference is that
411'textwidth' has a fixed width while 'wrapmargin' depends on the width of the
412screen. When using 'wrapmargin' this is equal to using 'textwidth' with a
413value equal to (columns - 'wrapmargin'), where columns is the width of the
414screen.
415
416When 'textwidth' and 'wrapmargin' are both set, 'textwidth' is used.
417
418If you don't really want to break the line, but view the line wrapped at a
419convenient place, see the 'linebreak' option.
420
421The line is only broken automatically when using insert mode, or when
422appending to a line. When in replace mode and the line length is not
423changed, the line will not be broken.
424
425Long lines are broken if you enter a non-white character after the margin.
426The situations where a line will be broken can be restricted by adding
427characters to the 'formatoptions' option:
428"l" Only break a line if it was not longer than 'textwidth' when the insert
429 started.
430"v" Only break at a white character that has been entered during the
431 current insert command. This is mostly Vi-compatible.
432"lv" Only break if the line was not longer than 'textwidth' when the insert
433 started and only at a white character that has been entered during the
434 current insert command. Only differs from "l" when entering non-white
435 characters while crossing the 'textwidth' boundary.
436
437If you want to format a block of text, you can use the "gq" operator. Type
438"gq" and a movement command to move the cursor to the end of the block. In
439many cases, the command "gq}" will do what you want (format until the end of
440paragraph). Alternatively, you can use "gqap", which will format the whole
441paragraph, no matter where the cursor currently is. Or you can use Visual
442mode: hit "v", move to the end of the block, and type "gq". See also |gq|.
443
444==============================================================================
4454. 'expandtab', 'smarttab' and 'softtabstop' options *ins-expandtab*
446
447If the 'expandtab' option is on, spaces will be used to fill the amount of
448whitespace of the tab. If you want to enter a real <Tab>, type CTRL-V first
449(use CTRL-Q when CTRL-V is mapped |i_CTRL-Q|).
450The 'expandtab' option is off by default. Note that in Replace mode, a single
451character is replaced with several spaces. The result of this is that the
452number of characters in the line increases. Backspacing will delete one
453space at a time. The original character will be put back for only one space
454that you backspace over (the last one). {Vi does not have the 'expandtab'
455option}
456
457 *ins-smarttab*
458When the 'smarttab' option is on, a <Tab> inserts 'shiftwidth' positions at
459the beginning of a line and 'tabstop' positions in other places. This means
460that often spaces instead of a <Tab> character are inserted. When 'smarttab
461is off, a <Tab> always inserts 'tabstop' positions, and 'shiftwidth' is only
462used for ">>" and the like. {not in Vi}
463
464 *ins-softtabstop*
465When the 'softtabstop' option is non-zero, a <Tab> inserts 'softtabstop'
466positions, and a <BS> used to delete white space, will delete 'softtabstop'
467positions. This feels like 'tabstop' was set to 'softtabstop', but a real
468<Tab> character still takes 'tabstop' positions, so your file will still look
469correct when used by other applications.
470
471If 'softtabstop' is non-zero, a <BS> will try to delete as much white space to
472move to the previous 'softtabstop' position, except when the previously
473inserted character is a space, then it will only delete the character before
474the cursor. Otherwise you cannot always delete a single character before the
475cursor. You will have to delete 'softtabstop' characters first, and then type
476extra spaces to get where you want to be.
477
478==============================================================================
4795. Replace mode *Replace* *Replace-mode* *mode-replace*
480
481Enter Replace mode with the "R" command in normal mode.
482
483In Replace mode, one character in the line is deleted for every character you
484type. If there is no character to delete (at the end of the line), the
485typed character is appended (as in Insert mode). Thus the number of
486characters in a line stays the same until you get to the end of the line.
487If a <NL> is typed, a line break is inserted and no character is deleted.
488
489Be careful with <Tab> characters. If you type a normal printing character in
490its place, the number of characters is still the same, but the number of
491columns will become smaller.
492
493If you delete characters in Replace mode (with <BS>, CTRL-W, or CTRL-U), what
494happens is that you delete the changes. The characters that were replaced
495are restored. If you had typed past the existing text, the characters you
496added are deleted. This is effectively a character-at-a-time undo.
497
498If the 'expandtab' option is on, a <Tab> will replace one character with
499several spaces. The result of this is that the number of characters in the
500line increases. Backspacing will delete one space at a time. The original
501character will be put back for only one space that you backspace over (the
502last one). {Vi does not have the 'expandtab' option}
503
504==============================================================================
5056. Virtual Replace mode *vreplace-mode* *Virtual-Replace-mode*
506
507Enter Virtual Replace mode with the "gR" command in normal mode.
508{not available when compiled without the +vreplace feature}
509{Vi does not have Virtual Replace mode}
510
511Virtual Replace mode is similar to Replace mode, but instead of replacing
512actual characters in the file, you are replacing screen real estate, so that
513characters further on in the file never appear to move.
514
515So if you type a <Tab> it may replace several normal characters, and if you
516type a letter on top of a <Tab> it may not replace anything at all, since the
517<Tab> will still line up to the same place as before.
518
519Typing a <NL> still doesn't cause characters later in the file to appear to
520move. The rest of the current line will be replaced by the <NL> (that is,
521they are deleted), and replacing continues on the next line. A new line is
522NOT inserted unless you go past the end of the file.
523
524Interesting effects are seen when using CTRL-T and CTRL-D. The characters
525before the cursor are shifted sideways as normal, but characters later in the
526line still remain still. CTRL-T will hide some of the old line under the
527shifted characters, but CTRL-D will reveal them again.
528
529As with Replace mode, using <BS> etc will bring back the characters that were
530replaced. This still works in conjunction with 'smartindent', CTRL-T and
531CTRL-D, 'expandtab', 'smarttab', 'softtabstop', etc.
532
533In 'list' mode, Virtual Replace mode acts as if it was not in 'list' mode,
534unless "L" is in 'cpoptions'.
535
536Note that the only times characters beyond the cursor should appear to move
537are in 'list' mode, and occasionally when 'wrap' is set (and the line changes
538length to become shorter or wider than the width of the screen), or
539momentarily when typing over a CTRL character. A CTRL character takes up two
540screen spaces. When replacing it with two normal characters, the first will
541be inserted and the second will replace the CTRL character.
542
543This mode is very useful for editing <Tab> separated columns in tables, for
544entering new data while keeping all the columns aligned.
545
546==============================================================================
5477. Insert mode completion *ins-completion*
548
549In Insert and Replace modes, there are several commands to complete part of a
550keyword or line that has been typed. This is useful if you are using
551complicated keywords (e.g., function names with capitals and underscores).
552
553These commands are not available when the |+insert_expand| feature was
554disabled at compile time.
555
556Completion can be done for:
557
5581. Whole lines |i_CTRL-X_CTRL-L|
5592. keywords in the current file |i_CTRL-X_CTRL-N|
5603. keywords in 'dictionary' |i_CTRL-X_CTRL-K|
5614. keywords in 'thesaurus', thesaurus-style |i_CTRL-X_CTRL-T|
5625. keywords in the current and included files |i_CTRL-X_CTRL-I|
5636. tags |i_CTRL-X_CTRL-]|
5647. file names |i_CTRL-X_CTRL-F|
5658. definitions or macros |i_CTRL-X_CTRL-D|
5669. Vim command-line |i_CTRL-X_CTRL-V|
56710. keywords in 'complete' |i_CTRL-N|
568
569All these (except 2) are done in CTRL-X mode. This is a sub-mode of Insert
570and Replace modes. You enter CTRL-X mode by typing CTRL-X and one of the
571CTRL-X commands. You exit CTRL-X mode by typing a key that is not a valid
572CTRL-X mode command. Valid keys are the CTRL-X command itself, CTRL-N (next),
573and CTRL-P (previous).
574
575Also see the 'infercase' option if you want to adjust the case of the match.
576
577Note: The keys that are valid in CTRL-X mode are not mapped. This allows for
578":map ^F ^X^F" to work (where ^F is CTRL-F and ^X is CTRL-X). The key that
579ends CTRL-X mode (any key that is not a valid CTRL-X mode command) is mapped.
580Also, when doing completion with 'complete' mappings apply as usual.
581
582The following mappings are suggested to make typing the completion commands
583a bit easier (although they will hide other commands): >
584 :inoremap ^] ^X^]
585 :inoremap ^F ^X^F
586 :inoremap ^D ^X^D
587 :inoremap ^L ^X^L
588
589As a special case, typing CTRL-R to perform register insertion (see
590|i_CTRL-R|) will not exit CTRL-X mode. This is primarily to allow the use of
591the '=' register to call some function to determine the next operation. If
592the contents of the register (or result of the '=' register evaluation) are
593not valid CTRL-X mode keys, then CTRL-X mode will be exited as if those keys
594had been typed.
595
596For example, the following will map <Tab> to either actually insert a <Tab> if
597the current line is currently only whitespace, or start/continue a CTRL-N
598completion operation: >
599
600 function! CleverTab()
601 if strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
602 return "\<Tab>"
603 else
604 return "\<C-N>"
605 endfunction
606 inoremap <Tab> <C-R>=CleverTab()<CR>
607
608
609
610Completing whole lines *compl-whole-line*
611
612 *i_CTRL-X_CTRL-L*
613CTRL-X CTRL-L Search backwards for a line that starts with the
614 same characters as in the current line before the
615 cursor. Indent is ignored. The found line is
616 inserted in front of the cursor.
617 The 'complete' option is used to decide in which
618 buffers a match is searched for. But only loaded
619 buffers are used.
620 CTRL-L or
621 CTRL-P Search backwards for next matching line. This line
622 replaces the previous matching line.
623
624 CTRL-N Search forward for next matching line. This line
625 replaces the previous matching line.
626
627 CTRL-X CTRL-L After expanding a line you can additionally get the
628 line next to it by typing CTRL-X CTRL-L again, unless
629 a double CTRL-X is used.
630
631Completing keywords in current file *compl-current*
632
633 *i_CTRL-X_CTRL-P*
634 *i_CTRL-X_CTRL-N*
635CTRL-X CTRL-N Search forwards for words that start with the keyword
636 in front of the cursor. The found keyword is inserted
637 in front of the cursor.
638
639CTRL-X CTRL-P Search backwards for words that start with the keyword
640 in front of the cursor. The found keyword is inserted
641 in front of the cursor.
642
643 CTRL-N Search forward for next matching keyword. This
644 keyword replaces the previous matching keyword.
645
646 CTRL-P Search backwards for next matching keyword. This
647 keyword replaces the previous matching keyword.
648
649 CTRL-X CTRL-N or
650 CTRL-X CTRL-P Further use of CTRL-X CTRL-N or CTRL-X CTRL-P will
651 copy the words following the previous expansion in
652 other contexts unless a double CTRL-X is used.
653
654If there is a keyword in front of the cursor (a name made out of alphabetic
655characters and characters in 'iskeyword'), it is used as the search pattern,
656with "\<" prepended (meaning: start of a word). Otherwise "\<\k\k" is used
657as search pattern (start of any keyword of at least two characters).
658
659In Replace mode, the number of characters that are replaced depends on the
660length of the matched string. This works like typing the characters of the
661matched string in Replace mode.
662
663If there is not a valid keyword character before the cursor, any keyword of
664at least two characters is matched.
665 e.g., to get:
666 printf("(%g, %g, %g)", vector[0], vector[1], vector[2]);
667 just type:
668 printf("(%g, %g, %g)", vector[0], ^P[1], ^P[2]);
669
670Multiple repeats of the same completion are skipped; thus a different match
671will be inserted at each CTRL-N and CTRL-P (unless there is only one
672matching keyword).
673
674Single character matches are never included, as they usually just get in
675the way of what you were really after.
676 e.g., to get:
677 printf("name = %s\n", name);
678 just type:
679 printf("name = %s\n", n^P);
680 or even:
681 printf("name = %s\n", ^P);
682The 'n' in '\n' is skipped.
683
684After expanding a word, you can use CTRL-X CTRL-P or CTRL-X CTRL-N to get the
685word following the expansion in other contexts. These sequences search for
686the text just expanded and further expand by getting an extra word. This is
687useful if you need to repeat a sequence of complicated words. Although CTRL-P
688and CTRL-N look just for strings of at least two characters, CTRL-X CTRL-P and
689CTRL-X CTRL-N can be used to expand words of just one character.
690 e.g., to get:
691 M&eacute;xico
692 you can type:
693 M^N^P^X^P^X^P
694CTRL-N starts the expansion and then CTRL-P takes back the single character
695"M", the next two CTRL-X CTRL-P's get the words "&eacute" and ";xico".
696
697If the previous expansion was split, because it got longer than 'textwidth',
698then just the text in the current line will be used.
699
700If the match found is at the end of a line, then the first word in the next
701line will be inserted and the message "word from next line" displayed, if
702this word is accepted the next CTRL-X CTRL-P or CTRL-X CTRL-N will search
703for those lines starting with this word.
704
705
706Completing keywords in 'dictionary' *compl-dictionary*
707
708 *i_CTRL-X_CTRL-K*
709CTRL-X CTRL-K Search the files given with the 'dictionary' option
710 for words that start with the keyword in front of the
711 cursor. This is like CTRL-N, but only the dictionary
712 files are searched, not the current file. The found
713 keyword is inserted in front of the cursor. This
714 could potentially be pretty slow, since all matches
715 are found before the first match is used. By default,
716 the 'dictionary' option is empty.
717 For suggestions where to find a list of words, see the
718 'dictionary' option.
719
720 CTRL-K or
721 CTRL-N Search forward for next matching keyword. This
722 keyword replaces the previous matching keyword.
723
724 CTRL-P Search backwards for next matching keyword. This
725 keyword replaces the previous matching keyword.
726
727 *i_CTRL-X_CTRL-T*
728CTRL-X CTRL-T Works as CTRL-X CTRL-K, but in a special way. It uses
729 the 'thesaurus' option instead of 'dictionary'. If a
730 match is found in the thesaurus file, all the
731 remaining words on the same line are included as
732 matches, even though they don't complete the word.
733 Thus a word can be completely replaced.
734
735 For an example, imagine the 'thesaurus' file has a
736 line like this: >
737 angry furious mad enraged
738< Placing the cursor after the letters "ang" and typing
739 CTRL-X CTRL-T would complete the word "angry";
740 subsequent presses would change the word to "furious",
741 "mad" etc.
742 Other uses include translation between two languages,
743 or grouping API functions by keyword.
744
745 CTRL-T or
746 CTRL-N Search forward for next matching keyword. This
747 keyword replaces the previous matching keyword.
748
749 CTRL-P Search backwards for next matching keyword. This
750 keyword replaces the previous matching keyword.
751
752
753Completing keywords in the current and included files *compl-keyword*
754
755The 'include' option is used to specify a line that contains an include file
756name. The 'path' option is used to search for include files.
757
758 *i_CTRL-X_CTRL-I*
759CTRL-X CTRL-I Search for the first keyword in the current and
760 included files that starts with the same characters
761 as those before the cursor. The matched keyword is
762 inserted in front of the cursor.
763
764 CTRL-N Search forwards for next matching keyword. This
765 keyword replaces the previous matching keyword.
766 Note: CTRL-I is the same as <Tab>, which is likely to
767 be typed after a successful completion, therefore
768 CTRL-I is not used for searching for the next match.
769
770 CTRL-P Search backward for previous matching keyword. This
771 keyword replaces the previous matching keyword.
772
773 CTRL-X CTRL-I Further use of CTRL-X CTRL-I will copy the words
774 following the previous expansion in other contexts
775 unless a double CTRL-X is used.
776
777Completing tags *compl-tag*
778 *i_CTRL-X_CTRL-]*
779CTRL-X CTRL-] Search for the first tag that starts with the same
780 characters as before the cursor. The matching tag is
781 inserted in front of the cursor. Alphabetic
782 characters and characters in 'iskeyword' are used
783 to decide which characters are included in the tag
784 name (same as for a keyword). See also |CTRL-]|.
785 The 'showfulltag' option can be used to add context
786 from around the tag definition.
787 CTRL-] or
788 CTRL-N Search forwards for next matching tag. This tag
789 replaces the previous matching tag.
790
791 CTRL-P Search backward for previous matching tag. This tag
792 replaces the previous matching tag.
793
794
795Completing file names *compl-filename*
796 *i_CTRL-X_CTRL-F*
797CTRL-X CTRL-F Search for the first file name that starts with the
798 same characters as before the cursor. The matching
799 file name is inserted in front of the cursor.
800 Alphabetic characters and characters in 'isfname'
801 are used to decide which characters are included in
802 the file name. Note: the 'path' option is not used
803 here (yet).
804 CTRL-F or
805 CTRL-N Search forwards for next matching file name. This
806 file name replaces the previous matching file name.
807
808 CTRL-P Search backward for previous matching file name.
809 This file name replaces the previous matching file
810 name.
811
812
813Completing definitions or macros *compl-define*
814
815The 'define' option is used to specify a line that contains a definition.
816The 'include' option is used to specify a line that contains an include file
817name. The 'path' option is used to search for include files.
818
819 *i_CTRL-X_CTRL-D*
820CTRL-X CTRL-D Search in the current and included files for the
821 first definition (or macro) name that starts with
822 the same characters as before the cursor. The found
823 definition name is inserted in front of the cursor.
824 CTRL-D or
825 CTRL-N Search forwards for next matching macro name. This
826 macro name replaces the previous matching macro
827 name.
828
829 CTRL-P Search backward for previous matching macro name.
830 This macro name replaces the previous matching macro
831 name.
832
833 CTRL-X CTRL-D Further use of CTRL-X CTRL-D will copy the words
834 following the previous expansion in other contexts
835 unless a double CTRL-X is used.
836
837
838Completing Vim commands *compl-vim*
839
840Completion is context-sensitive. It works like on the Command-line. It
841completes an Ex command as well as its arguments.
842
843 *i_CTRL-X_CTRL-V*
844CTRL-X CTRL-V Guess what kind of item is in front of the cursor and
845 find the first match for it.
846 Note: When CTRL-V is mapped you can often use CTRL-Q
847 instead |i_CTRL-Q|.
848 CTRL-V or
849 CTRL-N Search forwards for next match. This match replaces
850 the previous one.
851
852 CTRL-P Search backward for previous match. This match
853 replaces the previous one.
854
855 CTRL-X CTRL-V Further use of CTRL-X CTRL-V will do the same as
856 CTRL-V. This allows mapping a key to do Vim command
857 completion, for example: >
858 :imap <Tab> <C-X><C-V>
859
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000860User defined completing *compl-function*
861
862Completion is done by a function that can be defined by the user with the
863'completefunc' option. See the option for how the function is called and an
864example.
865
866 *i_CTRL-X_CTRL-U*
867CTRL-X CTRL-U Guess what kind of item is in front of the cursor and
868 find the first match for it.
869 CTRL-U or
870 CTRL-N Use the next match. This match replaces the previous
871 one.
872
873 CTRL-P Use the previous match. This match replaces the
874 previous one.
875
876
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877Completing keywords from different sources *compl-generic*
878
879 *i_CTRL-N*
880CTRL-N Find next match for words that start with the
881 keyword in front of the cursor, looking in places
882 specified with the 'complete' option. The found
883 keyword is inserted in front of the cursor.
884
885 *i_CTRL-P*
886CTRL-P Find previous match for words that start with the
887 keyword in front of the cursor, looking in places
888 specified with the 'complete' option. The found
889 keyword is inserted in front of the cursor.
890
891 CTRL-N Search forward for next matching keyword. This
892 keyword replaces the previous matching keyword.
893
894 CTRL-P Search backwards for next matching keyword. This
895 keyword replaces the previous matching keyword.
896
897 CTRL-X CTRL-N or
898 CTRL-X CTRL-P Further use of CTRL-X CTRL-N or CTRL-X CTRL-P will
899 copy the words following the previous expansion in
900 other contexts unless a double CTRL-X is used.
901
902==============================================================================
9038. Insert mode commands *inserting*
904
905The following commands can be used to insert new text into the buffer. They
906can all be undone and repeated with the "." command.
907
908 *a*
909a Append text after the cursor [count] times. If the
910 cursor is in the first column of an empty line Insert
911 starts there. But not when 'virtualedit' is set!
912
913 *A*
914A Append text at the end of the line [count] times.
915
916<insert> or *i* *insert* *<Insert>*
917i Insert text before the cursor [count] times.
918 When using CTRL-O in Insert mode |i_CTRL-O| the count
919 is not supported.
920
921 *I*
922I Insert text before the first non-blank in the line
923 [count] times.
924
925 *gI*
926gI Insert text in column 1 [count] times. {not in Vi}
927
928 *gi*
929gi Insert text in the same position as where Insert mode
930 was stopped last time in the current buffer.
931 This uses the |'^| mark. It's different from "`^i"
932 when the mark is past the end of the line.
933 The position is corrected for inserted/deleted lines,
934 but NOT for inserted/deleted characters.
935 When the |:keepjumps| command modifier is used the |'^|
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000936 mark won't be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {not in Vi}
938
939 *o*
940o Begin a new line below the cursor and insert text,
941 repeat [count] times. {Vi: blank [count] screen
942 lines}
943
944 *O*
945O Begin a new line above the cursor and insert text,
946 repeat [count] times. {Vi: blank [count] screen
947 lines}
948
949These commands are used to start inserting text. You can end insert mode with
950<Esc>. See |mode-ins-repl| for the other special characters in Insert mode.
951The effect of [count] takes place after Insert mode is exited.
952
953When 'autoindent' is on, the indent for a new line is obtained from the
954previous line. When 'smartindent' or 'cindent' is on, the indent for a line
955is automatically adjusted for C programs.
956
957'textwidth' can be set to the maximum width for a line. When a line becomes
958too long when appending characters a line break is automatically inserted.
959
960
961==============================================================================
9629. Ex insert commands *inserting-ex*
963
964 *:a* *:append*
965:{range}a[ppend] Insert several lines of text below the specified
966 line. If the {range} is missing, the text will be
967 inserted after the current line.
968
969 *:i* *:in* *:insert*
970:{range}i[nsert] Insert several lines of text above the specified
971 line. If the {range} is missing, the text will be
972 inserted before the current line.
973
974These two commands will keep on asking for lines, until you type a line
975containing only a ".". Watch out for lines starting with a backslash, see
976|line-continuation|.
977NOTE: ":append" and ":insert" don't work properly in between ":if" and
Bram Moolenaar06fb4352005-01-05 22:10:30 +0000978":endif", ":for" and ":endfor", ":while" and ":endwhile".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 *:start* *:startinsert*
981:star[tinsert][!] Start Insert mode just after executing this command.
982 Works like typing "i" in Normal mode. When the ! is
983 included it works like "A", append to the line.
984 Otherwise insertion starts at the cursor position.
985 Note that when using this command in a function or
986 script, the insertion only starts after the function
987 or script is finished.
988 {not in Vi}
989 {not available when compiled without the +ex_extra
990 feature}
991
992 *:stopi* *:stopinsert*
993:stopi[nsert] Stop Insert mode as soon as possible. Works like
994 typing <Esc> in Insert mode.
995 Can be used in an autocommand, example: >
996 :au BufEnter scratch stopinsert
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000997<
998 *replacing-ex* *:startreplace*
999:startr[eplace][!] Start Replace mode just after executing this command.
1000 Works just like typing "R" in Normal mode. When the
1001 ! is included it acts just like "$R" had been typed
1002 (ie. begin replace mode at the end-of-line). Other-
1003 wise replacement begins at the cursor position.
1004 Note that when using this command in a function or
1005 script that the replacement will only start after
1006 the function or script is finished.
1007 {not in Vi}
1008 {not available when compiled without the +ex_extra
1009 feature}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011==============================================================================
101210. Inserting a file *inserting-file*
1013
1014 *:r* *:re* *:read*
1015:r[ead] [name] Insert the file [name] (default: current file) below
1016 the cursor.
1017
1018:{range}r[ead] [name] Insert the file [name] (default: current file) below
1019 the specified line.
1020
1021 *:r!* *:read!*
1022:r[ead] !{cmd} Execute {cmd} and insert its standard output below
1023 the cursor. A temporary file is used to store the
1024 output of the command which is then read into the
1025 buffer. 'shellredir' is used to save the output of
1026 the command, which can be set to include stderr or
1027 not. {cmd} is executed like with ":!{cmd}", any '!'
1028 is replaced with the previous command |:!|.
1029
1030These commands insert the contents of a file, or the output of a command,
1031into the buffer. They can be undone. They cannot be repeated with the "."
1032command. They work on a line basis, insertion starts below the line in which
1033the cursor is, or below the specified line. To insert text above the first
1034line use the command ":0r {name}".
1035
1036After the ":read" command, the cursor is left on the first non-blank in the
1037first new line. Unless in Ex mode, then the cursor is left on the last new
1038line (sorry, this is Vi compatible).
1039
1040If a file name is given with ":r", it becomes the alternate file. This can be
1041used, for example, when you want to edit that file instead: ":e! #". This can
1042be switched off by removing the 'a' flag from the 'cpoptions' option.
1043
1044 *file-read*
1045The 'fileformat' option sets the <EOL> style for a file:
1046'fileformat' characters name ~
1047 "dos" <CR><NL> or <NL> DOS format
1048 "unix" <NL> Unix format
1049 "mac" <CR> Mac format
1050Previously 'textmode' was used. It is obsolete now.
1051
1052If 'fileformat' is "dos", a <CR> in front of an <NL> is ignored and a CTRL-Z
1053at the end of the file is ignored.
1054
1055If 'fileformat' is "mac", a <NL> in the file is internally represented by a
1056<CR>. This is to avoid confusion with a <NL> which is used to represent a
1057<NUL>. See |CR-used-for-NL|.
1058
1059If the 'fileformats' option is not empty Vim tries to recognize the type of
1060<EOL> (see |file-formats|). However, the 'fileformat' option will not be
1061changed, the detected format is only used while reading the file.
1062A similar thing happens with 'fileencodings'.
1063
1064On non-MS-DOS, Win32, and OS/2 systems the message "[dos format]" is shown if
1065a file is read in DOS format, to remind you that something unusual is done.
1066On Macintosh, MS-DOS, Win32, and OS/2 the message "[unix format]" is shown if
1067a file is read in Unix format.
1068On non-Macintosh systems, the message "[Mac format]" is shown if a file is
1069read in Mac format.
1070
1071An example on how to use ":r !": >
1072 :r !uuencode binfile binfile
1073This command reads "binfile", uuencodes it and reads it into the current
1074buffer. Useful when you are editing e-mail and want to include a binary
1075file.
1076
1077 *read-messages*
1078When reading a file Vim will display a message with information about the read
1079file. In the table is an explanation for some of the items. The others are
1080self explanatory. Using the long or the short version depends on the
1081'shortmess' option.
1082
1083 long short meaning ~
1084 [readonly] {RO} the file is write protected
1085 [fifo/socket] using a stream
1086 [fifo] using a fifo stream
1087 [socket] using a socket stream
1088 [CR missing] reading with "dos" 'fileformat' and a
1089 NL without a preceding CR was found.
1090 [NL found] reading with "mac" 'fileformat' and a
1091 NL was found (could be "unix" format)
1092 [long lines split] at least one line was split in two
1093 [NOT converted] conversion from 'fileencoding' to
1094 'encoding' was desired but not
1095 possible
1096 [converted] conversion from 'fileencoding' to
1097 'encoding' done
1098 [crypted] file was decrypted
1099 [READ ERRORS] not all of the file could be read
1100
1101
1102 vim:tw=78:ts=8:ft=help:norl: