robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
73.2 KiB1837 linesraw
1*map.txt*       Nvim
2
3
4		  VIM REFERENCE MANUAL	  by Bram Moolenaar
5
6
7Key mapping, abbreviations and user-defined commands.
8
9This subject is introduced in sections |05.3|, |24.7| and |40.1| of the user
10manual.
11
12                                      Type |gO| to see the table of contents.
13
14==============================================================================
151. Key mapping				*keybind* *key-mapping* *mapping*
16
17Key mapping is used to change the meaning of typed keys.  The most common use
18is to define a sequence of commands for a function key.  Example: >
19
20	:map <F2> a<C-R>=strftime("%c")<CR><Esc>
21
22This appends the current date and time after the cursor (in <> notation |<>|).
23
24
251.1 MAP COMMANDS					*:map-commands*
26
27There are commands to enter new mappings, remove mappings and list mappings.
28See |map-overview| for the various forms of "map" and their relationships with
29modes.
30
31{lhs}	means left-hand-side	*{lhs}*
32{rhs}	means right-hand-side	*{rhs}*
33
34:map	{lhs} {rhs}		|mapmode-nvo|		*:map*
35:nm[ap]	{lhs} {rhs}		|mapmode-n|		*:nm* *:nmap*
36:vm[ap]	{lhs} {rhs}		|mapmode-v|		*:vm* *:vmap*
37:xm[ap]	{lhs} {rhs}		|mapmode-x|		*:xm* *:xmap*
38:smap	{lhs} {rhs}		|mapmode-s|		    *:smap*
39:om[ap]	{lhs} {rhs}		|mapmode-o|		*:om* *:omap*
40:map!	{lhs} {rhs}		|mapmode-ic|		*:map!*
41:im[ap]	{lhs} {rhs}		|mapmode-i|		*:im* *:imap*
42:lm[ap]	{lhs} {rhs}		|mapmode-l|		*:lm* *:lma* *:lmap*
43:cm[ap]	{lhs} {rhs}		|mapmode-c|		*:cm* *:cmap*
44:tma[p]	{lhs} {rhs}		|mapmode-t|		*:tma* *:tmap*
45			Map the key sequence {lhs} to {rhs} for the modes
46			where the map command applies.  The result, including
47			{rhs}, is then further scanned for mappings.  This
48			allows for nested and recursive use of mappings.
49			Note: Trailing spaces are included in the {rhs},
50			because space is a valid Normal mode command.
51			See |map-trailing-white|.
52
53						*:nore* *:norem*
54:no[remap]  {lhs} {rhs}		|mapmode-nvo|	*:no*  *:noremap* *:nor*
55:nn[oremap] {lhs} {rhs}		|mapmode-n|	*:nn*  *:nnoremap*
56:vn[oremap] {lhs} {rhs}		|mapmode-v|	*:vn*  *:vnoremap*
57:xn[oremap] {lhs} {rhs}		|mapmode-x|	*:xn*  *:xnoremap*
58:snor[emap] {lhs} {rhs}		|mapmode-s|	*:snor* *:snore* *:snoremap*
59:ono[remap] {lhs} {rhs}		|mapmode-o|	*:ono* *:onoremap*
60:no[remap]! {lhs} {rhs}		|mapmode-ic|	*:no!* *:noremap!*
61:ino[remap] {lhs} {rhs}		|mapmode-i|	*:ino* *:inor* *:inoremap*
62:ln[oremap] {lhs} {rhs}		|mapmode-l|	*:ln*  *:lnoremap*
63:cno[remap] {lhs} {rhs}		|mapmode-c|	*:cno* *:cnor* *:cnoremap*
64:tno[remap] {lhs} {rhs}		|mapmode-t|	*:tno* *:tnoremap*
65			Map the key sequence {lhs} to {rhs} for the modes
66			where the map command applies.  Disallow mapping of
67			{rhs}, to avoid nested and recursive mappings.  Often
68			used to redefine a command.
69			Note: Keys in {rhs} also won't trigger abbreviation,
70			with the exception of |i_CTRL-]| and |c_CTRL-]|.
71			Note: When <Plug> appears in the {rhs} this part is
72			always applied even if remapping is disallowed.
73
74
75:unm[ap]  {lhs}			|mapmode-nvo|		*:unm*  *:unmap*
76:nun[map] {lhs}			|mapmode-n|		*:nun*  *:nunmap*
77:vu[nmap] {lhs}			|mapmode-v|		*:vu*   *:vunmap*
78:xu[nmap] {lhs}			|mapmode-x|		*:xu*   *:xunmap*
79:sunm[ap] {lhs}			|mapmode-s|		*:sunm* *:sunmap*
80:ou[nmap] {lhs}			|mapmode-o|		*:ou*   *:ounmap*
81:unm[ap]! {lhs}			|mapmode-ic|		*:unm!* *:unmap!*
82:iu[nmap] {lhs}			|mapmode-i|		*:iu*   *:iunmap*
83:lu[nmap] {lhs}			|mapmode-l|		*:lu*   *:lunmap*
84:cu[nmap] {lhs}			|mapmode-c|		*:cu*   *:cun* *:cunmap*
85:tunma[p] {lhs}			|mapmode-t|		*:tunma* *:tunmap*
86			Remove the mapping of {lhs} for the modes where the
87			map command applies.  The mapping may remain defined
88			for other modes where it applies.
89			If no mapping with {lhs} exists, it also works when
90			{lhs} matches the {rhs} of a mapping (see
91			|vim.keymap.del()| to avoid that).  This is for
92			when an abbreviation applied.
93			Note: Trailing spaces are included in the {lhs}.
94			See |map-trailing-white|.
95
96:mapc[lear]			|mapmode-nvo|		*:mapc*   *:mapclear*
97:nmapc[lear]			|mapmode-n|		*:nmapc*  *:nmapclear*
98:vmapc[lear]			|mapmode-v|		*:vmapc*  *:vmapclear*
99:xmapc[lear]			|mapmode-x|		*:xmapc*  *:xmapclear*
100:smapc[lear]			|mapmode-s|		*:smapc*  *:smapclear*
101:omapc[lear]			|mapmode-o|		*:omapc*  *:omapclear*
102:mapc[lear]!			|mapmode-ic|		*:mapc!*  *:mapclear!*
103:imapc[lear]			|mapmode-i|		*:imapc*  *:imapclear*
104:lmapc[lear]			|mapmode-l|		*:lmapc*  *:lmapclear*
105:cmapc[lear]			|mapmode-c|		*:cmapc*  *:cmapclear*
106:tmapc[lear]			|mapmode-t|		*:tmapc*  *:tmapclear*
107			Remove ALL mappings for the modes where the map
108			command applies.
109			Use the <buffer> argument to remove buffer-local
110			mappings |:map-<buffer>|
111			Warning: This also removes the |default-mappings|.
112
113:map				|mapmode-nvo|
114:nm[ap]				|mapmode-n|
115:vm[ap]				|mapmode-v|
116:xm[ap]				|mapmode-x|
117:sm[ap]				|mapmode-s|
118:om[ap]				|mapmode-o|
119:map!				|mapmode-ic|
120:im[ap]				|mapmode-i|
121:lm[ap]				|mapmode-l|
122:cm[ap]				|mapmode-c|
123:tma[p]				|mapmode-t|
124			List all key mappings for the modes where the map
125			command applies.  Note that ":map" and ":map!" are
126			used most often, because they include the other modes.
127
128:map    {lhs}			|mapmode-nvo|		*:map_l*
129:nm[ap] {lhs}			|mapmode-n|		*:nmap_l*
130:vm[ap] {lhs}			|mapmode-v|		*:vmap_l*
131:xm[ap] {lhs}			|mapmode-x|		*:xmap_l*
132:sm[ap] {lhs}			|mapmode-s|		*:smap_l*
133:om[ap] {lhs}			|mapmode-o|		*:omap_l*
134:map!   {lhs}			|mapmode-ic|		*:map_l!*
135:im[ap] {lhs}			|mapmode-i|		*:imap_l*
136:lm[ap] {lhs}			|mapmode-l|		*:lmap_l*
137:cm[ap] {lhs}			|mapmode-c|		*:cmap_l*
138:tma[p] {lhs}			|mapmode-t|		*:tmap_l*
139			List the key mappings for the key sequences starting
140			with {lhs} in the modes where the map command applies.
141
142These commands are used to map a key or key sequence to a string of
143characters.  You can use this to put command sequences under function keys,
144translate one key into another, etc.  See |:mkexrc| for how to save and
145restore the current mappings.
146
147							*map-ambiguous*
148When two mappings start with the same sequence of characters, they are
149ambiguous.  Example: >
150	:imap aa foo
151	:imap aaa bar
152When Vim has read "aa", it will need to get another character to be able to
153decide if "aa" or "aaa" should be mapped.  This means that after typing "aa"
154that mapping won't get expanded yet, Vim is waiting for another character.
155If you type a space, then "foo" will get inserted, plus the space.  If you
156type "a", then "bar" will get inserted.
157
158Trailing white space ~
159							*map-trailing-white*
160This unmap command does NOT work: >
161	:map @@ foo
162	:unmap @@ | print
163
164Because it tries to unmap "@@ ", including the white space before the command
165separator "|".  Other examples with trailing white space: >
166	unmap @@ 
167	unmap @@     " comment
168
169An error will be issued, which is very hard to identify, because the ending
170whitespace character in `unmap @@ ` is not visible.
171
172A generic solution is to put the command separator "|" right after the mapped
173keys.  After that white space and a comment may follow: >
174	unmap @@|    " comment
175
176
1771.2 SPECIAL ARGUMENTS					*:map-arguments*
178
179"<buffer>", "<nowait>", "<silent>", "<script>", "<expr>" and
180"<unique>" can be used in any order.  They must appear right after the
181command, before any other arguments.
182
183					*:map-local* *:map-<buffer>* *:map-buffer*
184					*E224* *E225*
185If the first argument to one of these commands is "<buffer>" the mapping will
186be effective in the current buffer only.  Example: >
187	:map <buffer>  ,w  /[.,;]<CR>
188Then you can map ",w" to something else in another buffer: >
189	:map <buffer>  ,w  /[#&!]<CR>
190The local buffer mappings are used before the global ones.  See <nowait> below
191to make a short local mapping take effect when a longer global one exists.
192The "<buffer>" argument can also be used to clear mappings: >
193	:unmap <buffer> ,w
194	:mapclear <buffer>
195Local mappings are also cleared when a buffer is deleted, but not when it is
196unloaded.  Just like local option values.
197Also see |map-precedence|.
198
199						*:map-<nowait>* *:map-nowait*
200When defining a buffer-local mapping for "," there may be a global mapping
201that starts with ",".  Then you need to type another character for Vim to know
202whether to use the "," mapping or the longer one.  To avoid this add the
203<nowait> argument.  Then the mapping will be used when it matches, Vim does
204not wait for more characters to be typed.  However, if the characters were
205already typed they are used.
206Note that this works when the <nowait> mapping fully matches and is found
207before any partial matches.  This works when:
208- There is only one matching buffer-local mapping, since these are always
209  found before global mappings.
210- There is another buffer-local mapping that partly matches, but it is
211  defined earlier (last defined mapping is found first).
212
213						*:map-<silent>* *:map-silent*
214To define a mapping which will not be echoed on the command line, add
215"<silent>" as the first argument.  Example: >
216	:map <silent> ,h /Header<CR>
217The search string will not be echoed when using this mapping.  Messages from
218the executed command are still given though.  To shut them up too, add a
219":silent" in the executed command: >
220	:map <silent> ,h :exe ":silent normal /Header\r"<CR>
221Note that the effect of a command might also be silenced, e.g., when the
222mapping selects another entry for command line completion it won't be
223displayed.
224Prompts will still be given, e.g., for inputdialog().
225Using "<silent>" for an abbreviation is possible, but will cause redrawing of
226the command line to fail.
227
228						*:map-<script>* *:map-script*
229If the first argument to one of these commands is "<script>" and it is used to
230define a new mapping or abbreviation, the mapping will only remap characters
231in the {rhs} using mappings that were defined local to a script, starting with
232"<SID>".  This can be used to avoid that mappings from outside a script
233interfere (e.g., when CTRL-V is remapped in mswin.vim), but do use other
234mappings defined in the script.
235Note: ":map <script>" and ":noremap <script>" do the same thing.  The
236"<script>" overrules the command name.  Using ":noremap <script>" is
237preferred, because it's clearer that remapping is (mostly) disabled.
238
239				*:map-<unique>* *:map-unique* *E226* *E227*
240If the first argument to one of these commands is "<unique>" and it is used to
241define a new mapping or abbreviation, the command will fail if the mapping or
242abbreviation already exists.  Example: >
243	:map <unique> ,w  /[#&!]<CR>
244When defining a local mapping, there will also be a check if a global map
245already exists which is equal.
246Example of what will fail: >
247	:map ,w  /[#&!]<CR>
248	:map <buffer> <unique> ,w  /[.,;]<CR>
249If you want to map a key and then have it do what it was originally mapped to,
250have a look at |maparg()|.
251
252						*:map-<expr>* *:map-expression*
253If the first argument to one of these commands is "<expr>" and it is used to
254define a new mapping or abbreviation, the argument is an expression.  The
255expression is evaluated to obtain the {rhs} that is used.  Example: >
256	:inoremap <expr> . <SID>InsertDot()
257The result of the s:InsertDot() function will be inserted.  It could check the
258text before the cursor and start omni completion when some condition is met.
259Using a script-local function is preferred, to avoid polluting the global
260namespace.  Use <SID> in the RHS so that the script that the mapping was
261defined in can be found.
262
263For abbreviations |v:char| is set to the character that was typed to trigger
264the abbreviation.  You can use this to decide how to expand the {lhs}.  You
265should not either insert or change the v:char.
266
267In case you want the mapping to not do anything, you can have the expression
268evaluate to an empty string.  If something changed that requires Vim to
269go through the main loop (e.g. to update the display), return "\<Ignore>".
270This is similar to "nothing" but makes Vim return from the loop that waits for
271input.
272
273Keep in mind that the expression may be evaluated when looking for
274typeahead, before the previous command has been executed.  For example: >
275	func StoreColumn()
276	  let g:column = col('.')
277	  return 'x'
278	endfunc
279	nnoremap <expr> x StoreColumn()
280	nmap ! f!x
281You will notice that g:column has the value from before executing "f!",
282because "x" is evaluated before "f!" is executed.
283This can be solved by inserting <Ignore> before the character that is
284expression-mapped: >
285	nmap ! f!<Ignore>x
286
287Be very careful about side effects!  The expression is evaluated while
288obtaining characters, you may very well make the command dysfunctional.
289Therefore the following is blocked for <expr> mappings:
290- Changing the buffer text |textlock|.
291- Editing another buffer.
292- The |:normal| command.
293- Moving the cursor is allowed, but it is restored afterwards.
294- If the cmdline is changed, the old text and cursor position are restored.
295If you want the mapping to do any of these let the returned characters do
296that, or use a |<Cmd>| mapping instead.
297
298You can use getchar(), it consumes typeahead if there is any.  E.g., if you
299have these mappings: >
300  inoremap <expr> <C-L> nr2char(getchar())
301  inoremap <expr> <C-L>x "foo"
302If you now type CTRL-L nothing happens yet, Vim needs the next character to
303decide what mapping to use.  If you type 'x' the second mapping is used and
304"foo" is inserted.  If you type any other key the first mapping is used,
305getchar() gets the typed key and returns it.
306
307Here is an example that inserts a list number that increases: >
308	let counter = 0
309	inoremap <expr> <C-L> ListItem()
310	inoremap <expr> <C-R> ListReset()
311
312	func ListItem()
313	  let g:counter += 1
314	  return g:counter .. '. '
315	endfunc
316
317	func ListReset()
318	  let g:counter = 0
319	  return ''
320	endfunc
321
322CTRL-L inserts the next number, CTRL-R resets the count.  CTRL-R returns an
323empty string, so that nothing is inserted.
324
325Note that using 0x80 as a single byte before other text does not work, it will
326be seen as a special key.
327
328						*<Cmd>* *:map-cmd*
329The <Cmd> pseudokey begins a "command mapping", which executes the command
330directly without changing modes.  Where you might use ":…<CR>" in the {rhs} of
331a mapping, you can instead use "<Cmd>…<CR>". Example: >
332	noremap x <Cmd>echo mode(1)<CR>
333<
334This is more flexible than `:<C-U>` in Visual and Operator-pending mode, or
335`<C-O>:` in Insert mode, because the commands are executed directly in the
336current mode, instead of always going to Normal mode.  Visual mode is
337preserved, so tricks with |gv| are not needed.  Commands can be invoked
338directly in Command-line mode (which would otherwise require timer hacks).
339Example of using <Cmd> halfway Insert mode: >
340	nnoremap <F3> aText <Cmd>echo mode(1)<CR> Added<Esc>
341
342Unlike <expr> mappings, there are no special restrictions on the <Cmd>
343command: it is executed as if an (unrestricted) |autocommand| was invoked
344or an async event was processed.
345
346Note:
347- Because <Cmd> avoids mode-changes (unlike ":") it does not trigger
348  |CmdlineEnter| and |CmdlineLeave| events. This helps performance.
349- For the same reason, |keycodes| like <C-R><C-W> are interpreted as plain,
350  unmapped keys.
351- The command is not echo'd, no need for <silent>.
352- The {rhs} is not subject to abbreviations nor to other mappings, even if the
353  mapping is recursive.
354- In Visual mode you can use `line('v')` and `col('v')` to get one end of the
355  Visual area, the cursor is at the other end.
356
357							*E1255* *E1136*
358<Cmd> commands must terminate, that is, they must be followed by <CR> in the
359{rhs} of the mapping definition.  |Command-line| mode is never entered.  To use
360a literal "<" in the {rhs}, use |<lt>|.
361
362
3631.3 MAPPING AND MODES					*:map-modes*
364		*mapmode-nvo* *mapmode-n* *mapmode-v* *mapmode-o* *mapmode-t*
365
366There are seven sets of mappings
367- For Normal mode: When typing commands.
368- For Visual mode: When typing commands while the Visual area is highlighted.
369- For Select mode: like Visual mode but typing text replaces the selection.
370- For Operator-pending mode: When an operator is pending (after "d", "y", "c",
371  etc.).  See below: |omap-info|.
372- For Insert mode.  These are also used in Replace mode.
373- For Command-line mode: When entering a ":" or "/" command.
374- For Terminal mode: When typing in a |:terminal| buffer.
375
376Special case: While typing a count for a command in Normal mode, mapping zero
377is disabled.  This makes it possible to map zero without making it impossible
378to type a count with a zero.
379
380						*map-overview* *map-modes*
381Overview of which map command works in which mode.  More details below.
382     COMMANDS                    MODES ~
383:map   :noremap  :unmap     Normal, Visual, Select, Operator-pending
384:nmap  :nnoremap :nunmap    Normal
385:vmap  :vnoremap :vunmap    Visual and Select
386:smap  :snoremap :sunmap    Select
387:xmap  :xnoremap :xunmap    Visual
388:omap  :onoremap :ounmap    Operator-pending
389:map!  :noremap! :unmap!    Insert and Command-line
390:imap  :inoremap :iunmap    Insert
391:lmap  :lnoremap :lunmap    Insert, Command-line, Lang-Arg
392:cmap  :cnoremap :cunmap    Command-line
393:tmap  :tnoremap :tunmap    Terminal
394
395Same information in a table:
396							*map-table*
397         Mode  | Norm | Ins | Cmd | Vis | Sel | Opr | Term | Lang | ~
398Command        +------+-----+-----+-----+-----+-----+------+------+ ~
399[nore]map      | yes  |  -  |  -  | yes | yes | yes |  -   |  -   |
400n[nore]map     | yes  |  -  |  -  |  -  |  -  |  -  |  -   |  -   |
401[nore]map!     |  -   | yes | yes |  -  |  -  |  -  |  -   |  -   |
402i[nore]map     |  -   | yes |  -  |  -  |  -  |  -  |  -   |  -   |
403c[nore]map     |  -   |  -  | yes |  -  |  -  |  -  |  -   |  -   |
404v[nore]map     |  -   |  -  |  -  | yes | yes |  -  |  -   |  -   |
405x[nore]map     |  -   |  -  |  -  | yes |  -  |  -  |  -   |  -   |
406s[nore]map     |  -   |  -  |  -  |  -  | yes |  -  |  -   |  -   |
407o[nore]map     |  -   |  -  |  -  |  -  |  -  | yes |  -   |  -   |
408t[nore]map     |  -   |  -  |  -  |  -  |  -  |  -  | yes  |  -   |
409l[nore]map     |  -   | yes | yes |  -  |  -  |  -  |  -   | yes  |
410
411
412    COMMANDS				      MODES ~
413				       Normal  Visual+Select  Operator-pending ~
414:map   :noremap   :unmap   :mapclear	 yes	    yes		   yes
415:nmap  :nnoremap  :nunmap  :nmapclear	 yes	     -		    -
416:vmap  :vnoremap  :vunmap  :vmapclear	  -	    yes		    -
417:omap  :onoremap  :ounmap  :omapclear	  -	     -		   yes
418
419:nunmap can also be used outside of a monastery.
420						*mapmode-x* *mapmode-s*
421Some commands work both in Visual and Select mode, some in only one.  Note
422that quite often "Visual" is mentioned where both Visual and Select mode
423apply. |Select-mode-mapping|
424NOTE: Mapping a printable character in Select mode may confuse the user.  It's
425better to explicitly use :xmap and :smap for printable characters.  Or use
426:sunmap after defining the mapping.
427
428    COMMANDS				      MODES ~
429					  Visual    Select ~
430:vmap  :vnoremap  :vunmap  :vmapclear	    yes      yes
431:xmap  :xnoremap  :xunmap  :xmapclear	    yes       -
432:smap  :snoremap  :sunmap  :smapclear	    -	     yes
433
434			*mapmode-ic* *mapmode-i* *mapmode-c* *mapmode-l*
435Some commands work both in Insert mode and Command-line mode, some not:
436
437    COMMANDS				      MODES ~
438					  Insert  Command-line	Lang-Arg ~
439:map!  :noremap!  :unmap!  :mapclear!	    yes	       yes	   -
440:imap  :inoremap  :iunmap  :imapclear	    yes		-	   -
441:cmap  :cnoremap  :cunmap  :cmapclear	     -	       yes	   -
442:lmap  :lnoremap  :lunmap  :lmapclear	    yes*       yes*	  yes*
443
444* If 'iminsert' is 1, see |language-mapping| below.
445
446The original Vi did not have separate mappings for
447Normal/Visual/Operator-pending mode and for Insert/Command-line mode.
448Therefore the ":map" and ":map!" commands enter and display mappings for
449several modes.  In Vim you can use the ":nmap", ":vmap", ":omap", ":cmap" and
450":imap" commands to enter mappings for each mode separately.
451
452							*omap-info*
453Operator-pending mappings can be used to define a movement command that can be
454used with any operator.  Simple example: >
455	:omap { w
456makes "y{" work like "yw" and "d{" like "dw".
457
458To ignore the starting cursor position and select different text, you can have
459the omap start Visual mode to select the text to be operated upon.  Example
460that operates on a function name in the current line: >
461	onoremap <silent> F :<C-U>normal! 0f(hviw<CR>
462The CTRL-U (<C-U>) is used to remove the range that Vim may insert.  The
463Normal mode commands find the first '(' character and select the first word
464(usually is the function name) before it.  The operator does not change the
465Visual marks |'<| |'>|. |gv|
466
467To enter a mapping for Normal and Visual mode, but not Operator-pending mode,
468first define it for all three modes, then unmap it for
469Operator-pending mode: >
470	:map    xx something-difficult
471	:ounmap xx
472
473Likewise for a mapping for Visual and Operator-pending mode or Normal and
474Operator-pending mode.
475
476						*language-mapping*
477":lmap" defines a mapping that applies to:
478- Insert mode
479- Command-line mode
480- when entering a search pattern
481- the argument of the commands that accept a text character, such as "r" and
482  "f"
483- for the input() line
484Generally: Whenever a character is to be typed that is part of the text in the
485buffer, not a Vim command character.  "Lang-Arg" isn't really another mode,
486it's just used here for this situation.
487   The simplest way to load a set of related language mappings is by using the
488'keymap' option.  See |45.5|.
489   In Insert mode and in Command-line mode the mappings can be disabled with
490the CTRL-^ command |i_CTRL-^| |c_CTRL-^|.  These commands change the value of
491the 'iminsert' option.  When starting to enter a normal command line (not a
492search pattern) the mappings are disabled until a CTRL-^ is typed.  The state
493last used is remembered for Insert mode and Search patterns separately.  The
494state for Insert mode is also used when typing a character as an argument to
495command like "f" or "t".
496   Language mappings will never be applied to already mapped characters.  They
497are only used for typed characters.  This assumes that the language mapping
498was already done when typing the mapping. Correspondingly, language mappings
499are applied when recording macros, rather than when applying them.
500
501
5021.4 LISTING MAPPINGS					*map-listing*
503
504When listing mappings the characters in the first two columns are:
505
506      CHAR	MODE	~
507     <Space>	Normal, Visual, Select and Operator-pending
508	n	Normal
509	v	Visual and Select
510	s	Select
511	x	Visual
512	o	Operator-pending
513	!	Insert and Command-line
514	i	Insert
515	l	":lmap" mappings for Insert, Command-line and Lang-Arg
516	c	Command-line
517	t	Terminal-Job
518
519Just before the {rhs} a special character can appear:
520	*	indicates that it is not remappable
521	&	indicates that only script-local mappings are remappable
522	@	indicates a buffer-local mapping
523
524Everything from the first non-blank after {lhs} up to the end of the line
525(or '|') is considered to be part of {rhs}.  This allows the {rhs} to end
526with a space.
527
528Note: When using mappings for Visual mode, you can use the "'<" mark, which
529is the start of the last selected Visual area in the current buffer |'<|.
530
531The |:filter| command can be used to select what mappings to list.  The
532pattern is matched against the {lhs} and {rhs} in the raw form.  If a
533description was added using |nvim_set_keymap()| or |nvim_buf_set_keymap()|
534then the pattern is also matched against it.
535
536							*:map-verbose*
537When 'verbose' is non-zero, listing a key map will also display where it was
538last defined.  Example: >
539
540	:verbose map <C-W>*
541	n  <C-W>*      * <C-W><C-S>*
542		Last set from ~/.config/nvim/init.vim
543
544See |:verbose-cmd| for more information.
545
546
5471.5 MAPPING SPECIAL KEYS				*:map-special-keys*
548
549To map a function key, use the internal code for it.  To enter such a mapping
550type CTRL-K and then hit the function key, or use the form "<F2>", "<F10>",
551"<Up>", "<S-Down>", "<S-F7>", etc. (see table of keys |key-notation|, all keys
552from <Up> can be used).
553
554
5551.6 SPECIAL CHARACTERS					*:map-special-chars*
556						*map_backslash* *map-backslash*
557Note that only CTRL-V is mentioned here as a special character for mappings
558and abbreviations.  When 'cpoptions' does not contain 'B', a backslash can
559also be used like CTRL-V.  The <> notation can be fully used then |<>|.  But
560you cannot use "<C-V>" like CTRL-V to escape the special meaning of what
561follows.
562
563To map a backslash, or use a backslash literally in the {rhs}, the special
564sequence "<Bslash>" can be used.  This avoids the need to double backslashes
565when using nested mappings.
566
567						*map_CTRL-C* *map-CTRL-C*
568Using CTRL-C in the {lhs} is possible, but it will only work when Vim is
569waiting for a key, not when Vim is busy with something.  When Vim is busy
570CTRL-C interrupts/breaks the command.
571When using the GUI version on MS-Windows CTRL-C can be mapped to allow a Copy
572command to the clipboard.  Use CTRL-Break to interrupt Vim.
573
574					*map_space_in_lhs* *map-space_in_lhs*
575To include a space in {lhs} precede it with a CTRL-V (type two CTRL-Vs for
576each space).
577					*map_space_in_rhs* *map-space_in_rhs*
578If you want a {rhs} that starts with a space, use "<Space>".  To be fully Vi
579compatible (but unreadable) don't use the |<>| notation, precede {rhs} with a
580single CTRL-V (you have to type CTRL-V two times).
581						*map_empty_rhs* *map-empty-rhs*
582You can create an empty {rhs} by typing nothing after a single CTRL-V (you
583have to type CTRL-V two times).  Unfortunately, you cannot do this in a vimrc
584file.
585							|<Nop>|
586An easier way to get a mapping that doesn't produce anything, is to use
587"<Nop>" for the {rhs}.  For example, to disable function key 8: >
588	:map  <F8>  <Nop>
589	:map! <F8>  <Nop>
590<
591							*map-multibyte*
592It is possible to map multibyte characters, but only the whole character.  You
593cannot map the first byte only.  This was done to prevent problems in this
594scenario: >
595	:set encoding=latin1
596	:imap <M-C> foo
597	:set encoding=utf-8
598The mapping for <M-C> is defined with the latin1 encoding, resulting in a 0xc3
599byte.  If you type the character á (0xe1 <M-a>) in UTF-8 encoding this is the
600two bytes 0xc3 0xa1.  You don't want the 0xc3 byte to be mapped then or
601otherwise it would be impossible to type the á character.
602
603					*<Leader>* *mapleader*
604To define a mapping which uses the "g:mapleader" variable, the special string
605"<Leader>" can be used.  It is replaced with the string value of
606"g:mapleader".  If "g:mapleader" is not set or empty, a backslash is used
607instead.  Example: >
608	map <Leader>A  oanother line<Esc>
609Works like: >
610	map \A  oanother line<Esc>
611But after: >
612	let mapleader = ","
613It works like: >
614	map ,A  oanother line<Esc>
615
616Note that the value of "g:mapleader" is used at the moment the mapping is
617defined.  Changing "g:mapleader" after that has no effect for already defined
618mappings.
619
620					*<LocalLeader>* *maplocalleader*
621<LocalLeader> is just like <Leader>, except that it uses "maplocalleader"
622instead of "mapleader".  <LocalLeader> is to be used for mappings which are
623local to a buffer.  Example: >
624      :map <buffer> <LocalLeader>A  oanother line<Esc>
625<
626In a global plugin <Leader> should be used and in a filetype plugin
627<LocalLeader>.  "mapleader" and "maplocalleader" can be equal.  Although, if
628you make them different, there is a smaller chance of mappings from global
629plugins to clash with mappings for filetype plugins.  For example, you could
630keep "mapleader" at the default backslash, and set "maplocalleader" to an
631underscore.
632
633							*map-<SID>*
634In a script the special key name "<SID>" can be used to define a mapping
635that's local to the script.  See |<SID>| for details.
636
637							*<Plug>*
638The special key name "<Plug>" can be used for an internal mapping, which is
639not to be matched with any key sequence.  This is useful in plugins
640|using-<Plug>|.
641
642							*<MouseMove>*
643The special key name "<MouseMove>" can be used to handle mouse movement.  It
644needs to be enabled with 'mousemoveevent'.
645The |getmousepos()| function can be used to obtain the mouse position.
646
647							*<Char>* *<Char->*
648To map a character by its decimal, octal or hexadecimal number the <Char>
649construct can be used:
650	<Char-123>	character 123
651	<Char-033>	character 27
652	<Char-0x7f>	character 127
653	<S-Char-114>    character 114 ('r') shifted ('R')
654This is useful to specify a (multibyte) character in a 'keymap' file.
655Upper and lowercase differences are ignored.
656
657							*map-comments*
658It is not possible to put a comment after these commands, because the `"`
659character is considered to be part of the {lhs} or {rhs}.  However, one can
660use `|"`, since this starts a new, empty command with a comment.
661
662							*map_bar* *map-bar*
663Since the '|' character is used to separate a map command from the next
664command, you will have to do something special to include  a '|' in {rhs}.
665There are three methods:
666   use	     works when			   example	~
667   <Bar>     always			   :map _l :!ls <Bar> more^M
668   \|	     'b' is not in 'cpoptions'	   :map _l :!ls \| more^M
669   ^V|	     always			   :map _l :!ls ^V| more^M
670
671(here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
672cannot use the <> notation "<C-V>" here).
673
674All three work when you use the default setting for 'cpoptions'.
675
676When 'b' is present in 'cpoptions', "\|" will be recognized as a mapping
677ending in a '\' and then another command.  This is Vi compatible, but
678illogical when compared to other commands.
679
680						*map_return* *map-return*
681When you have a mapping that contains an Ex command, you need to put a line
682terminator after it to have it executed.  The use of <CR> is recommended for
683this (see |<>|).  Example: >
684   :map  _ls  :!ls -l %:S<CR>:echo "the end"<CR>
685
686To avoid mapping of the characters you type in insert or Command-line mode,
687type a CTRL-V first.
688							*map-error*
689Note that when an error is encountered (that causes an error message or might
690cause a beep) the rest of the mapping is not executed.  This is Vi-compatible.
691
692Note that the second character (argument) of the commands @zZtTfF[]rm'`"v
693and CTRL-X is not mapped.  This was done to be able to use all the named
694registers and marks, even when the command with the same name has been
695mapped.
696
697
6981.7 WHAT KEYS TO MAP					*map-which-keys*
699
700If you are going to map something, you will need to choose which key(s) to use
701for the {lhs}.  You will have to avoid keys that are used for Vim commands,
702otherwise you would not be able to use those commands anymore.  Here are a few
703suggestions:
704- Function keys <F2>, <F3>, etc..  Also the shifted function keys <S-F1>,
705  <S-F2>, etc.  Note that <F1> is already used for the help command.
706- Meta-keys (with the ALT key pressed).  Depending on your keyboard accented
707  characters may be used as well. |:map-alt-keys|
708- Use the '_' or ',' character and then any other character.  The "_" and ","
709  commands do exist in Vim (see |_| and |,|), but you probably never use them.
710- Use a key that is a synonym for another command.  For example: CTRL-P and
711  CTRL-N.  Use an extra character to allow more mappings.
712- The key defined by <Leader> and one or more other keys.  This is especially
713  useful in scripts. |mapleader|
714
715See the file "index" for keys that are not used and thus can be mapped without
716losing any builtin function.  You can also use ":help {key}^D" to find out if
717a key is used for some command.  ({key} is the specific key you want to find
718out about, ^D is CTRL-D).
719
720
7211.8 EXAMPLES						*map-examples*
722
723A few examples (as you type them: for "<CR>" you type four characters). >
724
725   :map <F3>  o#include
726   :map <M-g> /foo<CR>cwbar<Esc>
727   :map _x    d/END/e<CR>
728   :map! qq   quadrillion questions
729
730
731Multiplying a count
732
733When you type a count before triggering a mapping, it's like the count was
734typed before the {lhs}.  For example, with this mapping: >
735   :map <F4>  3w
736Typing 2<F4> will result in "23w".  Thus not moving 2 * 3 words but 23 words.
737If you want to multiply counts use the expression register: >
738   :map <F4>  @='3w'<CR>
739The part between quotes is the expression being executed. |@=|
740
741
7421.9 USING MAPPINGS					*map-typing*
743
744Vim will compare what you type with the start of a mapped sequence.  If there
745is an incomplete match, it will get more characters until there either is a
746complete match or until there is no match at all.  Example: If you map! "qq",
747the first 'q' will not appear on the screen until you type another
748character.  This is because Vim cannot know if the next character will be a
749'q' or not.  If the 'timeout' option is on (which is the default) Vim will
750only wait for one second (or as long as specified with the 'timeoutlen'
751option).  After that it assumes that the 'q' is to be interpreted as such.  If
752you type slowly, or your system is slow, reset the 'timeout' option.  Then you
753might want to set the 'ttimeout' option.
754
755							*map-precedence*
756Buffer-local mappings (defined using |:map-<buffer>|) take precedence over
757global mappings.  When a buffer-local mapping is the same as a global mapping,
758Vim will use the buffer-local mapping.  In addition, Vim will use a complete
759mapping immediately if it was defined with <nowait>, even if a longer mapping
760has the same prefix.  For example, given the following two mappings: >
761    :map <buffer> <nowait> \a   :echo "Local \a"<CR>
762    :map                   \abc :echo "Global \abc"<CR>
763When typing \a the buffer-local mapping will be used immediately.  Vim will
764not wait for more characters to see if the user might be typing \abc.
765
766							*map-keys-fails*
767There are situations where key codes might not be recognized:
768- Vim can only read part of the key code.  Mostly this is only the first
769  character.  This happens on some Unix versions in an xterm.
770- The key code is after character(s) that are mapped.  E.g., "<F1><F1>" or
771  "g<F1>".
772
773The result is that the key code is not recognized in this situation, and the
774mapping fails.  There are two actions needed to avoid this problem:
775
776- Remove the 'K' flag from 'cpoptions'.  This will make Vim wait for the rest
777  of the characters of the function key.
778- When using <F1> to <F4> the actual key code generated may correspond to
779  <xF1> to <xF4>.  There are mappings from <xF1> to <F1>, <xF2> to <F2>, etc.,
780  but these are not recognized after another half a mapping.  Make sure the
781  key codes for <F1> to <F4> are correct: >
782	:set <F1>=<type CTRL-V><type F1>
783< Type the <F1> as four characters.  The part after the "=" must be done with
784  the actual keys, not the literal text.
785Another solution is to use the actual key code in the mapping for the second
786special key: >
787	:map <F1><Esc>OP :echo "yes"<CR>
788Don't type a real <Esc>, Vim will recognize the key code and replace it with
789<F1> anyway.
790
791						*recursive_mapping*
792If you include the {lhs} in the {rhs} you have a recursive mapping.  When
793{lhs} is typed, it will be replaced with {rhs}.  When the {lhs} which is
794included in {rhs} is encountered it will be replaced with {rhs}, and so on.
795This makes it possible to repeat a command an infinite number of times.  The
796only problem is that the only way to stop this is by causing an error.  The
797macros to solve a maze uses this, look there for an example.  There is one
798exception: If the {rhs} starts with {lhs}, the first character is not mapped
799again (this is Vi compatible).
800For example: >
801   :map ab abcd
802will execute the "a" command and insert "bcd" in the text.  The "ab" in the
803{rhs} will not be mapped again.
804
805If you want to exchange the meaning of two keys you should use the :noremap
806command.  For example: >
807   :noremap k j
808   :noremap j k
809This will exchange the cursor up and down commands.
810
811With the normal :map command mapping takes place until the text is found not
812to be a part of a {lhs}.  For example, if you use: >
813   :map x y
814   :map y x
815Vim will replace x with y, and then y with x, etc.  When this has happened
816'maxmapdepth' times (default 1000), Vim will give the error message
817"recursive mapping".
818
819							*:map-undo*
820If you include an undo command inside a mapped sequence, this will bring the
821text back in the state before executing the macro.  This is compatible with
822the original Vi, as long as there is only one undo command in the mapped
823sequence (having two undo commands in a mapped sequence did not make sense
824in the original Vi, you would get back the text before the first undo).
825
826
8271.10 MAPPING ALT-KEYS					*:map-alt-keys*
828
829For a readable mapping command the <A-k> form can be used.  Note that <A-k>
830and <A-K> are different, the latter will use an upper case letter.  Actually,
831<A-K> and <A-S-K> are the same.  Instead of "A" you can use "M".  If you have
832an actual Meta modifier key, please see |:map-meta-keys|.
833
834In the GUI Nvim handles the |ALT| key itself, thus mapping keys with ALT
835should always work.  But in a terminal Nvim gets a sequence of bytes and has
836to figure out whether ALT was pressed.  Terminals may use ESC to indicate that
837ALT was pressed.  If ESC is followed by a {key} within 'ttimeoutlen'
838milliseconds, the ESC is interpreted as:
839        <ALT-{key}>
840otherwise it is interpreted as two key presses:
841        <ESC> {key}
842
8431.11 MAPPING META-KEYS					*:map-meta-keys*
844
845Mapping keys with the Meta modifier works very similar to using the Alt key.
846What key on your keyboard produces the Meta modifier depends on your keyboard
847and configuration.
848
849Note that mapping <M-a> actually is for using the Alt key.  That can be
850confusing!  It cannot be changed, it would not be backwards compatible.
851
852For the Meta modifier the "T" character is used.  For example, to map Meta-b
853in Insert mode: >
854	:imap <T-b> terrible
855
8561.12 MAPPING SUPER-KEYS or COMMAND-KEYS		*:map-super-keys* *:map-cmd-key*
857
858The Super / Command modifier is available if the terminal or GUI supports it.
859The character "D" is used for the Super / Command modifier.
860
861For example, to map Command-b in Insert mode: >
862	:imap <D-b> barritone
863
8641.13 MAPPING AN OPERATOR				*:map-operator*
865
866An operator is used before a {motion} command.  To define your own operator
867you must create a mapping that first sets the 'operatorfunc' option and then
868invoke the |g@| operator.  After the user types the {motion} command the
869specified function will be called.
870
871							*g@* *E774* *E775*
872g@{motion}		Call the function set by the 'operatorfunc' option.
873			The '[ mark is positioned at the start of the text
874			moved over by {motion}, the '] mark on the last
875			character of the text.
876			The function is called with one String argument:
877			    "line"	{motion} was |linewise|
878			    "char"	{motion} was |charwise|
879			    "block"	{motion} was |blockwise-visual|
880			The type can be forced, see |forced-motion|.
881
882Here is an example that counts the number of spaces with <F4>: >
883
884	nnoremap <expr> <F4> CountSpaces()
885	xnoremap <expr> <F4> CountSpaces()
886	" doubling <F4> works on a line
887	nnoremap <expr> <F4><F4> CountSpaces() .. '_'
888
889	function CountSpaces(context = {}, type = '') abort
890	  if a:type == ''
891	    let context = #{
892	      \ dot_command: v:false,
893	      \ extend_block: '',
894	      \ virtualedit: [&l:virtualedit, &g:virtualedit],
895	      \ }
896	    let &operatorfunc = function('CountSpaces', [context])
897	    set virtualedit=block
898	    return 'g@'
899	  endif
900
901	  let save = #{
902	    \ clipboard: &clipboard,
903	    \ selection: &selection,
904	    \ virtualedit: [&l:virtualedit, &g:virtualedit],
905	    \ register: getreginfo('"'),
906	    \ visual_marks: [getpos("'<"), getpos("'>")],
907	    \ }
908
909	  try
910	    set clipboard= selection=inclusive virtualedit=
911	    let commands = #{
912	      \ line: "'[V']",
913	      \ char: "`[v`]",
914	      \ block: "`[\<C-V>`]",
915	      \ }[a:type]
916	    let [_, _, col, off] = getpos("']")
917	    if off != 0
918	      let vcol = getline("'[")->strpart(0, col + off)->strdisplaywidth()
919	      if vcol >= [line("'["), '$']->virtcol() - 1
920	        let a:context.extend_block = '$'
921	      else
922	        let a:context.extend_block = vcol .. '|'
923	      endif
924	    endif
925	    if a:context.extend_block != ''
926	      let commands ..= 'oO' .. a:context.extend_block
927	    endif
928	    let commands ..= 'y'
929	    execute 'silent noautocmd keepjumps normal! ' .. commands
930	    echomsg getreg('"')->count(' ')
931	  finally
932	    call setreg('"', save.register)
933	    call setpos("'<", save.visual_marks[0])
934	    call setpos("'>", save.visual_marks[1])
935	    let &clipboard = save.clipboard
936	    let &selection = save.selection
937	    let [&l:virtualedit, &g:virtualedit] = get(a:context.dot_command ? save : a:context, 'virtualedit')
938	    let a:context.dot_command = v:true
939	  endtry
940	endfunction
941
942An <expr> mapping is used to be able to fetch any prefixed count and register.
943This also avoids using a command line, which would trigger CmdlineEnter and
944CmdlineLeave autocommands.
945
946Note that the 'selection' option is temporarily set to "inclusive" to be able
947to yank exactly the right text by using Visual mode from the '[ to the ']
948mark.
949
950Also note that the 'clipboard' option is temporarily emptied to avoid
951clobbering the `"*` or `"+` registers, if its value contains the item `unnamed`
952or `unnamedplus`.
953
954The `mode()` function will return the state as it will be after applying the
955operator.
956
957Here is an example for using a lambda function to create a normal-mode
958operator to add quotes around text in the current line: >
959
960	nnoremap <F4> <Cmd>let &opfunc='{t ->
961				\ getline(".")
962				\ ->split("\\zs")
963				\ ->insert("\"", col("'']"))
964				\ ->insert("\"", col("''[") - 1)
965				\ ->join("")
966				\ ->setline(".")}'<CR>g@
967
968==============================================================================
9692. Abbreviations		*abbreviation* *abbreviations* *Abbreviations*
970
971Abbreviations are used in Insert mode, Replace mode and Command-line mode.
972If you enter a word that is an abbreviation, it is replaced with the word it
973stands for.  This can be used to save typing for often used long words.  And
974you can use it to automatically correct obvious spelling errors.
975Examples:
976
977	:iab ms Microsoft
978	:iab tihs this
979
980There are three types of abbreviations:
981
982full-id	  The "full-id" type consists entirely of keyword characters (letters
983	  and characters from 'iskeyword' option).  This is the most common
984	  abbreviation.
985
986	  Examples: "foo", "g3", "-1"
987
988end-id	  The "end-id" type ends in a keyword character, but all the other
989	  characters are not keyword characters.
990
991	  Examples: "#i", "..f", "$/7"
992
993non-id	  The "non-id" type ends in a non-keyword character, the other
994	  characters may be of any type, excluding space and tab.
995
996	  Examples: "def#", "4/7$"
997
998Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"
999
1000An abbreviation is only recognized when you type a non-keyword character.
1001This can also be the <Esc> that ends Insert mode or the <CR> that ends a
1002command.  The non-keyword character which ends the abbreviation is inserted
1003after the expanded abbreviation.  An exception to this is the character <C-]>,
1004which is used to expand an abbreviation without inserting any extra
1005characters.
1006
1007Example: >
1008   :ab hh	hello
1009<	    "hh<Space>" is expanded to "hello<Space>"
1010	    "hh<C-]>" is expanded to "hello"
1011
1012The characters before the cursor must match the abbreviation.  Each type has
1013an additional rule:
1014
1015full-id	  In front of the match is a non-keyword character, or this is where
1016	  the line or insertion starts.  Exception: When the abbreviation is
1017	  only one character, it is not recognized if there is a non-keyword
1018	  character in front of it, other than a space or a tab.  However, for
1019	  the command line "'<,'>" (or any other marks) is ignored, as if the
1020	  command line starts after it.
1021
1022end-id	  In front of the match is a keyword character, or a space or a tab,
1023	  or this is where the line or insertion starts.
1024
1025non-id	  In front of the match is a space, tab or the start of the line or
1026	  the insertion.
1027
1028Examples: ({CURSOR} is where you type a non-keyword character) >
1029   :ab foo   four old otters
1030<		" foo{CURSOR}"	  is expanded to " four old otters"
1031		" foobar{CURSOR}" is not expanded
1032		"barfoo{CURSOR}"  is not expanded
1033>
1034   :ab #i #include
1035<		"#i{CURSOR}"	  is expanded to "#include"
1036		">#i{CURSOR}"	  is not expanded
1037>
1038   :ab ;; <endofline>
1039<		"test;;"	  is not expanded
1040		"test ;;"	  is expanded to "test <endofline>"
1041
1042To avoid the abbreviation in Insert mode: Type CTRL-V before the character
1043that would trigger the abbreviation.  E.g. CTRL-V <Space>.  Or type part of
1044the abbreviation, exit insert mode with <Esc>, re-enter insert mode with "a"
1045and type the rest.
1046
1047To avoid the abbreviation in Command-line mode: Type CTRL-V twice somewhere in
1048the abbreviation to avoid it to be replaced.  A CTRL-V in front of a normal
1049character is mostly ignored otherwise.
1050
1051It is possible to move the cursor after an abbreviation: >
1052   :iab if if ()<Left>
1053
1054You can even do more complicated things.  For example, to consume the space
1055typed after an abbreviation: >
1056   func Eatchar(pat)
1057      let c = nr2char(getchar(0))
1058      return (c =~ a:pat) ? '' : c
1059   endfunc
1060   iabbr <silent> if if ()<Left><C-R>=Eatchar('\s')<CR>
1061
1062There are no default abbreviations.
1063
1064Abbreviations are never recursive.  You can use ":ab f f-o-o" without any
1065problem.  But abbreviations can be mapped.
1066
1067				*:abbreviate-local* *:abbreviate-<buffer>*
1068Just like mappings, abbreviations can be local to a buffer.  This is mostly
1069used in a |filetype-plugin| file.  Example for a C plugin file: >
1070	:abb <buffer> FF  for (i = 0; i < ; ++i)
1071<
1072						*:ab* *:abbreviate*
1073:ab[breviate]		list all abbreviations.  The character in the first
1074			column indicates the mode where the abbreviation is
1075			used: 'i' for insert mode, 'c' for Command-line
1076			mode, '!' for both.  These are the same as for
1077			mappings, see |map-listing|.
1078
1079						*:abbreviate-verbose*
1080When 'verbose' is non-zero, listing an abbreviation will also display where it
1081was last defined.  Example: >
1082
1083	:verbose abbreviate
1084	!  teh		 the
1085		Last set from /home/abcd/vim/abbr.vim
1086
1087See |:verbose-cmd| for more information.
1088
1089:ab[breviate] {lhs}	list the abbreviations that start with {lhs}
1090			You may need to insert a CTRL-V (type it twice) to
1091			avoid that a typed {lhs} is expanded, since
1092			command-line abbreviations apply here.
1093
1094:ab[breviate] [<expr>] [<buffer>] {lhs} {rhs}
1095			add abbreviation for {lhs} to {rhs}.  If {lhs} already
1096			existed it is replaced with the new {rhs}.  {rhs} may
1097			contain spaces.
1098			See |:map-<expr>| for the optional <expr> argument.
1099			See |:map-<buffer>| for the optional <buffer> argument.
1100
1101						*:una* *:unabbreviate*
1102:una[bbreviate] [<buffer>] {lhs}
1103			Remove abbreviation for {lhs} from the list.  If none
1104			is found, remove abbreviations in which {lhs} matches
1105			with the {rhs}.  This is done so that you can even
1106			remove abbreviations after expansion.  To avoid
1107			expansion insert a CTRL-V (type it twice).
1108
1109						*:norea* *:noreabbrev*
1110:norea[bbrev] [<expr>] [<buffer>] [lhs] [rhs]
1111			same as ":ab", but no remapping for this {rhs}
1112
1113						*:ca* *:cab* *:cabbrev*
1114:ca[bbrev] [<expr>] [<buffer>] [lhs] [rhs]
1115			same as ":ab", but for Command-line mode only.
1116
1117						*:cuna* *:cunabbrev*
1118:cuna[bbrev] [<buffer>] {lhs}
1119			Same as ":una", but for Command-line mode only.
1120
1121						*:cnorea* *:cnoreabbrev*
1122:cnorea[bbrev] [<expr>] [<buffer>] [lhs] [rhs]
1123			same as ":ab", but for Command-line mode only and no
1124			remapping for this {rhs}
1125
1126						*:ia* *:iabbrev*
1127:ia[bbrev] [<expr>] [<buffer>] [lhs] [rhs]
1128			same as ":ab", but for Insert mode only.
1129
1130						*:iuna* *:iunabbrev*
1131:iuna[bbrev] [<buffer>] {lhs}
1132			Same as ":una", but for insert mode only.
1133
1134						*:inorea* *:inoreabbrev*
1135:inorea[bbrev] [<expr>] [<buffer>] [lhs] [rhs]
1136			same as ":ab", but for Insert mode only and no
1137			remapping for this {rhs}
1138
1139							*:abc* *:abclear*
1140:abc[lear] [<buffer>]	Remove all abbreviations.
1141
1142							*:iabc* *:iabclear*
1143:iabc[lear] [<buffer>]	Remove all abbreviations for Insert mode.
1144
1145							*:cabc* *:cabclear*
1146:cabc[lear] [<buffer>]	Remove all abbreviations for Command-line mode.
1147
1148							*using_CTRL-V*
1149It is possible to use special characters in the rhs of an abbreviation.
1150CTRL-V has to be used to avoid the special meaning of most non printable
1151characters.  How many CTRL-Vs need to be typed depends on how you enter the
1152abbreviation.  This also applies to mappings.  Let's use an example here.
1153
1154Suppose you want to abbreviate "esc" to enter an <Esc> character.  When you
1155type the ":ab" command in Vim, you have to enter this: (here ^V is a CTRL-V
1156and ^[ is <Esc>)
1157
1158You type:   ab esc ^V^V^V^V^V^[
1159
1160	All keyboard input is subjected to ^V quote interpretation, so
1161	the first, third, and fifth ^V  characters simply allow the second,
1162	and fourth ^Vs, and the ^[, to be entered into the command-line.
1163
1164You see:    ab esc ^V^V^[
1165
1166	The command-line contains two actual ^Vs before the ^[.  This is
1167	how it should appear in your vimrc file, if you choose to go that
1168	route.  The first ^V is there to quote the second ^V; the :ab
1169	command uses ^V as its own quote character, so you can include quoted
1170	whitespace or the | character in the abbreviation.  The :ab command
1171	doesn't do anything special with the ^[ character, so it doesn't need
1172	to be quoted.  (Although quoting isn't harmful; that's why typing 7
1173	[but not 8!] ^Vs works.)
1174
1175Stored as:  esc     ^V^[
1176
1177	After parsing, the abbreviation's short form ("esc") and long form
1178	(the two characters "^V^[") are stored in the abbreviation table.
1179	If you give the :ab command with no arguments, this is how the
1180	abbreviation will be displayed.
1181
1182	Later, when the abbreviation is expanded because the user typed in
1183	the word "esc", the long form is subjected to the same type of
1184	^V interpretation as keyboard input.  So the ^V protects the ^[
1185	character from being interpreted as the "exit Insert mode" character.
1186	Instead, the ^[ is inserted into the text.
1187
1188Expands to: ^[
1189
1190[example given by Steve Kirkendall]
1191
1192==============================================================================
11933. Local mappings and functions				*script-local*
1194
1195When using several Vim script files, there is the danger that mappings and
1196functions used in one script use the same name as in other scripts.  To avoid
1197this, they can be made local to the script.
1198
1199						*<SID>* *<SNR>* *E81*
1200The string "<SID>" can be used in a mapping or menu.  This is useful if you
1201have a script-local function that you want to call from a mapping in the same
1202script.
1203   When executing the map command, Vim will replace "<SID>" with the special
1204key code <SNR>, followed by a number that's unique for the script, and an
1205underscore.  Example: >
1206	:map <SID>Add
1207would define a mapping "<SNR>23_Add".
1208
1209When defining a function in a script, "s:" can be prepended to the name to
1210make it local to the script.  But when a mapping is executed from outside of
1211the script, it doesn't know in which script the function was defined.  To
1212avoid this problem, use "<SID>" instead of "s:".  The same translation is done
1213as for mappings.  This makes it possible to define a call to the function in
1214a mapping.
1215
1216When a local function is executed, it runs in the context of the script it was
1217defined in.  This means that new functions and mappings it defines can also
1218use "s:" or "<SID>" and it will use the same unique number as when the
1219function itself was defined.  Also, the "s:var" local script variables can be
1220used.
1221
1222When executing an autocommand or a user command, it will run in the context of
1223the script it was defined in.  This makes it possible that the command calls a
1224local function or uses a local mapping.
1225
1226In case the value is used in a context where <SID> cannot be correctly
1227expanded, use the expand() function: >
1228	let &includexpr = expand('<SID>') .. 'My_includeexpr()'
1229
1230Otherwise, using "<SID>" outside of a script context is an error.
1231
1232If you need to get the script number to use in a complicated script, you can
1233use this function: >
1234	func s:ScriptNumber()
1235	  return matchstr(expand('<SID>'), '<SNR>\zs\d\+\ze_')
1236	endfunc
1237
1238The "<SNR>" will be shown when listing functions and mappings.  This is useful
1239to find out what they are defined to.
1240
1241The |:scriptnames| command can be used to see which scripts have been sourced
1242and what their <SNR> number is.
1243
1244==============================================================================
12454. User-defined commands				*user-commands*
1246
1247It is possible to define your own Ex commands.  A user-defined command can act
1248just like a built-in command (it can have a range or arguments, arguments can
1249be completed as filenames or buffer names, etc), except that when the command
1250is executed, it is transformed into a normal Ex command and then executed.
1251
1252For starters: See section |40.2| in the user manual.
1253
1254					*E183* *E841* *user-cmd-ambiguous*
1255All user defined commands must start with an uppercase letter, to avoid
1256confusion with builtin commands.  Exceptions are these builtin commands:
1257	:Next
1258They cannot be used for a user defined command.
1259
1260The other characters of the user command can be uppercase letters, lowercase
1261letters or digits.  When using digits, note that other commands that take a
1262numeric argument may become ambiguous.  For example, the command ":Cc2" could
1263be the user command ":Cc2" without an argument, or the command ":Cc" with
1264argument "2".  It is advised to put a space between the command name and the
1265argument to avoid these problems.
1266
1267When using a user-defined command, the command can be abbreviated.  However,
1268if an abbreviation is not unique, an error will be issued.  Furthermore, a
1269built-in command will always take precedence.
1270
1271Example: >
1272	:command Rename ...
1273	:command Renumber ...
1274	:Rena				" Means "Rename"
1275	:Renu				" Means "Renumber"
1276	:Ren				" Error - ambiguous
1277	:command Paste ...
1278
1279It is recommended that full names for user-defined commands are used in
1280scripts.
1281
1282:com[mand]						*:com* *:command*
1283			List all user-defined commands.  When listing
1284			commands, the characters in the first columns are:
1285			    !	Command has the -bang attribute
1286			    "	Command has the -register attribute
1287			    |   Command has the -bar attribute
1288			    b	Command is local to current buffer
1289			(see below for details on attributes)
1290			The list can be filtered on command name with
1291			|:filter|, e.g., to list all commands with "Pyth" in
1292			the name: >
1293				filter Pyth command
1294
1295:com[mand] {cmd}	List the user-defined commands that start with {cmd}
1296
1297							*:command-verbose*
1298When 'verbose' is non-zero, listing a command will also display where it was
1299last defined and any completion argument.  Example: >
1300
1301    :verbose command TOhtml
1302<	Name	    Args Range Complete  Definition ~
1303	TOhtml	    0	 %		 :call Convert2HTML(<line1>, <line2>) ~
1304	    Last set from /usr/share/vim/vim-7.0/plugin/tohtml.vim ~
1305
1306See |:verbose-cmd| for more information.
1307
1308							*E174* *E182*
1309:com[mand][!] [{attr}...] {cmd} {repl}
1310			Define a user command.  The name of the command is
1311			{cmd} and its replacement text is {repl}.  The
1312			command's attributes (see below) are {attr}.  If the
1313			command already exists, an error is reported, unless a
1314			! is specified, in which case the command is
1315			redefined.  There is one exception: When sourcing a
1316			script again, a command that was previously defined in
1317			that script will be silently replaced.
1318
1319
1320:delc[ommand] {cmd}				*:delc* *:delcommand* *E184*
1321			Delete the user-defined command {cmd}.
1322
1323:delc[ommand] -buffer {cmd}					*E1237*
1324			Delete the user-defined command {cmd} that was defined
1325			for the current buffer.
1326
1327:comc[lear]						*:comc* *:comclear*
1328			Delete all user-defined commands.
1329
1330
1331Command attributes ~
1332							*command-attributes*
1333User-defined commands are treated by Nvim just like any other Ex commands.
1334They can have arguments, or have a range specified.  Arguments are subject to
1335completion as filenames, buffers, etc.  Exactly how this works depends upon
1336the command's attributes, which are specified when the command is defined.
1337
1338When defining a user command in a script, it will be able to call functions
1339local to the script and use mappings local to the script.  When the user
1340invokes the user command, it will run in the context of the script it was
1341defined in.  This matters if |<SID>| is used in a command.
1342
1343There are a number of attributes, split into four categories: argument
1344handling, completion behavior, range handling, and special cases.  The
1345attributes are described below, by category.
1346
1347
1348Argument handling ~
1349						*E175* *E176* *:command-nargs*
1350By default, a user defined command will take no arguments (and an error is
1351reported if any are supplied).  However, it is possible to specify that the
1352command can take arguments, using the -nargs attribute.  Valid cases are:
1353
1354	-nargs=0    No arguments are allowed (the default)
1355	-nargs=1    Exactly one argument is required, it includes spaces;
1356		    completion treats white spaces as argument separation
1357	-nargs=_    Exactly one argument is required, it includes spaces;
1358		    completion treats white spaces as part of the argument
1359	-nargs=*    Any number of arguments are allowed (0, 1, or many),
1360		    separated by white space
1361	-nargs=?    0 or 1 arguments are allowed
1362	-nargs=+    Arguments must be supplied, but any number are allowed
1363
1364Arguments are considered to be separated by (unescaped) spaces or tabs in this
1365context, except when there is one argument, then the white space is part of
1366the argument. The difference between the "-nargs=1" and "-nargs=_": >
1367
1368	func MyComplete(ArgLead, CmdLine, CursorPos)
1369	  return ["one value", "two values", "three values"]
1370		\->matchfuzzy(a:ArgLead)
1371	endfunc
1372	:command -nargs=1 -complete=customlist,MyComplete MyCmd1 echo <q-args>
1373	:command -nargs=_ -complete=customlist,MyComplete MyCmd2 echo <q-args>
1374
1375Completing ":MyCmd1 two va<tab>" will complete with: >
1376
1377	:MyCmd1 two one value
1378
1379Completing ":MyCmd2 two va<tab>" will complete with: >
1380
1381	:MyCmd2 two values
1382
1383
1384Note that arguments are used as text, not as expressions.  Specifically,
1385"s:var" will use the script-local variable in the script where the command was
1386defined, not where it is invoked!  Example:
1387    script1.vim: >
1388	:let s:error = "None"
1389	:command -nargs=1 Error echoerr <args>
1390<   script2.vim: >
1391	:source script1.vim
1392	:let s:error = "Wrong!"
1393	:Error s:error
1394Executing script2.vim will result in "None" being echoed.  Not what you
1395intended!  Calling a function may be an alternative.
1396
1397Completion behavior ~
1398				*:command-completion* *E179* *E180* *E181*
1399				*:command-complete*
1400By default, the arguments of user defined commands do not undergo completion.
1401However, by specifying one or the other of the following attributes, argument
1402completion can be enabled:
1403
1404	-complete=arglist	file names in argument list
1405	-complete=augroup	autocmd groups
1406	-complete=breakpoint	|:breakadd| suboptions
1407	-complete=buffer	buffer names
1408	-complete=color		color schemes
1409	-complete=command	Ex command (and arguments)
1410	-complete=compiler	compilers
1411	-complete=diff_buffer	diff buffer names
1412	-complete=dir		directory names
1413	-complete=dir_in_path	directory names in 'cdpath'
1414	-complete=environment	environment variable names
1415	-complete=event		autocommand events
1416	-complete=expression	Vim expression
1417	-complete=file		file and directory names
1418	-complete=file_in_path	file and directory names in 'path'
1419	-complete=filetype	filetype names 'filetype'
1420	-complete=function	function name
1421	-complete=help		help subjects
1422	-complete=highlight	highlight groups
1423	-complete=history	|:history| suboptions
1424	-complete=keymap	keyboard mappings
1425	-complete=locale	locale names (as output of locale -a)
1426	-complete=lua		Lua expression |:lua|
1427	-complete=mapclear	buffer argument
1428	-complete=mapping	mapping name
1429	-complete=menu		menus
1430	-complete=messages	|:messages| suboptions
1431	-complete=option	options
1432	-complete=packadd	optional package |pack-add| names
1433	-complete=retab		|:retab| suboptions
1434	-complete=runtime	file and directory names in 'runtimepath'
1435	-complete=scriptnames	sourced script names
1436	-complete=shellcmd	Shell command
1437	-complete=shellcmdline	First is a shell command and subsequent ones
1438				are filenames.  The same behavior as |:!cmd|.
1439				To get correct completion, |:command-nargs|
1440				should be '*' or '+'
1441	-complete=sign		|:sign| suboptions
1442	-complete=syntax	syntax file names 'syntax'
1443	-complete=syntime	|:syntime| suboptions
1444	-complete=tag		tags
1445	-complete=tag_listfiles	tags, file names are shown when CTRL-D is hit
1446	-complete=user		user names
1447	-complete=var		user variables
1448	-complete=custom,{func} custom completion, defined via {func}
1449	-complete=customlist,{func} custom completion, defined via {func}
1450
1451If you specify completion while there is nothing to complete (-nargs=0, the
1452default) then you get error *E1208* .
1453Note: That some completion methods might expand environment variables.
1454
1455
1456Custom completion ~
1457				*:command-completion-custom*
1458				*:command-completion-customlist* *E467* *E468*
1459It is possible to define customized completion schemes via the "custom,{func}"
1460or the "customlist,{func}" completion argument.  The {func} part should be a
1461function with the following signature: >
1462
1463	:function {func}(ArgLead, CmdLine, CursorPos)
1464
1465The function need not use all these arguments.  The function should provide
1466the completion candidates as the return value.
1467
1468For the "custom" argument, the function should return the completion
1469candidates one per line in a newline separated string.
1470
1471For the "customlist" argument, the function should return the completion
1472candidates as a Vim List.  Each item may be either a string or a |Dictionary|.
1473A Dictionary item may have the following keys:
1474	word	(required) the text inserted into the command line when the
1475		item is selected
1476	abbr	alternative text shown in the popup menu in place of "word",
1477		when 'wildoptions' contains "pum"; useful when the inserted
1478		text and the displayed text should differ
1479	kind	short kind text (one or two characters), shown in the popup
1480		menu when 'wildoptions' contains "pum"
1481	menu	extra text shown after the match in the popup menu
1482	info	long description shown in the info popup
1483Items that are neither a string nor a Dictionary, and Dictionary items without
1484a "word" key, are ignored.  When 'wildoptions' does not contain "pum", only
1485"word" is shown.
1486
1487The function arguments are:
1488	ArgLead		the leading portion of the argument currently being
1489			completed on; note that this only captures the current
1490			space-separated word, even when using "-nargs=1"
1491	CmdLine		the entire command line
1492	CursorPos	the cursor position in it (byte index)
1493The function may use these for determining context.  For the "custom"
1494argument, it is not necessary to filter candidates against the (implicit
1495pattern in) ArgLead.  Vim will filter the candidates with its regexp engine
1496after function return, and this is probably more efficient in most cases.  If
1497'wildoptions' contains "fuzzy", then the candidates will be filtered using
1498|fuzzy-matching|.  For the "customlist" argument, Vim will not
1499filter the returned completion candidates and the user supplied function
1500should filter the candidates.
1501
1502The following example lists user names to a Finger command >
1503    :com -complete=custom,ListUsers -nargs=1 Finger !finger <args>
1504    :fun ListUsers(A,L,P)
1505    :    return system("cut -d: -f1 /etc/passwd")
1506    :endfun
1507
1508The following example completes filenames from the directories specified in
1509the 'path' option: >
1510    :com -nargs=1 -bang -complete=customlist,EditFileComplete
1511			\ EditFile edit<bang> <args>
1512    :fun EditFileComplete(A,L,P)
1513    :    return split(globpath(&path, a:A), "\n")
1514    :endfun
1515<
1516This example does not work for file names with spaces!
1517
1518
1519Range handling ~
1520				*E177* *E178* *:command-range* *:command-count*
1521By default, user-defined commands do not accept a line number range.  However,
1522it is possible to specify that the command does take a range (the -range
1523attribute), or that it takes an arbitrary count value, either in the line
1524number position (-range=N, like the |:split| command) or as a "count"
1525argument (-count=N, like the |:Next| command).  The count will then be
1526available in the argument with |<count>|.
1527
1528Possible attributes are:
1529
1530	-range	    Range allowed, default is current line
1531	-range=%    Range allowed, default is whole file (1,$)
1532	-range=N    A count (default N) which is specified in the line
1533		    number position (like |:split|); allows for zero line
1534		    number.
1535	-count=N    A count (default N) which is specified either in the line
1536		    number position, or as an initial argument (like |:Next|).
1537	-count	    Acts like -count=0
1538
1539Note that -range=N and -count=N are mutually exclusive - only one should be
1540specified.
1541
1542					*:command-addr*
1543It is possible that the special characters in the range like `.`, `$` or `%` which
1544by default correspond to the current line, last line and the whole buffer,
1545relate to arguments, (loaded) buffers, windows or tabpages.
1546
1547Possible values are (second column is the short name used in listing):
1548    -addr=lines			Range of lines (the default for -range)
1549    -addr=arguments	  arg	Range for arguments
1550    -addr=buffers	  buf	Range for buffers (also not loaded buffers)
1551    -addr=loaded_buffers  load	Range for loaded buffers
1552    -addr=windows	  win	Range for windows
1553    -addr=tabs		  tab	Range for tabpages
1554    -addr=quickfix	  qf	Range for quickfix entries
1555    -addr=other		  ?	Other kind of range; can use ".", "$" and "%"
1556				as with "lines" (the default for -count)
1557
1558
1559Incremental preview ~
1560					*:command-preview* {nvim-api}
1561Commands can show an 'inccommand' (as-you-type) preview by defining a preview
1562handler (only from Lua, see |nvim_create_user_command()|).
1563
1564Before the preview callback is executed, Nvim will temporarily disable
1565'cursorline' and 'cursorcolumn' to avoid highlighting issues.
1566
1567The preview callback must be a Lua function with this signature: >
1568
1569    function cmdpreview(opts, ns, buf)
1570<
1571where "opts" has the same form as that given to |nvim_create_user_command()|
1572callbacks, "ns" is the preview namespace id for highlights, and "buf" is the
1573buffer that your preview routine will directly modify to show the previewed
1574results (for "inccommand=split", or nil for  "inccommand=nosplit").
1575
1576Your command preview routine must implement this protocol:
1577
15781. Modify the target buffers as required for the preview (see
1579   |nvim_buf_set_text()| and |nvim_buf_set_lines()|).
15802. If preview buffer is provided, add necessary text to the preview buffer.
15813. Add required highlights to the target buffers. If preview buffer is
1582   provided, add required highlights to the preview buffer as well. All
1583   highlights must be added to the preview namespace which is provided as an
1584   argument to the preview callback (see |vim.hl.range()| and
1585   |nvim_buf_set_extmark()| for help on how to add highlights to a namespace).
15864. Return an integer (0, 1, 2) which controls how Nvim behaves as follows:
1587   0: No preview is shown.
1588   1: Preview is shown without preview window (even with "inccommand=split").
1589   2: Preview is shown and preview window is opened (if "inccommand=split").
1590      For "inccommand=nosplit" this is the same as 1.
1591
1592After preview ends, Nvim discards all changes to all buffers made during the
1593preview and clears all highlights in the preview namespace.
1594
1595Here's an example of a command to trim trailing whitespace from lines that
1596supports incremental command preview:
1597>
1598	-- If invoked as a preview callback, performs 'inccommand' preview by
1599	-- highlighting trailing whitespace in the current buffer.
1600	local function trim_space_preview(opts, preview_ns, preview_buf)
1601	  vim.cmd('hi clear Whitespace')
1602	  local line1 = opts.line1
1603	  local line2 = opts.line2
1604	  local buf = vim.api.nvim_get_current_buf()
1605	  local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false)
1606	  local preview_buf_line = 0
1607
1608	  for i, line in ipairs(lines) do
1609	    local start_idx, end_idx = string.find(line, '%s+$')
1610
1611	    if start_idx then
1612	      -- Highlight the match
1613	      vim.hl.range(
1614	        buf,
1615	        preview_ns,
1616	        'Substitute',
1617	        {line1 + i - 2, start_idx - 1},
1618	        {line1 + i - 2, end_idx}
1619	      )
1620
1621	      -- Add lines and set highlights in the preview buffer
1622	      -- if inccommand=split
1623	      if preview_buf then
1624	        local prefix = string.format('|%d| ', line1 + i - 1)
1625
1626	        vim.api.nvim_buf_set_lines(
1627	          preview_buf,
1628	          preview_buf_line,
1629	          preview_buf_line,
1630	          false,
1631	          { prefix .. line }
1632	        )
1633	        vim.hl.range(
1634	          preview_buf,
1635	          preview_ns,
1636	          'Substitute',
1637	          {preview_buf_line, #prefix + start_idx - 1},
1638	          {preview_buf_line, #prefix + end_idx}
1639	        )
1640	        preview_buf_line = preview_buf_line + 1
1641	      end
1642	    end
1643	  end
1644
1645	  -- Return the value of the preview type
1646	  return 2
1647	end
1648
1649	-- Trims all trailing whitespace in the current buffer.
1650	local function trim_space(opts)
1651	  local line1 = opts.line1
1652	  local line2 = opts.line2
1653	  local buf = vim.api.nvim_get_current_buf()
1654	  local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false)
1655
1656	  local new_lines = {}
1657	  for i, line in ipairs(lines) do
1658	    new_lines[i] = string.gsub(line, '%s+$', '')
1659	  end
1660	  vim.api.nvim_buf_set_lines(buf, line1 - 1, line2, false, new_lines)
1661	end
1662
1663	-- Create the user command
1664	vim.api.nvim_create_user_command(
1665	  'TrimTrailingWhitespace',
1666	  trim_space,
1667	  { nargs = '?', range = '%', addr = 'lines', preview = trim_space_preview }
1668	)
1669<
1670
1671
1672Special cases ~
1673					*:command-bang* *:command-bar*
1674					*:command-register* *:command-buffer*
1675					*:command-keepscript*
1676There are some special cases as well:
1677
1678	-bang	    The command can take a ! modifier (like :q or :w)
1679	-bar	    The command can be followed by a "|" and another command.
1680		    A "|" inside the command argument is not allowed then.
1681		    Also checks for a " to start a comment.
1682	-register   The first argument to the command can be an optional
1683		    register name (like :del, :put, :yank).
1684	-buffer	    The command will only be available in the current buffer.
1685	-keepscript Do not use the location of where the user command was
1686		    defined for verbose messages, use the location of where
1687		    the user command was invoked.
1688
1689In the cases of the -count and -register attributes, if the optional argument
1690is supplied, it is removed from the argument list and is available to the
1691replacement text separately.
1692Note that these arguments can be abbreviated, but that is a deprecated
1693feature.  Use the full name for new scripts.
1694
1695
1696Replacement text ~
1697
1698The replacement text {repl} for a user defined command is scanned for special
1699escape sequences, using <...> notation.  Escape sequences are replaced with
1700values from the entered command line, and all other text is copied unchanged.
1701The resulting string is executed as an Ex command.  To avoid the replacement
1702use <lt> in place of the initial <.  Thus to include "<bang>" literally use
1703"<lt>bang>".
1704
1705The valid escape sequences are
1706
1707						*<line1>*
1708	<line1>	The starting line of the command range.
1709						*<line2>*
1710	<line2>	The final line of the command range.
1711						*<range>*
1712	<range> The number of items in the command range: 0, 1 or 2
1713						*<count>*
1714	<count>	Any count supplied (as described for the '-range'
1715		and '-count' attributes).
1716						*<bang>*
1717	<bang>	(See the '-bang' attribute) Expands to a ! if the
1718		command was executed with a ! modifier, otherwise
1719		expands to nothing.
1720					*<mods>* *<q-mods>* *:command-modifiers*
1721	<mods>  The command modifiers, if specified.  Otherwise, expands to
1722		nothing.  Supported modifiers are |:aboveleft|, |:belowright|,
1723		|:botright|, |:browse|, |:confirm|, |:hide|, |:horizontal|,
1724		|:keepalt|, |:keepjumps|, |:keepmarks|, |:keeppatterns|,
1725		|:leftabove|, |:lockmarks|, |:noautocmd|, |:noswapfile|,
1726		|:rightbelow|, |:sandbox|, |:silent|, |:tab|, |:topleft|,
1727		|:unsilent|, |:verbose|, and |:vertical|.
1728		Note that |:filter| is not supported.
1729		Examples: >
1730		    command! -nargs=+ -complete=file MyEdit
1731				\ for f in expand(<q-args>, 0, 1) |
1732				\ exe '<mods> split ' .. f |
1733				\ endfor
1734
1735		    function! SpecialEdit(files, mods)
1736			for f in expand(a:files, 0, 1)
1737			    exe a:mods .. ' split ' .. f
1738			endfor
1739		    endfunction
1740		    command! -nargs=+ -complete=file Sedit
1741				\ call SpecialEdit(<q-args>, <q-mods>)
1742<
1743						*<reg>* *<register>*
1744	<reg>	(See the '-register' attribute) The optional register,
1745		if specified.  Otherwise, expands to nothing.  <register>
1746		is a synonym for this.
1747						*<args>*
1748	<args>	The command arguments, exactly as supplied (but as
1749		noted above, any count or register can consume some
1750		of the arguments, which are then not part of <args>).
1751	<lt>	A single '<' (Less-Than) character.  This is needed if you
1752		want to get a literal copy of one of these escape sequences
1753		into the expansion - for example, to get <bang>, use
1754		<lt>bang>.
1755
1756							*<q-args>*
1757If the first two characters of an escape sequence are "q-" (for example,
1758<q-args>) then the value is quoted in such a way as to make it a valid value
1759for use in an expression.  This uses the argument as one single value.
1760When there is no argument <q-args> is an empty string.  See the
1761|q-args-example| below.
1762							*<f-args>*
1763To allow commands to pass their arguments on to a user-defined function, there
1764is a special form <f-args> ("function args").  This splits the command
1765arguments at spaces and tabs, quotes each argument individually, and the
1766<f-args> sequence is replaced by the comma-separated list of quoted arguments.
1767See the Mycmd example below.  If no arguments are given <f-args> is removed.
1768   To embed whitespace into an argument of <f-args>, prepend a backslash.
1769<f-args> replaces every pair of backslashes (\\) with one backslash.  A
1770backslash followed by a character other than white space or a backslash
1771remains unmodified.  Also see |f-args-example| below.  Overview:
1772
1773	command		   <f-args> ~
1774	XX ab		   "ab"
1775	XX a\b		   'a\b'
1776	XX a\ b		   'a b'
1777	XX a\  b	   'a ', 'b'
1778	XX a\\b		   'a\b'
1779	XX a\\ b	   'a\', 'b'
1780	XX a\\\b	   'a\\b'
1781	XX a\\\ b	   'a\ b'
1782	XX a\\\\b	   'a\\b'
1783	XX a\\\\ b	   'a\\', 'b'
1784	XX		   [nothing]
1785
1786
1787Note that if the "no arguments" situation is to be handled, you have to make
1788sure that the function can be called without arguments.
1789
1790Examples for user commands: >
1791
1792   " Delete everything after here to the end
1793   :com Ddel +,$d
1794
1795   " Rename the current buffer
1796   :com -nargs=1 -bang -complete=file Ren f <args>|w<bang>
1797
1798   " Replace a range with the contents of a file
1799   " (Enter this all as one line)
1800   :com -range -nargs=1 -complete=file
1801	 Replace <line1>-pu_|<line1>,<line2>d|r <args>|<line1>d
1802
1803   " Count the number of lines in the range
1804   :com! -range -nargs=0 Lines  echo <line2> - <line1> + 1 "lines"
1805
1806<						*f-args-example*
1807Call a user function (example of <f-args>) >
1808   :com -nargs=* Mycmd call Myfunc(<f-args>)
1809
1810When executed as: >
1811	:Mycmd arg1 arg2
1812This will invoke: >
1813	:call Myfunc("arg1","arg2")
1814
1815<						*q-args-example*
1816A more substantial example: >
1817   :function Allargs(command)
1818   :   let i = 0
1819   :   while i < argc()
1820   :	  if filereadable(argv(i))
1821   :	     execute "e " .. argv(i)
1822   :	     execute a:command
1823   :      endif
1824   :      let i = i + 1
1825   :   endwhile
1826   :endfunction
1827   :command -nargs=+ -complete=command Allargs call Allargs(<q-args>)
1828
1829The command Allargs takes any Vim command(s) as argument and executes it on
1830all files in the argument list.  Usage example (note use of the "e" flag to
1831ignore errors and the "update" command to write modified buffers): >
1832	:Allargs %s/foo/bar/ge|update
1833This will invoke: >
1834	:call Allargs("%s/foo/bar/ge|update")
1835<
1836
1837 vim:tw=78:ts=8:noet:ft=help:norl: