blob: 9a961ef7f79478b0141dc110920cbf7b713c227d [file] [log] [blame]
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00001*usr_24.txt* For Vim version 7.0. Last change: 2006 Apr 24
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3 VIM USER MANUAL - by Bram Moolenaar
4
5 Inserting quickly
6
7
8When entering text, Vim offers various ways to reduce the number of keystrokes
9and avoid typing mistakes. Use Insert mode completion to repeat previously
10typed words. Abbreviate long words to short ones. Type characters that
11aren't on your keyboard.
12
13|24.1| Making corrections
14|24.2| Showing matches
15|24.3| Completion
16|24.4| Repeating an insert
17|24.5| Copying from another line
18|24.6| Inserting a register
19|24.7| Abbreviations
20|24.8| Entering special characters
21|24.9| Digraphs
22|24.10| Normal mode commands
23
24 Next chapter: |usr_25.txt| Editing formatted text
25 Previous chapter: |usr_23.txt| Editing other files
26Table of contents: |usr_toc.txt|
27
28==============================================================================
29*24.1* Making corrections
30
31The <BS> key was already mentioned. It deletes the character just before the
32cursor. The <Del> key does the same for the character under (after) the
33cursor.
34 When you typed a whole word wrong, use CTRL-W:
35
36 The horse had fallen to the sky ~
37 CTRL-W
38 The horse had fallen to the ~
39
40If you really messed up a line and want to start over, use CTRL-U to delete
41it. This keeps the text after the cursor and the indent. Only the text from
42the first non-blank to the cursor is deleted. With the cursor on the "f" of
43"fallen" in the next line pressing CTRL-U does this:
44
45 The horse had fallen to the ~
46 CTRL-U
47 fallen to the ~
48
49When you spot a mistake a few words back, you need to move the cursor there to
50correct it. For example, you typed this:
51
52 The horse had follen to the ground ~
53
54You need to change "follen" to "fallen". With the cursor at the end, you
55would type this to correct it: >
56
57 <Esc>4blraA
58
59< get out of Insert mode <Esc>
60 four words back 4b
61 move on top of the "o" l
62 replace with "a" ra
63 restart Insert mode A
64
65Another way to do this: >
66
67 <C-Left><C-Left><C-Left><C-Left><Right><Del>a<End>
68
69< four words back <C-Left><C-Left><C-Left><C-Left>
70 move on top of the "o" <Right>
71 delete the "o" <Del>
72 insert an "a" a
73 go to end of the line <End>
74
75This uses special keys to move around, while remaining in Insert mode. This
76resembles what you would do in a modeless editor. It's easier to remember,
77but takes more time (you have to move your hand from the letters to the cursor
78keys, and the <End> key is hard to press without looking at the keyboard).
79 These special keys are most useful when writing a mapping that doesn't
80leave Insert mode. The extra typing doesn't matter then.
81 An overview of the keys you can use in Insert mode:
82
83 <C-Home> to start of the file
84 <PageUp> a whole screenful up
85 <Home> to start of line
86 <S-Left> one word left
87 <C-Left> one word left
88 <S-Right> one word right
89 <C-Right> one word right
90 <End> to end of the line
91 <PageDown> a whole screenful down
92 <C-End> to end of the file
93
94There are a few more, see |ins-special-special|.
95
96==============================================================================
97*24.2* Showing matches
98
99When you type a ) it would be nice to see with which ( it matches. To make
100Vim do that use this command: >
101
102 :set showmatch
103
104When you now type a text like "(example)", as soon as you type the ) Vim will
105briefly move the cursor to the matching (, keep it there for half a second,
106and move back to where you were typing.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000107 In case there is no matching (, Vim will beep. Then you know that you
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108might have forgotten the ( somewhere, or typed a ) too many.
109 The match will also be shown for [] and {} pairs. You don't have to wait
110with typing the next character, as soon as Vim sees it the cursor will move
111back and inserting continues as before.
112 You can change the time Vim waits with the 'matchtime' option. For
113example, to make Vim wait one and a half second: >
114
115 :set matchtime=15
116
117The time is specified in tenths of a second.
118
119==============================================================================
120*24.3* Completion
121
122Vim can automatically complete words on insertion. You type the first part of
123a word, press CTRL-P, and Vim guesses the rest.
124 Suppose, for example, that you are creating a C program and want to type in
125the following:
126
127 total = ch_array[0] + ch_array[1] + ch_array[2]; ~
128
129You start by entering the following:
130
131 total = ch_array[0] + ch_ ~
132
133At this point, you tell Vim to complete the word using the command CTRL-P.
134Vim searches for a word that starts with what's in front of the cursor. In
135this case, it is "ch_", which matches with the word ch_array. So typing
136CTRL-P gives you the following:
137
138 total = ch_array[0] + ch_array ~
139
140After a little more typing, you get this (ending in a space):
141
142 total = ch_array[0] + ch_array[1] + ~
143
144If you now type CTRL-P Vim will search again for a word that completes the
145word before the cursor. Since there is nothing in front of the cursor, it
146finds the first word backwards, which is "ch_array". Typing CTRL-P again
147gives you the next word that matches, in this case "total". A third CTRL-P
148searches further back. If there is nothing else, it causes the editor to run
149out of words, so it returns to the original text, which is nothing. A fourth
150CTRL-P causes the editor to start over again with "ch_array".
151
152To search forward, use CTRL-N. Since the search wraps around the end of the
153file, CTRL-N and CTRL-P will find the same matches, but in a different
154sequence. Hint: CTRL-N is Next-match and CTRL-P is Previous-match.
155
156The Vim editor goes through a lot of effort to find words to complete. By
157default, it searches the following places:
158
159 1. Current file
160 2. Files in other windows
161 3. Other loaded files (hidden buffers)
162 4. Files which are not loaded (inactive buffers)
163 5. Tag files
164 6. All files #included by the current file
165
166
167OPTIONS
168
169You can customize the search order with the 'complete' option.
170
171The 'ignorecase' option is used. When it is set, case differences are ignored
172when searching for matches.
173
174A special option for completion is 'infercase'. This is useful to find
175matches while ignoring case ('ignorecase' must be set) but still using the
176case of the word typed so far. Thus if you type "For" and Vim finds a match
177"fortunately", it will result in "Fortunately".
178
179
180COMPLETING SPECIFIC ITEMS
181
182If you know what you are looking for, you can use these commands to complete
183with a certain type of item:
184
185 CTRL-X CTRL-F file names
186 CTRL-X CTRL-L whole lines
187 CTRL-X CTRL-D macro definitions (also in included files)
188 CTRL-X CTRL-I current and included files
189 CTRL-X CTRL-K words from a dictionary
190 CTRL-X CTRL-T words from a thesaurus
191 CTRL-X CTRL-] tags
192 CTRL-X CTRL-V Vim command line
193
194After each of them CTRL-N can be used to find the next match, CTRL-P to find
195the previous match.
196 More information for each of these commands here: |ins-completion|.
197
198
199COMPLETING FILE NAMES
200
201Let's take CTRL-X CTRL-F as an example. This will find file names. It scans
202the current directory for files and displays each one that matches the word in
203front of the cursor.
204 Suppose, for example, that you have the following files in the current
205directory:
206
207 main.c sub_count.c sub_done.c sub_exit.c
208
209Now enter Insert mode and start typing:
210
211 The exit code is in the file sub ~
212
213At this point, you enter the command CTRL-X CTRL-F. Vim now completes the
214current word "sub" by looking at the files in the current directory. The
215first match is sub_count.c. This is not the one you want, so you match the
216next file by typing CTRL-N. This match is sub_done.c. Typing CTRL-N again
217takes you to sub_exit.c. The results:
218
219 The exit code is in the file sub_exit.c ~
220
221If the file name starts with / (Unix) or C:\ (MS-Windows) you can find all
222files in the file system. For example, type "/u" and CTRL-X CTRL-F. This
223will match "/usr" (this is on Unix):
224
225 the file is found in /usr/ ~
226
227If you now press CTRL-N you go back to "/u". Instead, to accept the "/usr/"
228and go one directory level deeper, use CTRL-X CTRL-F again:
229
230 the file is found in /usr/X11R6/ ~
231
232The results depend on what is found in your file system, of course. The
233matches are sorted alphabetically.
234
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000235
236COMPLETING IN SOURCE CODE
237
238Source code files are well structured. That makes it possible to do
239completion in an intelligent way. In Vim this is called Omni completion. In
240some other editors it's called intellisense, but that is a trademark.
241
242The key to Omni completion is CTRL-X CTRL-O. Obviously the O stands for Omni
243here, so that you can remember it easier. Let's use an example for editing C
244source:
245
246 { ~
247 struct foo *p; ~
248 p-> ~
249
250The cursor is after "p->". Now type CTRL-X CTRL-O. Vim will offer you a list
251of alternatives, which are the items that "struct foo" contains. That is
252quite different from using CTRL-P, which would complete any word, while only
253members of "struct foo" are valid here.
254
255For Omni completion to work you may need to do some setup. For C code you
256need to create a tags file and set the 'tags' option. That is explained
257|ft-c-omni|. For other filetypes you may need to do something similar, look
258below |compl-omni-filetypes|. It only works for specific filetypes. Check
259the value of the 'omnifunc' option to find out if it would work.
260
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261==============================================================================
262*24.4* Repeating an insert
263
264If you press CTRL-A, the editor inserts the text you typed the last time you
265were in Insert mode.
266 Assume, for example, that you have a file that begins with the following:
267
268 "file.h" ~
269 /* Main program begins */ ~
270
271You edit this file by inserting "#include " at the beginning of the first
272line:
273
274 #include "file.h" ~
275 /* Main program begins */ ~
276
277You go down to the beginning of the next line using the commands "j^". You
278now start to insert a new "#include" line. So you type: >
279
280 i CTRL-A
281
282The result is as follows:
283
284 #include "file.h" ~
285 #include /* Main program begins */ ~
286
287The "#include " was inserted because CTRL-A inserts the text of the previous
288insert. Now you type "main.h"<Enter> to finish the line:
289
290
291 #include "file.h" ~
292 #include "main.h" ~
293 /* Main program begins */ ~
294
295The CTRL-@ command does a CTRL-A and then exits Insert mode. That's a quick
296way of doing exactly the same insertion again.
297
298==============================================================================
299*24.5* Copying from another line
300
301The CTRL-Y command inserts the character above the cursor. This is useful
302when you are duplicating a previous line. For example, you have this line of
303C code:
304
305 b_array[i]->s_next = a_array[i]->s_next; ~
306
307Now you need to type the same line, but with "s_prev" instead of "s_next".
308Start the new line, and press CTRL-Y 14 times, until you are at the "n" of
309"next":
310
311 b_array[i]->s_next = a_array[i]->s_next; ~
312 b_array[i]->s_ ~
313
314Now you type "prev":
315
316 b_array[i]->s_next = a_array[i]->s_next; ~
317 b_array[i]->s_prev ~
318
319Continue pressing CTRL-Y until the following "next":
320
321 b_array[i]->s_next = a_array[i]->s_next; ~
322 b_array[i]->s_prev = a_array[i]->s_ ~
323
324Now type "prev;" to finish it off.
325
326The CTRL-E command acts like CTRL-Y except it inserts the character below the
327cursor.
328
329==============================================================================
330*24.6* Inserting a register
331
332The command CTRL-R {register} inserts the contents of the register. This is
333useful to avoid having to type a long word. For example, you need to type
334this:
335
336 r = VeryLongFunction(a) + VeryLongFunction(b) + VeryLongFunction(c) ~
337
338The function name is defined in a different file. Edit that file and move the
339cursor on top of the function name there, and yank it into register v: >
340
341 "vyiw
342
343"v is the register specification, "yiw" is yank-inner-word. Now edit the file
344where the new line is to be inserted, and type the first letters:
345
346 r = ~
347
348Now use CTRL-R v to insert the function name:
349
350 r = VeryLongFunction ~
351
352You continue to type the characters in between the function name, and use
353CTRL-R v two times more.
354 You could have done the same with completion. Using a register is useful
355when there are many words that start with the same characters.
356
357If the register contains characters such as <BS> or other special characters,
358they are interpreted as if they had been typed from the keyboard. If you do
359not want this to happen (you really want the <BS> to be inserted in the text),
360use the command CTRL-R CTRL-R {register}.
361
362==============================================================================
363*24.7* Abbreviations
364
365An abbreviation is a short word that takes the place of a long one. For
366example, "ad" stands for "advertisement". Vim enables you to type an
367abbreviation and then will automatically expand it for you.
368 To tell Vim to expand "ad" into "advertisement" every time you insert it,
369use the following command: >
370
371 :iabbrev ad advertisement
372
373Now, when you type "ad", the whole word "advertisement" will be inserted into
374the text. This is triggered by typing a character that can't be part of a
375word, for example a space:
376
377 What Is Entered What You See
378 I saw the a I saw the a ~
379 I saw the ad I saw the ad ~
380 I saw the ad<Space> I saw the advertisement<Space> ~
381
382The expansion doesn't happen when typing just "ad". That allows you to type a
383word like "add", which will not get expanded. Only whole words are checked
384for abbreviations.
385
386
387ABBREVIATING SEVERAL WORDS
388
389It is possible to define an abbreviation that results in multiple words. For
390example, to define "JB" as "Jack Benny", use the following command: >
391
392 :iabbrev JB Jack Benny
393
394As a programmer, I use two rather unusual abbreviations: >
395
396 :iabbrev #b /****************************************
397 :iabbrev #e <Space>****************************************/
398
399These are used for creating boxed comments. The comment starts with #b, which
400draws the top line. I then type the comment text and use #e to draw the
401bottom line.
402 Notice that the #e abbreviation begins with a space. In other words, the
403first two characters are space-star. Usually Vim ignores spaces between the
404abbreviation and the expansion. To avoid that problem, I spell space as seven
405characters: <, S, p, a, c, e, >.
406
407 Note:
408 ":iabbrev" is a long word to type. ":iab" works just as well.
409 That's abbreviating the abbreviate command!
410
411
412FIXING TYPING MISTAKES
413
414It's very common to make the same typing mistake every time. For example,
415typing "teh" instead of "the". You can fix this with an abbreviation: >
416
417 :abbreviate teh the
418
419You can add a whole list of these. Add one each time you discover a common
420mistake.
421
422
423LISTING ABBREVIATIONS
424
425The ":abbreviate" command lists the abbreviations:
426
427 :abbreviate
428 i #e ****************************************/
429 i #b /****************************************
430 i JB Jack Benny
431 i ad advertisement
432 ! teh the
433
434The "i" in the first column indicates Insert mode. These abbreviations are
435only active in Insert mode. Other possible characters are:
436
437 c Command-line mode :cabbrev
438 ! both Insert and Command-line mode :abbreviate
439
440Since abbreviations are not often useful in Command-line mode, you will mostly
441use the ":iabbrev" command. That avoids, for example, that "ad" gets expanded
442when typing a command like: >
443
444 :edit ad
445
446
447DELETING ABBREVIATIONS
448
449To get rid of an abbreviation, use the ":unabbreviate" command. Suppose you
450have the following abbreviation: >
451
452 :abbreviate @f fresh
453
454You can remove it with this command: >
455
456 :unabbreviate @f
457
458While you type this, you will notice that @f is expanded to "fresh". Don't
459worry about this, Vim understands it anyway (except when you have an
460abbreviation for "fresh", but that's very unlikely).
461 To remove all the abbreviations: >
462
463 :abclear
464
465":unabbreviate" and ":abclear" also come in the variants for Insert mode
466(":iunabbreviate and ":iabclear") and Command-line mode (":cunabbreviate" and
467":cabclear").
468
469
470REMAPPING ABBREVIATIONS
471
472There is one thing to watch out for when defining an abbreviation: The
473resulting string should not be mapped. For example: >
474
475 :abbreviate @a adder
476 :imap dd disk-door
477
478When you now type @a, you will get "adisk-doorer". That's not what you want.
479To avoid this, use the ":noreabbrev" command. It does the same as
480":abbreviate", but avoids that the resulting string is used for mappings: >
481
482 :noreabbrev @a adder
483
484Fortunately, it's unlikely that the result of an abbreviation is mapped.
485
486==============================================================================
487*24.8* Entering special characters
488
489The CTRL-V command is used to insert the next character literally. In other
490words, any special meaning the character has, it will be ignored. For
491example: >
492
493 CTRL-V <Esc>
494
495Inserts an escape character. Thus you don't leave Insert mode. (Don't type
496the space after CTRL-V, it's only to make this easier to read).
497
498 Note:
499 On MS-Windows CTRL-V is used to paste text. Use CTRL-Q instead of
500 CTRL-V. On Unix, on the other hand, CTRL-Q does not work on some
501 terminals, because it has a special meaning.
502
503You can also use the command CTRL-V {digits} to insert a character with the
504decimal number {digits}. For example, the character number 127 is the <Del>
505character (but not necessarily the <Del> key!). To insert <Del> type: >
506
507 CTRL-V 127
508
509You can enter characters up to 255 this way. When you type fewer than two
510digits, a non-digit will terminate the command. To avoid the need of typing a
511non-digit, prepend one or two zeros to make three digits.
512 All the next commands insert a <Tab> and then a dot:
513
514 CTRL-V 9.
515 CTRL-V 09.
516 CTRL-V 009.
517
518To enter a character in hexadecimal, use an "x" after the CTRL-V: >
519
520 CTRL-V x7f
521
522This also goes up to character 255 (CTRL-V xff). You can use "o" to type a
523character as an octal number and two more methods allow you to type up to
524a 16 bit and a 32 bit number (e.g., for a Unicode character): >
525
526 CTRL-V o123
527 CTRL-V u1234
528 CTRL-V U12345678
529
530==============================================================================
531*24.9* Digraphs
532
533Some characters are not on the keyboard. For example, the copyright character
534(©). To type these characters in Vim, you use digraphs, where two characters
535represent one. To enter a ©, for example, you press three keys: >
536
537 CTRL-K Co
538
539To find out what digraphs are available, use the following command: >
540
541 :digraphs
542
543Vim will display the digraph table. Here are three lines of it:
544
545 AC ~_ 159 NS | 160 !I ¡ 161 Ct ¢ 162 Pd £ 163 Cu ¤ 164 Ye ¥ 165 ~
546 BB ¦ 166 SE § 167 ': ¨ 168 Co © 169 -a ª 170 << « 171 NO ¬ 172 ~
547 -- ­ 173 Rg ® 174 'm ¯ 175 DG ° 176 +- ± 177 2S ² 178 3S ³ 179 ~
548
549This shows, for example, that the digraph you get by typing CTRL-K Pd is the
550character (£). This is character number 163 (decimal).
551 Pd is short for Pound. Most digraphs are selected to give you a hint about
552the character they will produce. If you look through the list you will
553understand the logic.
554 You can exchange the first and second character, if there is no digraph for
555that combination. Thus CTRL-K dP also works. Since there is no digraph for
556"dP" Vim will also search for a "Pd" digraph.
557
558 Note:
559 The digraphs depend on the character set that Vim assumes you are
560 using. On MS-DOS they are different from MS-Windows. Always use
561 ":digraphs" to find out which digraphs are currently available.
562
563You can define your own digraphs. Example: >
564
565 :digraph a" ä
566
567This defines that CTRL-K a" inserts an ä character. You can also specify the
568character with a decimal number. This defines the same digraph: >
569
570 :digraph a" 228
571
572More information about digraphs here: |digraphs|
573 Another way to insert special characters is with a keymap. More about that
574here: |45.5|
575
576==============================================================================
577*24.10* Normal mode commands
578
579Insert mode offers a limited number of commands. In Normal mode you have many
580more. When you want to use one, you usually leave Insert mode with <Esc>,
581execute the Normal mode command, and re-enter Insert mode with "i" or "a".
582 There is a quicker way. With CTRL-O {command} you can execute any Normal
583mode command from Insert mode. For example, to delete from the cursor to the
584end of the line: >
585
586 CTRL-O D
587
588You can execute only one Normal mode command this way. But you can specify a
589register or a count. A more complicated example: >
590
591 CTRL-O "g3dw
592
593This deletes up to the third word into register g.
594
595==============================================================================
596
597Next chapter: |usr_25.txt| Editing formatted text
598
599Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl: