map.txt 61 KB

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