map.txt 78 KB

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