1 *cmdline.txt* For Vim version 7.2b. Last change: 2008 Jul 21
4 VIM REFERENCE MANUAL by Bram Moolenaar
7 *Cmdline-mode* *Command-line-mode*
8 Command-line mode *Cmdline* *Command-line* *mode-cmdline* *:*
10 Command-line mode is used to enter Ex commands (":"), search patterns
11 ("/" and "?"), and filter commands ("!").
13 Basic command line editing is explained in chapter 20 of the user manual
16 1. Command-line editing |cmdline-editing|
17 2. Command-line completion |cmdline-completion|
18 3. Ex command-lines |cmdline-lines|
19 4. Ex command-line ranges |cmdline-ranges|
20 5. Ex command-line flags |ex-flags|
21 6. Ex special characters |cmdline-special|
22 7. Command-line window |cmdline-window|
24 ==============================================================================
25 1. Command-line editing *cmdline-editing*
27 Normally characters are inserted in front of the cursor position. You can
28 move around in the command-line with the left and right cursor keys. With the
29 <Insert> key, you can toggle between inserting and overstriking characters.
30 {Vi: can only alter the last character in the line}
32 Note that if your keyboard does not have working cursor keys or any of the
33 other special keys, you can use ":cnoremap" to define another key for them.
34 For example, to define tcsh style editing keys: *tcsh-style* >
35 :cnoremap <C-A> <Home>
36 :cnoremap <C-F> <Right>
37 :cnoremap <C-B> <Left>
38 :cnoremap <Esc>b <S-Left>
39 :cnoremap <Esc>f <S-Right>
40 (<> notation |<>|; type all this literally)
43 When the command line is getting longer than what fits on the screen, only the
44 part that fits will be shown. The cursor can only move in this visible part,
45 thus you cannot edit beyond that.
47 *cmdline-history* *history*
48 The command-lines that you enter are remembered in a history table. You can
49 recall them with the up and down cursor keys. There are actually five
51 - one for ':' commands
52 - one for search strings
54 - one for input lines, typed for the |input()| function.
55 - one for debug mode commands
56 These are completely separate. Each history can only be accessed when
57 entering the same type of line.
58 Use the 'history' option to set the number of lines that are remembered
61 - When you enter a command-line that is exactly the same as an older one, the
62 old one is removed (to avoid repeated commands moving older commands out of
64 - Only commands that are typed are remembered. Ones that completely come from
65 mappings are not put in the history.
66 - All searches are put in the search history, including the ones that come
67 from commands like "*" and "#". But for a mapping, only the last search is
68 remembered (to avoid that long mappings trash the history).
70 {not available when compiled without the |+cmdline_hist| feature}
72 There is an automatic completion of names on the command-line; see
76 CTRL-V Insert next non-digit literally. Up to three digits form the
77 decimal value of a single byte. The non-digit and the three
78 digits are not considered for mapping. This works the same
79 way as in Insert mode (see above, |i_CTRL-V|).
80 Note: Under Windows CTRL-V is often mapped to paste text.
81 Use CTRL-Q instead then.
83 CTRL-Q Same as CTRL-V. But with some terminals it is used for
84 control flow, it doesn't work then.
91 <S-Left> or <C-Left> *c_<C-Left>*
94 <S-Right> or <C-Right> *c_<C-Right>*
96 CTRL-B or <Home> *c_CTRL-B* *c_<Home>*
97 cursor to beginning of command-line
98 CTRL-E or <End> *c_CTRL-E* *c_<End>*
99 cursor to end of command-line
102 <LeftMouse> Move the cursor to the position of the mouse click.
104 CTRL-H *c_<BS>* *c_CTRL-H*
105 <BS> Delete the character in front of the cursor (see |:fixdel| if
106 your <BS> key does not do what you want).
108 <Del> Delete the character under the cursor (at end of line:
109 character before the cursor) (see |:fixdel| if your <Del>
110 key does not do what you want).
112 CTRL-W Delete the |WORD| before the cursor.
114 CTRL-U Remove all characters between the cursor position and
115 the beginning of the line. Previous versions of vim
116 deleted all characters on the line. If that is the
117 preferred behavior, add the following to your .vimrc: >
118 :cnoremap <C-U> <C-E><C-U>
120 Note: if the command-line becomes empty with one of the
121 delete commands, Command-line mode is quit.
123 <Insert> Toggle between insert and overstrike. {not in Vi}
125 {char1} <BS> {char2} or *c_digraph*
126 CTRL-K {char1} {char2} *c_CTRL-K*
127 enter digraph (see |digraphs|). When {char1} is a special
128 key, the code for that key is inserted in <> form. {not in Vi}
130 CTRL-R {0-9a-z"%#:-=.} *c_CTRL-R* *c_<C-R>*
131 Insert the contents of a numbered or named register. Between
132 typing CTRL-R and the second character '"' will be displayed
133 to indicate that you are expected to enter the name of a
135 The text is inserted as if you typed it, but mappings and
136 abbreviations are not used. Command-line completion through
137 'wildchar' is not triggered though. And characters that end
138 the command line are inserted literally (<Esc>, <CR>, <NL>,
139 <C-C>). A <BS> or CTRL-W could still end the command line
140 though, and remaining characters will then be interpreted in
141 another mode, which might not be what you intended.
143 '"' the unnamed register, containing the text of
144 the last delete or yank
145 '%' the current file name
146 '#' the alternate file name
147 '*' the clipboard contents (X11: primary selection)
148 '+' the clipboard contents
149 '/' the last search pattern
150 ':' the last command-line
151 '-' the last small (less than a line) delete
152 '.' the last inserted text
154 '=' the expression register: you are prompted to
155 enter an expression (see |expression|)
156 (doesn't work at the expression prompt; some
157 things such as changing the buffer or current
158 window are not allowed to avoid side effects)
159 See |registers| about registers. {not in Vi}
160 Implementation detail: When using the |expression| register
161 and invoking setcmdpos(), this sets the position before
162 inserting the resulting string. Use CTRL-R CTRL-R to set the
165 CTRL-R CTRL-F *c_CTRL-R_CTRL-F* *c_<C-R>_<C-F>*
166 CTRL-R CTRL-P *c_CTRL-R_CTRL-P* *c_<C-R>_<C-P>*
167 CTRL-R CTRL-W *c_CTRL-R_CTRL-W* *c_<C-R>_<C-W>*
168 CTRL-R CTRL-A *c_CTRL-R_CTRL-A* *c_<C-R>_<C-A>*
169 Insert the object under the cursor:
170 CTRL-F the Filename under the cursor
171 CTRL-P the Filename under the cursor, expanded with
173 CTRL-W the Word under the cursor
174 CTRL-A the WORD under the cursor; see |WORD|
176 When 'incsearch' is set the cursor position at the end of the
177 currently displayed match is used. With CTRL-W the part of
178 the word that was already typed is not inserted again.
181 CTRL-F and CTRL-P: {only when +file_in_path feature is
184 *c_CTRL-R_CTRL-R* *c_<C-R>_<C-R>*
185 *c_CTRL-R_CTRL-O* *c_<C-R>_<C-O>*
186 CTRL-R CTRL-R {0-9a-z"%#:-=. CTRL-F CTRL-P CTRL-W CTRL-A}
187 CTRL-R CTRL-O {0-9a-z"%#:-=. CTRL-F CTRL-P CTRL-W CTRL-A}
188 Insert register or object under the cursor. Works like
189 |c_CTRL-R| but inserts the text literally. For example, if
190 register a contains "xy^Hz" (where ^H is a backspace),
191 "CTRL-R a" will insert "xz" while "CTRL-R CTRL-R a" will
194 CTRL-\ e {expr} *c_CTRL-\_e*
195 Evaluate {expr} and replace the whole command line with the
196 result. You will be prompted for the expression, type <Enter>
197 to finish it. It's most useful in mappings though. See
199 See |c_CTRL-R_=| for inserting the result of an expression.
200 Useful functions are |getcmdtype()|, |getcmdline()| and
202 The cursor position is unchanged, except when the cursor was
203 at the end of the line, then it stays at the end.
204 |setcmdpos()| can be used to set the cursor position.
205 The |sandbox| is used for evaluating the expression to avoid
208 :cmap <F7> <C-\>eAppendSome()<CR>
210 :let cmd = getcmdline() . " Some()"
211 :" place the cursor on the )
212 :call setcmdpos(strlen(cmd))
215 < This doesn't work recursively, thus not when already editing
219 CTRL-Y When there is a modeless selection, copy the selection into
220 the clipboard. |modeless-selection|
221 If there is no selection CTRL-Y is inserted as a character.
223 CTRL-J *c_CTRL-J* *c_<NL>* *c_<CR>*
224 <CR> or <NL> start entered command
226 <Esc> When typed and 'x' not present in 'cpoptions', quit
227 Command-line mode without executing. In macros or when 'x'
228 present in 'cpoptions', start entered command.
229 Note: If your <Esc> key is hard to hit on your keyboard, train
230 yourself to use CTRL-[.
232 CTRL-C quit command-line without executing
235 <Up> recall older command-line from history, whose beginning
236 matches the current command-line (see below).
237 {not available when compiled without the |+cmdline_hist|
240 <Down> recall more recent command-line from history, whose beginning
241 matches the current command-line (see below).
242 {not available when compiled without the |+cmdline_hist|
245 *c_<S-Up>* *c_<PageUp>*
247 recall older command-line from history
248 {not available when compiled without the |+cmdline_hist|
250 *c_<S-Down>* *c_<PageDown>*
251 <S-Down> or <PageDown>
252 recall more recent command-line from history
253 {not available when compiled without the |+cmdline_hist|
256 CTRL-D command-line completion (see |cmdline-completion|)
258 command-line completion (see |cmdline-completion|)
259 CTRL-N command-line completion (see |cmdline-completion|)
260 CTRL-P command-line completion (see |cmdline-completion|)
261 CTRL-A command-line completion (see |cmdline-completion|)
262 CTRL-L command-line completion (see |cmdline-completion|)
265 CTRL-_ a - switch between Hebrew and English keyboard mode, which is
266 private to the command-line and not related to hkmap.
267 This is useful when Hebrew text entry is required in the
268 command-line, searches, abbreviations, etc. Applies only if
269 Vim is compiled with the |+rightleft| feature and the
270 'allowrevins' option is set.
273 b - switch between Farsi and English keyboard mode, which is
274 private to the command-line and not related to fkmap. In
275 Farsi keyboard mode the characters are inserted in reverse
276 insert manner. This is useful when Farsi text entry is
277 required in the command-line, searches, abbreviations, etc.
278 Applies only if Vim is compiled with the |+farsi| feature.
282 CTRL-^ Toggle the use of language |:lmap| mappings and/or Input
284 When typing a pattern for a search command and 'imsearch' is
285 not -1, VAL is the value of 'imsearch', otherwise VAL is the
287 When language mappings are defined:
288 - If VAL is 1 (langmap mappings used) it becomes 0 (no langmap
290 - If VAL was not 1 it becomes 1, thus langmap mappings are
292 When no language mappings are defined:
293 - If VAL is 2 (Input Method is used) it becomes 0 (no input
295 - If VAL has another value it becomes 2, thus the Input Method
297 These language mappings are normally used to type characters
298 that are different from what the keyboard produces. The
299 'keymap' option can be used to install a whole number of them.
300 When entering a command line, langmap mappings are switched
301 off, since you are expected to type a command. After
302 switching it on with CTRL-^, the new state is not used again
303 for the next command or Search pattern.
307 CTRL-] Trigger abbreviation, without inserting a character. {not in
310 For Emacs-style editing on the command-line see |emacs-keys|.
312 The <Up> and <Down> keys take the current command-line as a search string.
313 The beginning of the next/previous command-lines are compared with this
314 string. The first line that matches is the new command-line. When typing
315 these two keys repeatedly, the same string is used again. For example, this
316 can be used to find the previous substitute command: Type ":s" and then <Up>.
317 The same could be done by typing <S-Up> a number of times until the desired
318 command-line is shown. (Note: the shifted arrow keys do not work on all
322 :his[tory] Print the history of last entered commands.
324 {not available when compiled without the |+cmdline_hist|
327 :his[tory] [{name}] [{first}][, [{last}]]
328 List the contents of history {name} which can be:
329 c[md] or : command-line history
330 s[earch] or / search string history
331 e[xpr] or = expression register history
332 i[nput] or @ input line history
333 d[ebug] or > debug command history
334 a[ll] all of the above
337 If the numbers {first} and/or {last} are given, the respective
338 range of entries from a history is listed. These numbers can
339 be specified in the following form:
341 A positive number represents the absolute index of an entry
342 as it is given in the first column of a :history listing.
343 This number remains fixed even if other entries are deleted.
345 A negative number means the relative position of an entry,
346 counted from the newest entry (which has index -1) backwards.
349 List entries 6 to 12 from the search history: >
352 List the recent five entries from all histories: >
355 ==============================================================================
356 2. Command-line completion *cmdline-completion*
358 When editing the command-line, a few commands can be used to complete the
359 word before the cursor. This is available for:
361 - Command names: At the start of the command-line.
362 - Tags: Only after the ":tag" command.
363 - File names: Only after a command that accepts a file name or a setting for
364 an option that can be set to a file name. This is called file name
366 - Shell command names: After ":!cmd", ":r !cmd" and ":w !cmd". $PATH is used.
367 - Options: Only after the ":set" command.
368 - Mappings: Only after a ":map" or similar command.
369 - Variable and function names: Only after a ":if", ":call" or similar command.
371 When Vim was compiled with the |+cmdline_compl| feature disabled, only file
372 names, directories and help items can be completed. The number of help item
373 matches is limited (currently to 300) to avoid a long delay when there are
376 These are the commands that can be used:
379 CTRL-D List names that match the pattern in front of the cursor.
380 When showing file names, directories are highlighted (see
381 'highlight' option). Names where 'suffixes' matches are moved
383 The 'wildoptions' option can be set to "tagfile" to list the
384 file of matching tags.
385 *c_CTRL-I* *c_wildchar* *c_<Tab>*
387 A match is done on the pattern in front of the cursor. The
388 match (if there are several, the first match) is inserted
389 in place of the pattern. (Note: does not work inside a
390 macro, because <Tab> or <Esc> are mostly used as 'wildchar',
391 and these have a special meaning in some macros.) When typed
392 again and there were multiple matches, the next
393 match is inserted. After the last match, the first is used
395 The behavior can be changed with the 'wildmode' option.
397 CTRL-N After using 'wildchar' which got multiple matches, go to next
398 match. Otherwise recall more recent command-line from history.
399 <S-Tab> *c_CTRL-P* *c_<S-Tab>*
400 CTRL-P After using 'wildchar' which got multiple matches, go to
401 previous match. Otherwise recall older command-line from
402 history. <S-Tab> only works with the GUI, on the Amiga and
405 CTRL-A All names that match the pattern in front of the cursor are
408 CTRL-L A match is done on the pattern in front of the cursor. If
409 there is one match, it is inserted in place of the pattern.
410 If there are multiple matches the longest common part is
411 inserted in place of the pattern. If the result is shorter
412 than the pattern, no completion is done.
413 When 'incsearch' is set, entering a search pattern for "/" or
414 "?" and the current match is displayed then CTRL-L will add
415 one character from the end of the current match.
417 The 'wildchar' option defaults to <Tab> (CTRL-E when in Vi compatible mode; in
418 a previous version <Esc> was used). In the pattern standard wildcards '*' and
419 '?' are accepted. '*' matches any string, '?' matches exactly one character.
421 If you like tcsh's autolist completion, you can use this mapping:
422 :cnoremap X <C-L><C-D>
423 (Where X is the command key to use, <C-L> is CTRL-L and <C-D> is CTRL-D)
424 This will find the longest match and then list all matching files.
426 If you like tcsh's autolist completion, you can use the 'wildmode' option to
427 emulate it. For example, this mimics autolist=ambiguous:
428 :set wildmode=longest,list
429 This will find the longest match with the first 'wildchar', then list all
430 matching files with the next.
433 For file name completion you can use the 'suffixes' option to set a priority
434 between files with almost the same name. If there are multiple matches,
435 those files with an extension that is in the 'suffixes' option are ignored.
436 The default is ".bak,~,.o,.h,.info,.swp,.obj", which means that files ending
437 in ".bak", "~", ".o", ".h", ".info", ".swp" and ".obj" are sometimes ignored.
438 It is impossible to ignore suffixes with two dots. Examples:
440 pattern: files: match: ~
441 test* test.c test.h test.o test.c
442 test* test.h test.o test.h and test.o
443 test* test.i test.h test.c test.i and test.c
445 If there is more than one matching file (after ignoring the ones matching
446 the 'suffixes' option) the first file name is inserted. You can see that
447 there is only one match when you type 'wildchar' twice and the completed
448 match stays the same. You can get to the other matches by entering
449 'wildchar', CTRL-N or CTRL-P. All files are included, also the ones with
450 extensions matching the 'suffixes' option.
452 To completely ignore files with some extension use 'wildignore'.
454 To match only files that end at the end of the typed text append a "$". For
455 example, to match only files that end in ".c": >
457 This will not match a file ending in ".cpp". Without the "$" it does match.
459 The old value of an option can be obtained by hitting 'wildchar' just after
460 the '='. For example, typing 'wildchar' after ":set dir=" will insert the
461 current value of 'dir'. This overrules file name completion for the options
462 that take a file name.
464 If you would like using <S-Tab> for CTRL-P in an xterm, put this command in
466 xmodmap -e "keysym Tab = Tab Find"
467 And this in your .vimrc: >
470 ==============================================================================
471 3. Ex command-lines *cmdline-lines*
473 The Ex commands have a few specialties:
476 '"' at the start of a line causes the whole line to be ignored. '"'
477 after a command causes the rest of the line to be ignored. This can be used
478 to add comments. Example: >
479 :set ai "set 'autoindent' option
480 It is not possible to add a comment to a shell command ":!cmd" or to the
481 ":map" command and friends, because they see the '"' as part of their
485 '|' can be used to separate commands, so you can give multiple commands in one
486 line. If you want to use '|' in an argument, precede it with '\'.
488 These commands see the '|' as their argument, and can therefore not be
489 followed by another Vim command:
521 a user defined command without the "-bar" argument |:command|
523 Note that this is confusing (inherited from Vi): With ":g" the '|' is included
524 in the command, with ":s" it is not.
526 To be able to use another command anyway, use the ":execute" command.
527 Example (append the output of "ls" and jump to the first line): >
528 :execute 'r !ls' | '[
530 There is one exception: When the 'b' flag is present in 'cpoptions', with the
531 ":map" and ":abbr" commands and friends CTRL-V needs to be used instead of
532 '\'. You can also use "<Bar>" instead. See also |map_bar|.
535 :!ls | wc view the output of two commands
536 :r !ls | wc insert the same output in the text
537 :%g/foo/p|> moves all matching lines one shiftwidth
538 :%s/foo/bar/|> moves one line one shiftwidth
539 :map q 10^V| map "q" to "10|"
540 :map q 10\| map \ l map "q" to "10\" and map "\" to "l"
541 (when 'b' is present in 'cpoptions')
543 You can also use <NL> to separate commands in the same way as with '|'. To
544 insert a <NL> use CTRL-V CTRL-J. "^@" will be shown. Using '|' is the
545 preferred method. But for external commands a <NL> must be used, because a
546 '|' is included in the external command. To avoid the special meaning of <NL>
547 it must be preceded with a backslash. Example: >
549 This reads the current date into the file and joins it with the previous line.
551 Note that when the command before the '|' generates an error, the following
552 commands will not be executed.
555 Because of Vi compatibility the following strange commands are supported: >
556 :| print current line (like ":p")
557 :3| print line 3 (like ":3p")
560 A colon is allowed between the range and the command name. It is ignored
561 (this is Vi compatible). For example: >
564 When the character '%' or '#' is used where a file name is expected, they are
565 expanded to the current and alternate file name (see the chapter "editing
568 Embedded spaces in file names are allowed on the Amiga if one file name is
569 expected as argument. Trailing spaces will be ignored, unless escaped with a
570 backslash or CTRL-V. Note that the ":next" command uses spaces to separate
571 file names. Escape the spaces to include them in a file name. Example: >
572 :next foo\ bar goes\ to school\
573 starts editing the three files "foo bar", "goes to" and "school ".
575 When you want to use the special characters '"' or '|' in a command, or want
576 to use '%' or '#' in a file name, precede them with a backslash. The
577 backslash is not required in a range and in the ":substitute" command.
580 The '!' (bang) character after an Ex command makes the command behave in a
581 different way. The '!' should be placed immediately after the command, without
582 any blanks in between. If you insert blanks the '!' will be seen as an
583 argument for the command, which has a different meaning. For example:
584 :w! name write the current buffer to file "name", overwriting
586 :w !name send the current buffer as standard input to command
589 ==============================================================================
590 4. Ex command-line ranges *cmdline-ranges* *[range]* *E16*
592 Some Ex commands accept a line range in front of them. This is noted as
593 [range]. It consists of one or more line specifiers, separated with ',' or
596 The basics are explained in section |10.3| of the user manual.
599 When separated with ';' the cursor position will be set to that line
600 before interpreting the next line specifier. This doesn't happen for ','.
603 < from line 4 till match with "this line" after the cursor line. >
605 < from line 5 till match with "that line" after line 5.
607 The default line specifier for most commands is the cursor position, but the
608 commands ":write" and ":global" have the whole file (1,$) as default.
610 If more line specifiers are given than required for the command, the first
611 one(s) will be ignored.
613 Line numbers may be specified with: *:range* *E14* *{address}*
614 {number} an absolute line number
615 . the current line *:.*
616 $ the last line in the file *:$*
617 % equal to 1,$ (the entire file) *:%*
618 't position of mark t (lowercase) *:'*
619 'T position of mark T (uppercase); when the mark is in
620 another file it cannot be used in a range
621 /{pattern}[/] the next line where {pattern} matches *:/*
622 ?{pattern}[?] the previous line where {pattern} matches *:?*
623 \/ the next line where the previously used search
625 \? the previous line where the previously used search
627 \& the next line where the previously used substitute
630 Each may be followed (several times) by '+' or '-' and an optional number.
631 This number is added or subtracted from the preceding line number. If the
632 number is omitted, 1 is used.
634 The "/" and "?" after {pattern} are required to separate the pattern from
635 anything that follows.
637 The "/" and "?" may be preceded with another address. The search starts from
638 there. The difference from using ';' is that the cursor isn't moved.
640 /pat1//pat2/ Find line containing "pat2" after line containing
641 "pat1", without moving the cursor.
642 7;/pat2/ Find line containing "pat2", after line 7, leaving
643 the cursor in line 7.
645 The {number} must be between 0 and the number of lines in the file. When
646 using a 0 (zero) this is interpreted as a 1 by most commands. Commands that
647 use it as a count do use it as a zero (|:tag|, |:pop|, etc). Some commands
648 interpret the zero as "before the first line" (|:read|, search pattern, etc).
651 .+3 three lines below the cursor
652 /that/+1 the line below the next line containing "that"
653 .,$ from current line until end of file
654 0;/that the first line containing "that", also matches in the
656 1;/that the first line after line 1 containing "that"
658 Some commands allow for a count after the command. This count is used as the
659 number of lines to be used, starting with the line given in the last line
660 specifier (the default is the cursor line). The commands that accept a count
661 are the ones that use a range but do not have a file name argument (because
662 a file name can also be a number).
665 :s/x/X/g 5 substitute 'x' by 'X' in the current line and four
667 :23d 4 delete lines 23, 24, 25 and 26
672 When folds are active the line numbers are rounded off to include the whole
673 closed fold. See |fold-behavior|.
678 A range should have the lower line number first. If this is not the case, Vim
679 will ask you if it should swap the line numbers.
680 Backwards range given, OK to swap ~
681 This is not done within the global command ":g".
683 You can use ":silent" before a command to avoid the question, the range will
684 always be swapped then.
689 When giving a count before entering ":", this is translated into:
691 In words: The 'count' lines at and after the cursor. Example: To delete
693 3:d<CR> is translated into: .,.+2d<CR>
696 Visual Mode and Range *v_:*
698 {Visual}: Starts a command-line with the Visual selected lines as a
699 range. The code ":'<,'>" is used for this range, which makes
700 it possible to select a similar line from the command-line
701 history for repeating a command on different Visually selected
704 ==============================================================================
705 5. Ex command-line flags *ex-flags*
707 These flags are supported by a selection of Ex commands. They print the line
708 that the cursor ends up after executing the command:
710 l output like for |:list|
712 p output like for |:print|
714 The flags can be combined, thus "l#" uses both a line number and |:list| style
717 ==============================================================================
718 6. Ex special characters *cmdline-special*
720 Note: These are special characters in the executed command line. If you want
721 to insert special things while typing you can use the CTRL-R command. For
722 example, "%" stands for the current file name, while CTRL-R % inserts the
723 current file name right away. See |c_CTRL-R|.
725 Note: If you want to avoid the special characters in a Vim script you may want
726 to use |fnameescape()|.
729 In Ex commands, at places where a file name can be used, the following
730 characters have a special meaning. These can also be used in the expression
731 function expand() |expand()|.
732 % is replaced with the current file name *:_%*
733 # is replaced with the alternate file name *:_#*
734 #n (where n is a number) is replaced with the file name of
735 buffer n. "#0" is the same as "#"
736 ## is replaced with all names in the argument list *:_##*
737 concatenated, separated by spaces. Each space in a name
738 is preceded with a backslash.
739 Note that these give the file name as it was typed. If an absolute path is
740 needed (when using the file name from a different directory), you need to add
741 ":p". See |filename-modifiers|.
742 Note that backslashes are inserted before spaces, so that the command will
743 correctly interpret the file name. But this doesn't happen for shell
744 commands. For those you probably have to use quotes: >
748 To avoid the special meaning of '%' and '#' insert a backslash before it.
749 Detail: The special meaning is always escaped when there is a backslash before
750 it, no matter how many backslashes.
756 *:<cword>* *:<cWORD>* *:<cfile>* *<cfile>*
757 *:<sfile>* *<sfile>* *:<afile>* *<afile>*
758 *:<abuf>* *<abuf>* *:<amatch>* *<amatch>*
759 *E495* *E496* *E497* *E498* *E499* *E500*
760 Note: these are typed literally, they are not special keys!
761 <cword> is replaced with the word under the cursor (like |star|)
762 <cWORD> is replaced with the WORD under the cursor (see |WORD|)
763 <cfile> is replaced with the path name under the cursor (like what
765 <afile> when executing autocommands, is replaced with the file name
766 for a file read or write
767 <abuf> when executing autocommands, is replaced with the currently
768 effective buffer number (for ":r file" and ":so file" it is
769 the current buffer, the file being read/sourced is not in a
771 <amatch> when executing autocommands, is replaced with the match for
772 which this autocommand was executed. It differs from
773 <afile> only when the file name isn't used to match with
774 (for FileType, Syntax and SpellFileMissing events).
775 <sfile> when executing a ":source" command, is replaced with the
776 file name of the sourced file;
777 when executing a function, is replaced with
778 "function {function-name}"; function call nesting is
780 "function {function-name1}..{function-name2}". Note that
781 filename-modifiers are useless when <sfile> is used inside
785 *:_%:* *::8* *::p* *::.* *::~* *::h* *::t* *::r* *::e* *::s* *::gs*
786 The file name modifiers can be used after "%", "#", "#n", "<cfile>", "<sfile>",
787 "<afile>" or "<abuf>". They are also used with the |fnamemodify()| function.
788 These are not available when Vim has been compiled without the |+modify_fname|
790 These modifiers can be given, in this order:
791 :p Make file name a full path. Must be the first modifier. Also
792 changes "~/" (and "~user/" for Unix and VMS) to the path for
793 the home directory. If the name is a directory a path
794 separator is added at the end. For a file name that does not
795 exist and does not have an absolute path the result is
797 :8 Converts the path to 8.3 short format (currently only on
798 win32). Will act on as much of a path that is an existing
800 :~ Reduce file name to be relative to the home directory, if
801 possible. File name is unmodified if it is not below the home
803 :. Reduce file name to be relative to current directory, if
804 possible. File name is unmodified if it is not below the
806 For maximum shortness, use ":~:.".
807 :h Head of the file name (the last component and any separators
808 removed). Cannot be used with :e, :r or :t.
809 Can be repeated to remove several components at the end.
810 When the file name ends in a path separator, only the path
811 separator is removed. Thus ":p:h" on a directory name results
812 on the directory name itself (without trailing slash).
813 When the file name is an absolute path (starts with "/" for
814 Unix; "x:\" for MS-DOS, WIN32, OS/2; "drive:" for Amiga), that
815 part is not removed. When there is no head (path is relative
816 to current directory) the result is empty.
817 :t Tail of the file name (last component of the name). Must
818 precede any :r or :e.
819 :r Root of the file name (the last extension removed). When
820 there is only an extension (file name that starts with '.',
821 e.g., ".vimrc"), it is not removed. Can be repeated to remove
822 several extensions (last one first).
823 :e Extension of the file name. Only makes sense when used alone.
824 When there is no extension the result is empty.
825 When there is only an extension (file name that starts with
826 '.'), the result is empty. Can be repeated to include more
827 extensions. If there are not enough extensions (but at least
828 one) as much as possible are included.
830 Substitute the first occurrence of "pat" with "sub". This
831 works like the |:s| command. "pat" is a regular expression.
832 Any character can be used for '?', but it must not occur in
834 After this, the previous modifiers can be used again. For
835 example ":p", to make a full path after the substitution.
837 Substitute all occurrences of "path" with "sub". Otherwise
838 this works like ":s".
840 Examples, when the file name is "src/version.c", current dir
842 :p /home/mool/vim/src/version.c
844 :p:~ ~/vim/src/version.c
846 :p:h /home/mool/vim/src
847 :p:h:h /home/mool/vim
851 :p:r /home/mool/vim/src/version
854 :s?version?main? src/main.c
855 :s?version?main?:p /home/mool/vim/src/main.c
856 :p:gs?/?\\? \home\mool\vim\src\version.c
858 Examples, when the file name is "src/version.c.gz": >
859 :p /home/mool/vim/src/version.c.gz
869 *extension-removal* *:_%<*
870 If a "<" is appended to "%", "#", "#n" or "CTRL-V p" the extension of the file
871 name is removed (everything after and including the last '.' in the file
872 name). This is included for backwards compatibility with version 3.0, the
873 ":r" form is preferred. Examples: >
876 %< current file name without extension
877 # alternate file name for current window
878 #< idem, without extension
879 #31 alternate file number 31
880 #31< idem, without extension
881 <cword> word under the cursor
882 <cWORD> WORD under the cursor (see |WORD|)
883 <cfile> path name under the cursor
884 <cfile>< idem, without extension
886 Note: Where a file name is expected wildcards expansion is done. On Unix the
887 shell is used for this, unless it can be done internally (for speed).
888 Backticks also work, like in >
890 (backtick expansion is not possible in |restricted-mode|)
891 But expansion is only done if there are any wildcards before expanding the
892 '%', '#', etc.. This avoids expanding wildcards inside a file name. If you
893 want to expand the result of <cfile>, add a wildcard character to it.
894 Examples: (alternate file name is "?readme?")
897 :e `ls #` :e {files matching "?readme?"}
898 :e #.* :e {files matching "?readme?.*"}
899 :cd <cfile> :cd {file name under cursor}
900 :cd <cfile>* :cd {file name under cursor plus "*" and then expanded}
902 When the expanded argument contains a "!" and it is used for a shell command
903 (":!cmd", ":r !cmd" or ":w !cmd"), the "!" is escaped with a backslash to
904 avoid it being expanded into a previously used command. When the 'shell'
905 option contains "sh", this is done twice, to avoid the shell trying to expand
909 For filesystems that use a backslash as directory separator (MS-DOS, Windows,
910 OS/2), it's a bit difficult to recognize a backslash that is used to escape
911 the special meaning of the next character. The general rule is: If the
912 backslash is followed by a normal file name character, it does not have a
913 special meaning. Therefore "\file\foo" is a valid file name, you don't have
914 to type the backslash twice.
916 An exception is the '$' sign. It is a valid character in a file name. But
917 to avoid a file name like "$home" to be interpreted as an environment variable,
918 it needs to be preceded by a backslash. Therefore you need to use "/\$home"
919 for the file "$home" in the root directory. A few examples:
921 FILE NAME INTERPRETED AS ~
922 $home expanded to value of environment var $home
923 \$home file "$home" in current directory
924 /\$home file "$home" in root directory
925 \\$home file "\\", followed by expanded $home
927 ==============================================================================
928 6. Command-line window *cmdline-window* *cmdwin*
930 In the command-line window the command line can be edited just like editing
931 text in any window. It is a special kind of window, because you cannot leave
933 {not available when compiled without the |+cmdline_hist| or |+vertsplit|
939 There are two ways to open the command-line window:
940 1. From Command-line mode, use the key specified with the 'cedit' option.
941 The default is CTRL-F when 'compatible' is not set.
942 2. From Normal mode, use the "q:", "q/" or "q?" command. *q:* *q/* *q?*
943 This starts editing an Ex command-line ("q:") or search string ("q/" or
944 "q?"). Note that this is not possible while recording is in progress (the
945 "q" stops recording then).
947 When the window opens it is filled with the command-line history. The last
948 line contains the command as typed so far. The left column will show a
949 character that indicates the type of command-line being edited, see
952 Vim will be in Normal mode when the editor is opened, except when 'insertmode'
955 The height of the window is specified with 'cmdwinheight' (or smaller if there
956 is no room). The window is always full width and is positioned just above the
962 You can now use commands to move around and edit the text in the window. Both
963 in Normal mode and Insert mode.
965 It is possible to use ":", "/" and other commands that use the command-line,
966 but it's not possible to open another command-line window then. There is no
969 The command-line window is not a normal window. It is not possible to move to
970 another window or edit another buffer. All commands that would do this are
971 disabled in the command-line window. Of course it _is_ possible to execute
972 any command that you entered in the command-line window.
977 There are several ways to leave the command-line window:
979 <CR> Execute the command-line under the cursor. Works both in
980 Insert and in Normal mode.
981 CTRL-C Continue in Command-line mode. The command-line under the
982 cursor is used as the command-line. Works both in Insert and
983 in Normal mode. ":close" also works. There is no redraw,
984 thus the window will remain visible.
985 :quit Discard the command line and go back to Normal mode.
986 ":exit", ":xit" and CTRL-\ CTRL-N also work.
987 :qall Quit Vim, unless there are changes in some buffer.
988 :qall! Quit Vim, discarding changes to any buffer.
990 Once the command-line window is closed the old window sizes are restored. The
991 executed command applies to the window and buffer where the command-line was
992 started from. This works as if the command-line window was not there, except
993 that there will be an extra screen redraw.
994 The buffer used for the command-line window is deleted. Any changes to lines
995 other than the one that is executed with <CR> are lost.
997 If you would like to execute the command under the cursor and then have the
998 command-line window open again, you may find this mapping useful: >
1000 :autocmd CmdwinEnter * map <buffer> <F5> <CR>q:
1005 The command-line window cannot be used:
1006 - when there already is a command-line window (no nesting)
1007 - for entering a encryption key or when using inputsecret()
1008 - when Vim was not compiled with the +vertsplit feature
1010 Some options are set when the command-line window is opened:
1011 'filetype' "vim", when editing an Ex command-line; this starts Vim syntax
1012 highlighting if it was enabled
1018 It is allowed to write the buffer contents to a file. This is an easy way to
1019 save the command-line history and read it back later.
1021 If the 'wildchar' option is set to <Tab>, and the command-line window is used
1022 for an Ex command, then two mappings will be added to use <Tab> for completion
1023 in the command-line window, like this: >
1024 :imap <buffer> <Tab> <C-X><C-V>
1025 :nmap <buffer> <Tab> a<C-X><C-V>
1026 Note that hitting <Tab> in Normal mode will do completion on the next
1027 character. That way it works at the end of the line.
1028 If you don't want these mappings, disable them with: >
1029 au CmdwinEnter [:>] iunmap <Tab>
1030 au CmdwinEnter [:>] nunmap <Tab>
1031 You could put these lines in your vimrc file.
1033 While in the command-line window you cannot use the mouse to put the cursor in
1034 another window, or drag statuslines of other windows. You can drag the
1035 statusline of the command-line window itself and the statusline above it.
1036 Thus you can resize the command-line window, but not others.
1041 Two autocommand events are used: |CmdwinEnter| and |CmdwinLeave|. Since this
1042 window is of a special type, the WinEnter, WinLeave, BufEnter and BufLeave
1043 events are not triggered. You can use the Cmdwin events to do settings
1044 specifically for the command-line window. Be careful not to cause side
1047 :au CmdwinEnter : let b:cpt_save = &cpt | set cpt=.
1048 :au CmdwinLeave : let &cpt = b:cpt_save
1049 This sets 'complete' to use completion in the current window for |i_CTRL-N|.
1051 :au CmdwinEnter [/?] startinsert
1052 This will make Vim start in Insert mode in the command-line window.
1055 The character used for the pattern indicates the type of command-line:
1057 > debug mode command |debug-mode|
1058 / forward search string
1059 ? backward search string
1060 = expression for "= |expr-register|
1061 @ string for |input()|
1062 - text for |:insert| or |:append|
1064 vim:tw=78:ts=8:ft=help:norl: