neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

map.txt (73471B)


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