map.txt 70 KB

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