usr_24.txt 20 KB

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