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