usr_20.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. *usr_20.txt* For Vim version 9.0. Last change: 2021 Nov 18
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Typing command-line commands quickly
  4. Vim has a few generic features that makes it easier to enter commands. Colon
  5. commands can be abbreviated, edited and repeated. Completion is available for
  6. nearly everything.
  7. |20.1| Command line editing
  8. |20.2| Command line abbreviations
  9. |20.3| Command line completion
  10. |20.4| Command line history
  11. |20.5| Command line window
  12. Next chapter: |usr_21.txt| Go away and come back
  13. Previous chapter: |usr_12.txt| Clever tricks
  14. Table of contents: |usr_toc.txt|
  15. ==============================================================================
  16. *20.1* Command line editing
  17. When you use a colon (:) command or search for a string with / or ?, Vim puts
  18. the cursor on the bottom of the screen. There you type the command or search
  19. pattern. This is called the Command line. Also when it's used for entering a
  20. search command.
  21. The most obvious way to edit the command you type is by pressing the <BS> key.
  22. This erases the character before the cursor. To erase another character,
  23. typed earlier, first move the cursor with the cursor keys.
  24. For example, you have typed this: >
  25. :s/col/pig/
  26. Before you hit <Enter>, you notice that "col" should be "cow". To correct
  27. this, you type <Left> five times. The cursor is now just after "col". Type
  28. <BS> and "w" to correct: >
  29. :s/cow/pig/
  30. Now you can press <Enter> directly. You don't have to move the cursor to the
  31. end of the line before executing the command.
  32. The most often used keys to move around in the command line:
  33. <Left> one character left
  34. <Right> one character right
  35. <S-Left> or <C-Left> one word left
  36. <S-Right> or <C-Right> one word right
  37. CTRL-B or <Home> to begin of command line
  38. CTRL-E or <End> to end of command line
  39. Note:
  40. <S-Left> (cursor left key with Shift key pressed) and <C-Left> (cursor
  41. left key with Control pressed) will not work on all keyboards. Same
  42. for the other Shift and Control combinations.
  43. You can also use the mouse to move the cursor.
  44. DELETING
  45. As mentioned, <BS> deletes the character before the cursor. To delete a whole
  46. word use CTRL-W.
  47. /the fine pig ~
  48. CTRL-W
  49. /the fine ~
  50. CTRL-U removes all text, thus allows you to start all over again.
  51. OVERSTRIKE
  52. The <Insert> key toggles between inserting characters and replacing the
  53. existing ones. Start with this text:
  54. /the fine pig ~
  55. Move the cursor to the start of "fine" with <S-Left> twice (or <Left> eight
  56. times, if <S-Left> doesn't work). Now press <Insert> to switch to overstrike
  57. and type "great":
  58. /the greatpig ~
  59. Oops, we lost the space. Now, don't use <BS>, because it would delete the
  60. "t" (this is different from Replace mode). Instead, press <Insert> to switch
  61. from overstrike to inserting, and type the space:
  62. /the great pig ~
  63. CANCELLING
  64. You thought of executing a : or / command, but changed your mind. To get rid
  65. of what you already typed, without executing it, press CTRL-C or <Esc>.
  66. Note:
  67. <Esc> is the universal "get out" key. Unfortunately, in the good old
  68. Vi pressing <Esc> in a command line executed the command! Since that
  69. might be considered to be a bug, Vim uses <Esc> to cancel the command.
  70. But with the 'cpoptions' option it can be made Vi compatible. And
  71. when using a mapping (which might be written for Vi) <Esc> also works
  72. Vi compatible. Therefore, using CTRL-C is a method that always works.
  73. If you are at the start of the command line, pressing <BS> will cancel the
  74. command. It's like deleting the ":" or "/" that the line starts with.
  75. ==============================================================================
  76. *20.2* Command line abbreviations
  77. Some of the ":" commands are really long. We already mentioned that
  78. ":substitute" can be abbreviated to ":s". This is a generic mechanism, all
  79. ":" commands can be abbreviated.
  80. How short can a command get? There are 26 letters, and many more commands.
  81. For example, ":set" also starts with ":s", but ":s" doesn't start a ":set"
  82. command. Instead ":set" can be abbreviated to ":se".
  83. When the shorter form of a command could be used for two commands, it
  84. stands for only one of them. There is no logic behind which one, you have to
  85. learn them. In the help files the shortest form that works is mentioned. For
  86. example: >
  87. :s[ubstitute]
  88. This means that the shortest form of ":substitute" is ":s". The following
  89. characters are optional. Thus ":su" and ":sub" also work.
  90. In the user manual we will either use the full name of command, or a short
  91. version that is still readable. For example, ":function" can be abbreviated
  92. to ":fu". But since most people don't understand what that stands for, we
  93. will use ":fun". (Vim doesn't have a ":funny" command, otherwise ":fun" would
  94. be confusing too.)
  95. It is recommended that in Vim scripts you write the full command name. That
  96. makes it easier to read back when you make later changes. Except for some
  97. often used commands like ":w" (":write") and ":r" (":read").
  98. A particularly confusing one is ":end", which could stand for ":endif",
  99. ":endwhile" or ":endfunction". Therefore, always use the full name.
  100. SHORT OPTION NAMES
  101. In the user manual the long version of the option names is used. Many options
  102. also have a short name. Unlike ":" commands, there is only one short name
  103. that works. For example, the short name of 'autoindent' is 'ai'. Thus these
  104. two commands do the same thing: >
  105. :set autoindent
  106. :set ai
  107. You can find the full list of long and short names here: |option-list|.
  108. ==============================================================================
  109. *20.3* Command line completion
  110. This is one of those Vim features that, by itself, is a reason to switch from
  111. Vi to Vim. Once you have used this, you can't do without.
  112. Suppose you have a directory that contains these files:
  113. info.txt
  114. intro.txt
  115. bodyofthepaper.txt
  116. To edit the last one, you use the command: >
  117. :edit bodyofthepaper.txt
  118. It's easy to type this wrong. A much quicker way is: >
  119. :edit b<Tab>
  120. Which will result in the same command. What happened? The <Tab> key does
  121. completion of the word before the cursor. In this case "b". Vim looks in the
  122. directory and finds only one file that starts with a "b". That must be the
  123. one you are looking for, thus Vim completes the file name for you.
  124. Now type: >
  125. :edit i<Tab>
  126. Vim will beep, and give you: >
  127. :edit info.txt
  128. The beep means that Vim has found more than one match. It then uses the first
  129. match it found (alphabetically). If you press <Tab> again, you get: >
  130. :edit intro.txt
  131. Thus, if the first <Tab> doesn't give you the file you were looking for, press
  132. it again. If there are more matches, you will see them all, one at a time.
  133. If you press <Tab> on the last matching entry, you will go back to what you
  134. first typed: >
  135. :edit i
  136. Then it starts all over again. Thus Vim cycles through the list of matches.
  137. Use CTRL-P to go through the list in the other direction:
  138. <------------------- <Tab> -------------------------+
  139. |
  140. <Tab> --> <Tab> -->
  141. :edit i :edit info.txt :edit intro.txt
  142. <-- CTRL-P <-- CTRL-P
  143. |
  144. +---------------------- CTRL-P ------------------------>
  145. CONTEXT
  146. When you type ":set i" instead of ":edit i" and press <Tab> you get: >
  147. :set icon
  148. Hey, why didn't you get ":set info.txt"? That's because Vim has context
  149. sensitive completion. The kind of words Vim will look for depends on the
  150. command before it. Vim knows that you cannot use a file name just after a
  151. ":set" command, but you can use an option name.
  152. Again, if you repeat typing the <Tab>, Vim will cycle through all matches.
  153. There are quite a few, it's better to type more characters first: >
  154. :set isk<Tab>
  155. Gives: >
  156. :set iskeyword
  157. Now type "=" and press <Tab>: >
  158. :set iskeyword=@,48-57,_,192-255
  159. What happens here is that Vim inserts the old value of the option. Now you
  160. can edit it.
  161. What is completed with <Tab> is what Vim expects in that place. Just try
  162. it out to see how it works. In some situations you will not get what you
  163. want. That's either because Vim doesn't know what you want, or because
  164. completion was not implemented for that situation. In that case you will get
  165. a <Tab> inserted (displayed as ^I).
  166. LIST MATCHES
  167. When there are many matches, you would like to see an overview. Do this by
  168. pressing CTRL-D. For example, pressing CTRL-D after: >
  169. :set is
  170. results in: >
  171. :set is
  172. incsearch isfname isident iskeyword isprint
  173. :set is
  174. Vim lists the matches and then comes back with the text you typed. You can
  175. now check the list for the item you wanted. If it isn't there, you can use
  176. <BS> to correct the word. If there are many matches, type a few more
  177. characters before pressing <Tab> to complete the rest.
  178. If you have watched carefully, you will have noticed that "incsearch"
  179. doesn't start with "is". In this case "is" stands for the short name of
  180. "incsearch". (Many options have a short and a long name.) Vim is clever
  181. enough to know that you might have wanted to expand the short name of the
  182. option into the long name.
  183. THERE IS MORE
  184. The CTRL-L command completes the word to the longest unambiguous string. If
  185. you type ":edit i" and there are files "info.txt" and "info_backup.txt" you
  186. will get ":edit info".
  187. The 'wildmode' option can be used to change the way completion works.
  188. The 'wildmenu' option can be used to get a menu-like list of matches.
  189. Use the 'suffixes' option to specify files that are less important and appear
  190. at the end of the list of files.
  191. The 'wildignore' option specifies files that are not listed at all.
  192. More about all of this here: |cmdline-completion|
  193. ==============================================================================
  194. *20.4* Command line history
  195. In chapter 3 we briefly mentioned the history. The basics are that you can
  196. use the <Up> key to recall an older command line. <Down> then takes you back
  197. to newer commands.
  198. There are actually five histories. The ones we will mention here are for ":"
  199. commands and for "/" and "?" search commands. The "/" and "?" commands share
  200. the same history, because they are both search commands. The three other
  201. histories are for expressions, debug mode commands and input lines for the
  202. input() function. |cmdline-history|
  203. Suppose you have done a ":set" command, typed ten more colon commands and then
  204. want to repeat that ":set" command again. You could press ":" and then ten
  205. times <Up>. There is a quicker way: >
  206. :se<Up>
  207. Vim will now go back to the previous command that started with "se". You have
  208. a good chance that this is the ":set" command you were looking for. At least
  209. you should not have to press <Up> very often (unless ":set" commands is all
  210. you have done).
  211. The <Up> key will use the text typed so far and compare it with the lines in
  212. the history. Only matching lines will be used.
  213. If you do not find the line you were looking for, use <Down> to go back to
  214. what you typed and correct that. Or use CTRL-U to start all over again.
  215. To see all the lines in the history: >
  216. :history
  217. That's the history of ":" commands. The search history is displayed with this
  218. command: >
  219. :history /
  220. CTRL-P will work like <Up>, except that it doesn't matter what you already
  221. typed. Similarly for CTRL-N and <Down>. CTRL-P stands for previous, CTRL-N
  222. for next.
  223. ==============================================================================
  224. *20.5* Command line window
  225. Typing the text in the command line works differently from typing text in
  226. Insert mode. It doesn't allow many commands to change the text. For most
  227. commands that's OK, but sometimes you have to type a complicated command.
  228. That's where the command line window is useful.
  229. Open the command line window with this command: >
  230. q:
  231. Vim now opens a (small) window at the bottom. It contains the command line
  232. history, and an empty line at the end:
  233. +-------------------------------------+
  234. |other window |
  235. |~ |
  236. |file.txt=============================|
  237. |:e c |
  238. |:e config.h.in |
  239. |:set path=.,/usr/include,, |
  240. |:set iskeyword=@,48-57,_,192-255 |
  241. |:set is |
  242. |:q |
  243. |: |
  244. |command-line=========================|
  245. | |
  246. +-------------------------------------+
  247. You are now in Normal mode. You can use the "hjkl" keys to move around. For
  248. example, move up with "5k" to the ":e config.h.in" line. Type "$h" to go to
  249. the "i" of "in" and type "cwout". Now you have changed the line to:
  250. :e config.h.out ~
  251. Now press <Enter> and this command will be executed. The command line window
  252. will close.
  253. The <Enter> command will execute the line under the cursor. It doesn't
  254. matter whether Vim is in Insert mode or in Normal mode.
  255. Changes in the command line window are lost. They do not result in the
  256. history to be changed. Except that the command you execute will be added to
  257. the end of the history, like with all executed commands.
  258. The command line window is very useful when you want to have overview of the
  259. history, lookup a similar command, change it a bit and execute it. A search
  260. command can be used to find something.
  261. In the previous example the "?config" search command could have been used
  262. to find the previous command that contains "config". It's a bit strange,
  263. because you are using a command line to search in the command line window.
  264. While typing that search command you can't open another command line window,
  265. there can be only one.
  266. ==============================================================================
  267. Next chapter: |usr_21.txt| Go away and come back
  268. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: