codekingpro/portable-devtools
114k
1*usr_20.txt* For Vim version 9.2. Last change: 2026 Feb 142 3 4 VIM USER MANUAL by Bram Moolenaar5 6 7 Typing command-line commands quickly8 9 10Vim has a few generic features that makes it easier to enter commands. Colon11commands can be abbreviated, edited and repeated. Completion is available for12nearly everything.13 14|20.1| Command line editing15|20.2| Command line abbreviations16|20.3| Command line completion17|20.4| Command line history18|20.5| Command line window19 20 Next chapter: |usr_21.txt| Go away and come back21 Previous chapter: |usr_12.txt| Clever tricks22Table of contents: |usr_toc.txt|23 24==============================================================================25*20.1* Command line editing26 27When you use a colon (:) command or search for a string with / or ?, Vim puts28the cursor on the bottom of the screen. There you type the command or search29pattern. This is called the Command line. Also when it's used for entering a30search command.31 32The most obvious way to edit the command you type is by pressing the <BS> key.33This erases the character before the cursor. To erase another character,34typed earlier, first move the cursor with the cursor keys.35 For example, you have typed this: >36 37 :s/col/pig/38 39Before you hit <Enter>, you notice that "col" should be "cow". To correct40this, you type <Left> five times. The cursor is now just after "col". Type41<BS> and "w" to correct: >42 43 :s/cow/pig/44 45Now you can press <Enter> directly. You don't have to move the cursor to the46end of the line before executing the command.47 48The most often used keys to move around in the command line:49 50 <Left> one character left51 <Right> one character right52 <S-Left> or <C-Left> one word left53 <S-Right> or <C-Right> one word right54 CTRL-B or <Home> to begin of command line55 CTRL-E or <End> to end of command line56 57 Note:58 <S-Left> (cursor left key with Shift key pressed) and <C-Left> (cursor59 left key with Control pressed) will not work on all keyboards. Same60 for the other Shift and Control combinations.61 62You can also use the mouse to move the cursor.63 64 65DELETING66 67As mentioned, <BS> deletes the character before the cursor. To delete a whole68word use CTRL-W.69 70 /the fine pig ~71 72 CTRL-W73 74 /the fine ~75 76CTRL-U removes all text, thus allows you to start all over again.77 78 79OVERSTRIKE80 81The <Insert> key toggles between inserting characters and replacing the82existing ones. Start with this text:83 84 /the fine pig ~85 86Move the cursor to the start of "fine" with <S-Left> twice (or <Left> eight87times, if <S-Left> doesn't work). Now press <Insert> to switch to overstrike88and type "great":89 90 /the greatpig ~91 92Oops, we lost the space. Now, don't use <BS>, because it would delete the93"t" (this is different from Replace mode). Instead, press <Insert> to switch94from overstrike to inserting, and type the space:95 96 /the great pig ~97 98 99CANCELLING100 101You thought of executing a : or / command, but changed your mind. To get rid102of what you already typed, without executing it, press CTRL-C or <Esc>.103 104 Note:105 <Esc> is the universal "get out" key. Unfortunately, in the good old106 Vi pressing <Esc> in a command line executed the command! Since that107 might be considered to be a bug, Vim uses <Esc> to cancel the command.108 But with the 'cpoptions' option it can be made Vi compatible. And109 when using a mapping (which might be written for Vi) <Esc> also works110 Vi compatible. Therefore, using CTRL-C is a method that always works.111 112If you are at the start of the command line, pressing <BS> will cancel the113command. It's like deleting the ":" or "/" that the line starts with.114 115==============================================================================116*20.2* Command line abbreviations117 118Some of the ":" commands are really long. We already mentioned that119":substitute" can be abbreviated to ":s". This is a generic mechanism, all120":" commands can be abbreviated.121 122How short can a command get? There are 26 letters, and many more commands.123For example, ":set" also starts with ":s", but ":s" doesn't start a ":set"124command. Instead ":set" can be abbreviated to ":se".125 When the shorter form of a command could be used for two commands, it126stands for only one of them. There is no logic behind which one, you have to127learn them. In the help files the shortest form that works is mentioned. For128example: >129 130 :s[ubstitute]131 132This means that the shortest form of ":substitute" is ":s". The following133characters are optional. Thus ":su" and ":sub" also work.134 135In the user manual we will either use the full name of command, or a short136version that is still readable. For example, ":function" can be abbreviated137to ":fu". But since most people don't understand what that stands for, we138will use ":fun". (Vim doesn't have a ":funny" command, otherwise ":fun" would139be confusing too.)140 141It is recommended that in Vim scripts you write the full command name. That142makes it easier to read back when you make later changes. Except for some143often used commands like ":w" (":write") and ":r" (":read").144 A particularly confusing one is ":end", which could stand for ":endif",145":endwhile" or ":endfunction". Therefore, always use the full name.146 147 148SHORT OPTION NAMES149 150In the user manual the long version of the option names is used. Many options151also have a short name. Unlike ":" commands, there is only one short name152that works. For example, the short name of 'autoindent' is 'ai'. Thus these153two commands do the same thing: >154 155 :set autoindent156 :set ai157 158You can find the full list of long and short names here: |option-list|.159 160==============================================================================161*20.3* Command line completion162 163This is one of those Vim features that, by itself, is a reason to switch from164Vi to Vim. Once you have used this, you can't do without.165 166Suppose you have a directory that contains these files:167 168 info.txt169 intro.txt170 bodyofthepaper.txt171 172To edit the last one, you use the command: >173 174 :edit bodyofthepaper.txt175 176It's easy to type this wrong. A much quicker way is: >177 178 :edit b<Tab>179 180Which will result in the same command. What happened? The <Tab> key does181completion of the word before the cursor. In this case "b". Vim looks in the182directory and finds only one file that starts with a "b". That must be the183one you are looking for, thus Vim completes the file name for you.184 185Now type: >186 187 :edit i<Tab>188 189Vim will beep, and give you: >190 191 :edit info.txt192 193The beep means that Vim has found more than one match. It then uses the first194match it found (alphabetically). If you press <Tab> again, you get: >195 196 :edit intro.txt197 198Thus, if the first <Tab> doesn't give you the file you were looking for, press199it again. If there are more matches, you will see them all, one at a time.200 If you press <Tab> on the last matching entry, you will go back to what you201first typed: >202 203 :edit i204 205Then it starts all over again. Thus Vim cycles through the list of matches.206Use CTRL-P to go through the list in the other direction:207 208 <------------------- <Tab> -------------------------+209 |210 <Tab> --> <Tab> -->211 :edit i :edit info.txt :edit intro.txt212 <-- CTRL-P <-- CTRL-P213 |214 +---------------------- CTRL-P ------------------------>215 216 217CONTEXT218 219When you type ":set i" instead of ":edit i" and press <Tab> you get: >220 221 :set icon222 223Hey, why didn't you get ":set info.txt"? That's because Vim has context224sensitive completion. The kind of words Vim will look for depends on the225command before it. Vim knows that you cannot use a file name just after a226":set" command, but you can use an option name.227 Again, if you repeat typing the <Tab>, Vim will cycle through all matches.228There are quite a few, it's better to type more characters first: >229 230 :set isk<Tab>231 232Gives: >233 234 :set iskeyword235 236Now type "=" and press <Tab>: >237 238 :set iskeyword=@,48-57,_,192-255239 240What happens here is that Vim inserts the old value of the option. Now you241can edit it.242 What is completed with <Tab> is what Vim expects in that place. Just try243it out to see how it works. In some situations you will not get what you244want. That's either because Vim doesn't know what you want, or because245completion was not implemented for that situation. In that case you will get246a <Tab> inserted (displayed as ^I).247 248 249LIST MATCHES250 251When there are many matches, you would like to see an overview. Do this by252pressing CTRL-D. For example, pressing CTRL-D after: >253 254 :set is255 256results in: >257 258 :set is259 incsearch isfname isident iskeyword isprint260 :set is261 262Vim lists the matches and then comes back with the text you typed. You can263now check the list for the item you wanted. If it isn't there, you can use264<BS> to correct the word. If there are many matches, type a few more265characters before pressing <Tab> to complete the rest.266 If you have watched carefully, you will have noticed that "incsearch"267doesn't start with "is". In this case "is" stands for the short name of268"incsearch". (Many options have a short and a long name.) Vim is clever269enough to know that you might have wanted to expand the short name of the270option into the long name.271 272 273THERE IS MORE274 275The CTRL-L command completes the word to the longest unambiguous string. If276you type ":edit i" and there are files "info.txt" and "info_backup.txt" you277will get ":edit info".278 279The 'wildmode' option can be used to change the way completion works.280The 'wildmenu' option can be used to get a menu-like list of matches.281Use the 'suffixes' option to specify files that are less important and appear282at the end of the list of files.283The 'wildignore' option specifies files that are not listed at all.284 285More about all of this here: |cmdline-completion|286 287==============================================================================288*20.4* Command line history289 290In chapter 3 we briefly mentioned the history. The basics are that you can291use the <Up> key to recall an older command line. <Down> then takes you back292to newer commands.293 294There are actually five histories. The ones we will mention here are for ":"295commands and for "/" and "?" search commands. The "/" and "?" commands share296the same history, because they are both search commands. The three other297histories are for expressions, debug mode commands and input lines for the298input() function. |cmdline-history|299 300Suppose you have done a ":set" command, typed ten more colon commands and then301want to repeat that ":set" command again. You could press ":" and then ten302times <Up>. There is a quicker way: >303 304 :se<Up>305 306Vim will now go back to the previous command that started with "se". You have307a good chance that this is the ":set" command you were looking for. At least308you should not have to press <Up> very often (unless ":set" commands is all309you have done).310 311The <Up> key will use the text typed so far and compare it with the lines in312the history. Only matching lines will be used.313 If you do not find the line you were looking for, use <Down> to go back to314what you typed and correct that. Or use CTRL-U to start all over again.315 316To see all the lines in the history: >317 318 :history319 320That's the history of ":" commands. The search history is displayed with this321command: >322 323 :history /324 325CTRL-P will work like <Up>, except that it doesn't matter what you already326typed. Similarly for CTRL-N and <Down>. CTRL-P stands for previous, CTRL-N327for next.328 329==============================================================================330*20.5* Command line window331 332Typing the text in the command line works differently from typing text in333Insert mode. It doesn't allow many commands to change the text. For most334commands that's OK, but sometimes you have to type a complicated command.335That's where the command line window is useful.336 337Open the command line window with this command: >338 339 q:340 341Vim now opens a (small) window at the bottom. It contains the command line342history, and an empty line at the end:343 344 +-------------------------------------+345 |other window |346 |~ |347 |file.txt=============================|348 |:e c |349 |:e config.h.in |350 |:set path=.,/usr/include,, |351 |:set iskeyword=@,48-57,_,192-255 |352 |:set is |353 |:q |354 |: |355 |command-line=========================|356 | |357 +-------------------------------------+358 359You are now in Normal mode. You can use the "hjkl" keys to move around. For360example, move up with "5k" to the ":e config.h.in" line. Type "$h" to go to361the "i" of "in" and type "cwout". Now you have changed the line to:362 363 :e config.h.out ~364 365Now press <Enter> and this command will be executed. The command line window366will close.367 The <Enter> command will execute the line under the cursor. It doesn't368matter whether Vim is in Insert mode or in Normal mode.369 Changes in the command line window are lost. They do not result in the370history to be changed. Except that the command you execute will be added to371the end of the history, like with all executed commands.372 373The command line window is very useful when you want to have overview of the374history, lookup a similar command, change it a bit and execute it. A search375command can be used to find something.376 In the previous example the "?config" search command could have been used377to find the previous command that contains "config". It's a bit strange,378because you are using a command line to search in the command line window.379While typing that search command you can't open another command line window,380there can be only one.381 382==============================================================================383 384Next chapter: |usr_21.txt| Go away and come back385 386Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl:387 