autocmd.txt 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  1. *autocmd.txt* For Vim version 9.0. Last change: 2022 Nov 22
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Automatic commands *autocommand* *autocommands*
  4. For a basic explanation, see section |40.3| in the user manual.
  5. 1. Introduction |autocmd-intro|
  6. 2. Defining autocommands |autocmd-define|
  7. 3. Removing autocommands |autocmd-remove|
  8. 4. Listing autocommands |autocmd-list|
  9. 5. Events |autocmd-events|
  10. 6. Patterns |autocmd-patterns|
  11. 7. Buffer-local autocommands |autocmd-buflocal|
  12. 8. Groups |autocmd-groups|
  13. 9. Executing autocommands |autocmd-execute|
  14. 10. Using autocommands |autocmd-use|
  15. 11. Disabling autocommands |autocmd-disable|
  16. ==============================================================================
  17. 1. Introduction *autocmd-intro*
  18. You can specify commands to be executed automatically when reading or writing
  19. a file, when entering or leaving a buffer or window, and when exiting Vim.
  20. For example, you can create an autocommand to set the 'cindent' option for
  21. files matching *.c. You can also use autocommands to implement advanced
  22. features, such as editing compressed files (see |gzip-example|). The usual
  23. place to put autocommands is in your .vimrc or .exrc file.
  24. *E203* *E204* *E143* *E855* *E937* *E952*
  25. WARNING: Using autocommands is very powerful, and may lead to unexpected side
  26. effects. Be careful not to destroy your text.
  27. - It's a good idea to do some testing on an expendable copy of a file first.
  28. For example: If you use autocommands to decompress a file when starting to
  29. edit it, make sure that the autocommands for compressing when writing work
  30. correctly.
  31. - Be prepared for an error halfway through (e.g., disk full). Vim will mostly
  32. be able to undo the changes to the buffer, but you may have to clean up the
  33. changes to other files by hand (e.g., compress a file that has been
  34. decompressed).
  35. - If the BufRead* events allow you to edit a compressed file, the FileRead*
  36. events should do the same (this makes recovery possible in some rare cases).
  37. It's a good idea to use the same autocommands for the File* and Buf* events
  38. when possible.
  39. Recommended use:
  40. - Always use a group, so that it's easy to delete the autocommand.
  41. - Keep the command itself short, call a function to do more work.
  42. - Make it so that the script it is defined in can be sourced several times
  43. without the autocommand being repeated.
  44. Example in Vim9 script: >
  45. autocmd_add({replace: true,
  46. group: 'DemoGroup',
  47. event: 'BufEnter',
  48. pattern: '*.txt',
  49. cmd: 'call DemoBufEnter()'
  50. })
  51. In legacy script: >
  52. call autocmd_add(#{replace: v:true,
  53. \ group: 'DemoGroup',
  54. \ event: 'BufEnter',
  55. \ pattern: '*.txt',
  56. \ cmd: 'call DemoBufEnter()'
  57. \ })
  58. ==============================================================================
  59. 2. Defining autocommands *autocmd-define*
  60. *:au* *:autocmd*
  61. :au[tocmd] [group] {event} {aupat} [++once] [++nested] {cmd}
  62. Add {cmd} to the list of commands that Vim will
  63. execute automatically on {event} for a file matching
  64. {aupat} |autocmd-patterns|.
  65. Here {event} cannot be "*". *E1155*
  66. Note: A quote character is seen as argument to the
  67. :autocmd and won't start a comment.
  68. Vim always adds the {cmd} after existing autocommands,
  69. so that the autocommands execute in the order in which
  70. they were given.
  71. See |autocmd-nested| for [++nested]. "nested"
  72. (without the ++) can also be used, for backwards
  73. compatibility, but not in |Vim9| script. *E1078*
  74. *autocmd-once*
  75. If [++once] is supplied the command is executed once,
  76. then removed ("one shot").
  77. The special pattern <buffer> or <buffer=N> defines a buffer-local autocommand.
  78. See |autocmd-buflocal|.
  79. If the `:autocmd` is in Vim9 script (a script that starts with `:vim9script`
  80. and in a `:def` function) then {cmd} will be executed as in Vim9
  81. script. Thus this depends on where the autocmd is defined, not where it is
  82. triggered.
  83. {cmd} can be a block, like with `:command`, see |:command-repl|. Example: >
  84. au BufReadPost *.xml {
  85. setlocal matchpairs+=<:>
  86. /<start
  87. }
  88. The |autocmd_add()| function can be used to add a list of autocmds and autocmd
  89. groups from a Vim script. It is preferred if you have anything that would
  90. require using `:execute` with `:autocmd`.
  91. Note: The ":autocmd" command can only be followed by another command when the
  92. '|' appears where the pattern is expected. This works: >
  93. :augroup mine | au! BufRead | augroup END
  94. But this sees "augroup" as part of the defined command: >
  95. :augroup mine | au! BufRead * | augroup END
  96. :augroup mine | au BufRead * set tw=70 | augroup END
  97. Instead you can put the group name into the command: >
  98. :au! mine BufRead *
  99. :au mine BufRead * set tw=70
  100. Or use `:execute`: >
  101. :augroup mine | exe "au! BufRead *" | augroup END
  102. :augroup mine | exe "au BufRead * set tw=70" | augroup END
  103. < *autocmd-expand*
  104. Note that special characters (e.g., "%", "<cword>") in the ":autocmd"
  105. arguments are not expanded when the autocommand is defined. These will be
  106. expanded when the Event is recognized, and the {cmd} is executed. The only
  107. exception is that "<sfile>" is expanded when the autocmd is defined. Example:
  108. >
  109. :au BufNewFile,BufRead *.html so <sfile>:h/html.vim
  110. Here Vim expands <sfile> to the name of the file containing this line.
  111. `:autocmd` adds to the list of autocommands regardless of whether they are
  112. already present. When your .vimrc file is sourced twice, the autocommands
  113. will appear twice. To avoid this, define your autocommands in a group, so
  114. that you can easily clear them: >
  115. augroup vimrc
  116. " Remove all vimrc autocommands
  117. autocmd!
  118. au BufNewFile,BufRead *.html so <sfile>:h/html.vim
  119. augroup END
  120. If you don't want to remove all autocommands, you can instead use a variable
  121. to ensure that Vim includes the autocommands only once: >
  122. :if !exists("autocommands_loaded")
  123. : let autocommands_loaded = 1
  124. : au ...
  125. :endif
  126. When the [group] argument is not given, Vim uses the current group (as defined
  127. with ":augroup"); otherwise, Vim uses the group defined with [group]. Note
  128. that [group] must have been defined before. You cannot define a new group
  129. with ":au group ..."; use ":augroup" for that.
  130. While testing autocommands, you might find the 'verbose' option to be useful: >
  131. :set verbose=9
  132. This setting makes Vim echo the autocommands as it executes them.
  133. When defining an autocommand in a script, it will be able to call functions
  134. local to the script and use mappings local to the script. When the event is
  135. triggered and the command executed, it will run in the context of the script
  136. it was defined in. This matters if |<SID>| is used in a command.
  137. When executing the commands, the message from one command overwrites a
  138. previous message. This is different from when executing the commands
  139. manually. Mostly the screen will not scroll up, thus there is no hit-enter
  140. prompt. When one command outputs two messages this can happen anyway.
  141. ==============================================================================
  142. 3. Removing autocommands *autocmd-remove*
  143. In addition to the below described commands, the |autocmd_delete()| function can
  144. be used to remove a list of autocmds and autocmd groups from a Vim script.
  145. :au[tocmd]! [group] {event} {aupat} [++once] [++nested] {cmd}
  146. Remove all autocommands associated with {event} and
  147. {aupat}, and add the command {cmd}.
  148. See |autocmd-once| for [++once].
  149. See |autocmd-nested| for [++nested].
  150. :au[tocmd]! [group] {event} {aupat}
  151. Remove all autocommands associated with {event} and
  152. {aupat}.
  153. :au[tocmd]! [group] * {aupat}
  154. Remove all autocommands associated with {aupat} for
  155. all events.
  156. :au[tocmd]! [group] {event}
  157. Remove ALL autocommands for {event}.
  158. Warning: You should not do this without a group for
  159. |BufRead| and other common events, it can break
  160. plugins, syntax highlighting, etc.
  161. :au[tocmd]! [group] Remove ALL autocommands.
  162. Note: a quote will be seen as argument to the :autocmd
  163. and won't start a comment.
  164. Warning: You should normally not do this without a
  165. group, it breaks plugins, syntax highlighting, etc.
  166. When the [group] argument is not given, Vim uses the current group (as defined
  167. with ":augroup"); otherwise, Vim uses the group defined with [group].
  168. ==============================================================================
  169. 4. Listing autocommands *autocmd-list*
  170. :au[tocmd] [group] {event} {aupat}
  171. Show the autocommands associated with {event} and
  172. {aupat}.
  173. :au[tocmd] [group] * {aupat}
  174. Show the autocommands associated with {aupat} for all
  175. events.
  176. :au[tocmd] [group] {event}
  177. Show all autocommands for {event}.
  178. :au[tocmd] [group] Show all autocommands.
  179. If you provide the [group] argument, Vim lists only the autocommands for
  180. [group]; otherwise, Vim lists the autocommands for ALL groups. Note that this
  181. argument behavior differs from that for defining and removing autocommands.
  182. In order to list buffer-local autocommands, use a pattern in the form <buffer>
  183. or <buffer=N>. See |autocmd-buflocal|.
  184. The |autocmd_get()| function can be used from a Vim script to get a list of
  185. autocmds.
  186. *:autocmd-verbose*
  187. When 'verbose' is non-zero, listing an autocommand will also display where it
  188. was last defined. Example: >
  189. :verbose autocmd BufEnter
  190. FileExplorer BufEnter
  191. * call s:LocalBrowse(expand("<amatch>"))
  192. Last set from /usr/share/vim/vim-7.0/plugin/NetrwPlugin.vim
  193. <
  194. See |:verbose-cmd| for more information.
  195. ==============================================================================
  196. 5. Events *autocmd-events* *E215* *E216*
  197. You can specify a comma-separated list of event names. No white space can be
  198. used in this list. The command applies to all the events in the list.
  199. For READING FILES there are four kinds of events possible:
  200. BufNewFile starting to edit a non-existent file
  201. BufReadPre BufReadPost starting to edit an existing file
  202. FilterReadPre FilterReadPost read the temp file with filter output
  203. FileReadPre FileReadPost any other file read
  204. Vim uses only one of these four kinds when reading a file. The "Pre" and
  205. "Post" events are both triggered, before and after reading the file.
  206. Note that the autocommands for the *ReadPre events and all the Filter events
  207. are not allowed to change the current buffer (you will get an error message if
  208. this happens). This is to prevent the file to be read into the wrong buffer.
  209. Note that the 'modified' flag is reset AFTER executing the BufReadPost
  210. and BufNewFile autocommands. But when the 'modified' option was set by the
  211. autocommands, this doesn't happen.
  212. You can use the 'eventignore' option to ignore a number of events or all
  213. events.
  214. *autocommand-events* *{event}*
  215. Vim recognizes the following events. Vim ignores the case of event names
  216. (e.g., you can use "BUFread" or "bufread" instead of "BufRead").
  217. First an overview by function with a short explanation. Then the list
  218. alphabetically with full explanations |autocmd-events-abc|.
  219. Name triggered by ~
  220. Reading
  221. |BufNewFile| starting to edit a file that doesn't exist
  222. |BufReadPre| starting to edit a new buffer, before reading the file
  223. |BufRead| starting to edit a new buffer, after reading the file
  224. |BufReadPost| starting to edit a new buffer, after reading the file
  225. |BufReadCmd| before starting to edit a new buffer |Cmd-event|
  226. |FileReadPre| before reading a file with a ":read" command
  227. |FileReadPost| after reading a file with a ":read" command
  228. |FileReadCmd| before reading a file with a ":read" command |Cmd-event|
  229. |FilterReadPre| before reading a file from a filter command
  230. |FilterReadPost| after reading a file from a filter command
  231. |StdinReadPre| before reading from stdin into the buffer
  232. |StdinReadPost| After reading from the stdin into the buffer
  233. Writing
  234. |BufWrite| starting to write the whole buffer to a file
  235. |BufWritePre| starting to write the whole buffer to a file
  236. |BufWritePost| after writing the whole buffer to a file
  237. |BufWriteCmd| before writing the whole buffer to a file |Cmd-event|
  238. |FileWritePre| starting to write part of a buffer to a file
  239. |FileWritePost| after writing part of a buffer to a file
  240. |FileWriteCmd| before writing part of a buffer to a file |Cmd-event|
  241. |FileAppendPre| starting to append to a file
  242. |FileAppendPost| after appending to a file
  243. |FileAppendCmd| before appending to a file |Cmd-event|
  244. |FilterWritePre| starting to write a file for a filter command or diff
  245. |FilterWritePost| after writing a file for a filter command or diff
  246. Buffers
  247. |BufAdd| just after adding a buffer to the buffer list
  248. |BufCreate| just after adding a buffer to the buffer list
  249. |BufDelete| before deleting a buffer from the buffer list
  250. |BufWipeout| before completely deleting a buffer
  251. |BufFilePre| before changing the name of the current buffer
  252. |BufFilePost| after changing the name of the current buffer
  253. |BufEnter| after entering a buffer
  254. |BufLeave| before leaving to another buffer
  255. |BufWinEnter| after a buffer is displayed in a window
  256. |BufWinLeave| before a buffer is removed from a window
  257. |BufUnload| before unloading a buffer
  258. |BufHidden| just before a buffer becomes hidden
  259. |BufNew| just after creating a new buffer
  260. |SwapExists| detected an existing swap file
  261. Options
  262. |FileType| when the 'filetype' option has been set
  263. |Syntax| when the 'syntax' option has been set
  264. |EncodingChanged| after the 'encoding' option has been changed
  265. |TermChanged| after the value of 'term' has changed
  266. |OptionSet| after setting any option
  267. Startup and exit
  268. |VimEnter| after doing all the startup stuff
  269. |GUIEnter| after starting the GUI successfully
  270. |GUIFailed| after starting the GUI failed
  271. |TermResponse| after the terminal response to |t_RV| is received
  272. |QuitPre| when using `:quit`, before deciding whether to exit
  273. |ExitPre| when using a command that may make Vim exit
  274. |VimLeavePre| before exiting Vim, before writing the viminfo file
  275. |VimLeave| before exiting Vim, after writing the viminfo file
  276. |VimSuspend| when suspending Vim
  277. |VimResume| when Vim is resumed after being suspended
  278. Terminal
  279. |TerminalOpen| after a terminal buffer was created
  280. |TerminalWinOpen| after a terminal buffer was created in a new window
  281. Various
  282. |FileChangedShell| Vim notices that a file changed since editing started
  283. |FileChangedShellPost| After handling a file changed since editing started
  284. |FileChangedRO| before making the first change to a read-only file
  285. |DiffUpdated| after diffs have been updated
  286. |DirChangedPre| before the working directory will change
  287. |DirChanged| after the working directory has changed
  288. |ShellCmdPost| after executing a shell command
  289. |ShellFilterPost| after filtering with a shell command
  290. |CmdUndefined| a user command is used but it isn't defined
  291. |FuncUndefined| a user function is used but it isn't defined
  292. |SpellFileMissing| a spell file is used but it can't be found
  293. |SourcePre| before sourcing a Vim script
  294. |SourcePost| after sourcing a Vim script
  295. |SourceCmd| before sourcing a Vim script |Cmd-event|
  296. |VimResized| after the Vim window size changed
  297. |FocusGained| Vim got input focus
  298. |FocusLost| Vim lost input focus
  299. |CursorHold| the user doesn't press a key for a while
  300. |CursorHoldI| the user doesn't press a key for a while in Insert mode
  301. |CursorMoved| the cursor was moved in Normal mode
  302. |CursorMovedI| the cursor was moved in Insert mode
  303. |WinNew| after creating a new window
  304. |TabNew| after creating a new tab page
  305. |WinClosed| after closing a window
  306. |TabClosed| after closing a tab page
  307. |WinEnter| after entering another window
  308. |WinLeave| before leaving a window
  309. |TabEnter| after entering another tab page
  310. |TabLeave| before leaving a tab page
  311. |CmdwinEnter| after entering the command-line window
  312. |CmdwinLeave| before leaving the command-line window
  313. |CmdlineChanged| after a change was made to the command-line text
  314. |CmdlineEnter| after the cursor moves to the command line
  315. |CmdlineLeave| before the cursor leaves the command line
  316. |InsertEnter| starting Insert mode
  317. |InsertChange| when typing <Insert> while in Insert or Replace mode
  318. |InsertLeave| when leaving Insert mode
  319. |InsertLeavePre| just before leaving Insert mode
  320. |InsertCharPre| when a character was typed in Insert mode, before
  321. inserting it
  322. |ModeChanged| after changing the mode
  323. |TextChanged| after a change was made to the text in Normal mode
  324. |TextChangedI| after a change was made to the text in Insert mode
  325. when popup menu is not visible
  326. |TextChangedP| after a change was made to the text in Insert mode
  327. when popup menu visible
  328. |TextChangedT| after a change was made to the text in Terminal mode
  329. |TextYankPost| after text has been yanked or deleted
  330. |SafeState| nothing pending, going to wait for the user to type a
  331. character
  332. |SafeStateAgain| repeated SafeState
  333. |ColorSchemePre| before loading a color scheme
  334. |ColorScheme| after loading a color scheme
  335. |RemoteReply| a reply from a server Vim was received
  336. |QuickFixCmdPre| before a quickfix command is run
  337. |QuickFixCmdPost| after a quickfix command is run
  338. |SessionLoadPost| after loading a session file
  339. |MenuPopup| just before showing the popup menu
  340. |CompleteChanged| after Insert mode completion menu changed
  341. |CompleteDonePre| after Insert mode completion is done, before clearing
  342. info
  343. |CompleteDone| after Insert mode completion is done, after clearing
  344. info
  345. |User| to be used in combination with ":doautocmd"
  346. |SigUSR1| after the SIGUSR1 signal has been detected
  347. |WinScrolled| after scrolling or resizing a window
  348. The alphabetical list of autocommand events: *autocmd-events-abc*
  349. *BufCreate* *BufAdd*
  350. BufAdd or BufCreate Just after creating a new buffer which is
  351. added to the buffer list, or adding a buffer
  352. to the buffer list.
  353. Also used just after a buffer in the buffer
  354. list has been renamed.
  355. Not triggered for the initial buffers created
  356. during startup.
  357. The BufCreate event is for historic reasons.
  358. NOTE: When this autocommand is executed, the
  359. current buffer "%" may be different from the
  360. buffer being created "<afile>".
  361. *BufDelete*
  362. BufDelete Before deleting a buffer from the buffer list.
  363. The BufUnload may be called first (if the
  364. buffer was loaded).
  365. Also used just before a buffer in the buffer
  366. list is renamed.
  367. NOTE: When this autocommand is executed, the
  368. current buffer "%" may be different from the
  369. buffer being deleted "<afile>" and "<abuf>".
  370. Don't change to another buffer, it will cause
  371. problems.
  372. *BufEnter*
  373. BufEnter After entering a buffer. Useful for setting
  374. options for a file type. Also executed when
  375. starting to edit a buffer, after the
  376. BufReadPost autocommands.
  377. *BufFilePost*
  378. BufFilePost After changing the name of the current buffer
  379. with the ":file" or ":saveas" command.
  380. *BufFilePre*
  381. BufFilePre Before changing the name of the current buffer
  382. with the ":file" or ":saveas" command.
  383. *BufHidden*
  384. BufHidden Just before a buffer becomes hidden. That is,
  385. when there are no longer windows that show
  386. the buffer, but the buffer is not unloaded or
  387. deleted. Not used for ":qa" or ":q" when
  388. exiting Vim.
  389. NOTE: When this autocommand is executed, the
  390. current buffer "%" may be different from the
  391. buffer being unloaded "<afile>".
  392. *BufLeave*
  393. BufLeave Before leaving to another buffer. Also when
  394. leaving or closing the current window and the
  395. new current window is not for the same buffer.
  396. Not used for ":qa" or ":q" when exiting Vim.
  397. *BufNew*
  398. BufNew Just after creating a new buffer. Also used
  399. just after a buffer has been renamed. When
  400. the buffer is added to the buffer list BufAdd
  401. will be triggered too.
  402. NOTE: When this autocommand is executed, the
  403. current buffer "%" may be different from the
  404. buffer being created "<afile>".
  405. *BufNewFile*
  406. BufNewFile When starting to edit a file that doesn't
  407. exist. Can be used to read in a skeleton
  408. file.
  409. *BufRead* *BufReadPost*
  410. BufRead or BufReadPost When starting to edit a new buffer, after
  411. reading the file into the buffer, before
  412. executing the modelines. See |BufWinEnter|
  413. for when you need to do something after
  414. processing the modelines.
  415. Also triggered:
  416. - when writing an unnamed buffer in a way that
  417. the buffer gets a name
  418. - after successfully recovering a file
  419. - for the filetypedetect group when executing
  420. ":filetype detect"
  421. Not triggered:
  422. - for the `:read file` command
  423. - when the file doesn't exist
  424. *BufReadCmd*
  425. BufReadCmd Before starting to edit a new buffer. Should
  426. read the file into the buffer. |Cmd-event|
  427. *BufReadPre* *E200* *E201*
  428. BufReadPre When starting to edit a new buffer, before
  429. reading the file into the buffer. Not used
  430. if the file doesn't exist.
  431. *BufUnload*
  432. BufUnload Before unloading a buffer. This is when the
  433. text in the buffer is going to be freed. This
  434. may be after a BufWritePost and before a
  435. BufDelete. Also used for all buffers that are
  436. loaded when Vim is going to exit.
  437. NOTE: When this autocommand is executed, the
  438. current buffer "%" may be different from the
  439. buffer being unloaded "<afile>".
  440. Don't change to another buffer or window, it
  441. will cause problems!
  442. When exiting and v:dying is 2 or more this
  443. event is not triggered.
  444. *BufWinEnter*
  445. BufWinEnter After a buffer is displayed in a window. This
  446. can be when the buffer is loaded (after
  447. processing the modelines) or when a hidden
  448. buffer is displayed in a window (and is no
  449. longer hidden).
  450. Does not happen for |:split| without
  451. arguments, since you keep editing the same
  452. buffer, or ":split" with a file that's already
  453. open in a window, because it re-uses an
  454. existing buffer. But it does happen for a
  455. ":split" with the name of the current buffer,
  456. since it reloads that buffer.
  457. Does not happen for a terminal window, because
  458. it starts in Terminal-Job mode and Normal mode
  459. commands won't work. Use |TerminalOpen| instead.
  460. *BufWinLeave*
  461. BufWinLeave Before a buffer is removed from a window.
  462. Not when it's still visible in another window.
  463. Also triggered when exiting. It's triggered
  464. before BufUnload or BufHidden.
  465. NOTE: When this autocommand is executed, the
  466. current buffer "%" may be different from the
  467. buffer being unloaded "<afile>".
  468. When exiting and v:dying is 2 or more this
  469. event is not triggered.
  470. *BufWipeout*
  471. BufWipeout Before completely deleting a buffer. The
  472. BufUnload and BufDelete events may be called
  473. first (if the buffer was loaded and was in the
  474. buffer list). Also used just before a buffer
  475. is renamed (also when it's not in the buffer
  476. list).
  477. NOTE: When this autocommand is executed, the
  478. current buffer "%" may be different from the
  479. buffer being deleted "<afile>".
  480. Don't change to another buffer, it will cause
  481. problems.
  482. *BufWrite* *BufWritePre*
  483. BufWrite or BufWritePre Before writing the whole buffer to a file.
  484. *BufWriteCmd*
  485. BufWriteCmd Before writing the whole buffer to a file.
  486. Should do the writing of the file and reset
  487. 'modified' if successful, unless '+' is in
  488. 'cpo' and writing to another file |cpo-+|.
  489. The buffer contents should not be changed.
  490. When the command resets 'modified' the undo
  491. information is adjusted to mark older undo
  492. states as 'modified', like |:write| does.
  493. |Cmd-event|
  494. *BufWritePost*
  495. BufWritePost After writing the whole buffer to a file
  496. (should undo the commands for BufWritePre).
  497. *CmdUndefined*
  498. CmdUndefined When a user command is used but it isn't
  499. defined. Useful for defining a command only
  500. when it's used. The pattern is matched
  501. against the command name. Both <amatch> and
  502. <afile> are set to the name of the command.
  503. NOTE: Autocompletion won't work until the
  504. command is defined. An alternative is to
  505. always define the user command and have it
  506. invoke an autoloaded function. See |autoload|.
  507. *CmdlineChanged*
  508. CmdlineChanged After a change was made to the text in the
  509. command line. Be careful not to mess up
  510. the command line, it may cause Vim to lock up.
  511. <afile> is set to a single character,
  512. indicating the type of command-line.
  513. |cmdwin-char|
  514. *CmdlineEnter*
  515. CmdlineEnter After moving the cursor to the command line,
  516. where the user can type a command or search
  517. string; including non-interactive use of ":"
  518. in a mapping, but not when using |<Cmd>|.
  519. <afile> is set to a single character,
  520. indicating the type of command-line.
  521. |cmdwin-char|
  522. *CmdlineLeave*
  523. CmdlineLeave Before leaving the command line; including
  524. non-interactive use of ":" in a mapping, but
  525. not when using |<Cmd>|.
  526. Also when abandoning the command line, after
  527. typing CTRL-C or <Esc>.
  528. When the commands result in an error the
  529. command line is still executed.
  530. <afile> is set to a single character,
  531. indicating the type of command-line.
  532. |cmdwin-char|
  533. *CmdwinEnter*
  534. CmdwinEnter After entering the command-line window.
  535. Useful for setting options specifically for
  536. this special type of window.
  537. <afile> is set to a single character,
  538. indicating the type of command-line.
  539. |cmdwin-char|
  540. *CmdwinLeave*
  541. CmdwinLeave Before leaving the command-line window.
  542. Useful to clean up any global setting done
  543. with CmdwinEnter.
  544. <afile> is set to a single character,
  545. indicating the type of command-line.
  546. |cmdwin-char|
  547. *ColorScheme*
  548. ColorScheme After loading a color scheme. |:colorscheme|
  549. Not triggered if the color scheme is not
  550. found.
  551. The pattern is matched against the
  552. colorscheme name. <afile> can be used for the
  553. name of the actual file where this option was
  554. set, and <amatch> for the new colorscheme
  555. name.
  556. *ColorSchemePre*
  557. ColorSchemePre Before loading a color scheme. |:colorscheme|
  558. Useful to setup removing things added by a
  559. color scheme, before another one is loaded.
  560. CompleteChanged *CompleteChanged*
  561. After each time the Insert mode completion
  562. menu changed. Not fired on popup menu hide,
  563. use |CompleteDonePre| or |CompleteDone| for
  564. that. Never triggered recursively.
  565. Sets these |v:event| keys:
  566. completed_item See |complete-items|.
  567. height nr of items visible
  568. width screen cells
  569. row top screen row
  570. col leftmost screen column
  571. size total nr of items
  572. scrollbar TRUE if visible
  573. It is not allowed to change the text |textlock|.
  574. The size and position of the popup are also
  575. available by calling |pum_getpos()|.
  576. *CompleteDonePre*
  577. CompleteDonePre After Insert mode completion is done. Either
  578. when something was completed or abandoning
  579. completion. |ins-completion|
  580. |complete_info()| can be used, the info is
  581. cleared after triggering CompleteDonePre.
  582. The |v:completed_item| variable contains
  583. information about the completed item.
  584. *CompleteDone*
  585. CompleteDone After Insert mode completion is done. Either
  586. when something was completed or abandoning
  587. completion. |ins-completion|
  588. |complete_info()| cannot be used, the info is
  589. cleared before triggering CompleteDone. Use
  590. CompleteDonePre if you need it.
  591. The |v:completed_item| variable contains
  592. information about the completed item.
  593. *CursorHold*
  594. CursorHold When the user doesn't press a key for the time
  595. specified with 'updatetime'. Not triggered
  596. until the user has pressed a key (i.e. doesn't
  597. fire every 'updatetime' ms if you leave Vim to
  598. make some coffee. :) See |CursorHold-example|
  599. for previewing tags.
  600. This event is only triggered in Normal mode.
  601. It is not triggered when waiting for a command
  602. argument to be typed, or a movement after an
  603. operator.
  604. While recording the CursorHold event is not
  605. triggered. |q|
  606. *<CursorHold>*
  607. Internally the autocommand is triggered by the
  608. <CursorHold> key. In an expression mapping
  609. |getchar()| may see this character.
  610. Note: Interactive commands cannot be used for
  611. this event. There is no hit-enter prompt,
  612. the screen is updated directly (when needed).
  613. Note: In the future there will probably be
  614. another option to set the time.
  615. Hint: to force an update of the status lines
  616. use: >
  617. :let &ro = &ro
  618. < {only on Amiga, Unix, Win32 and all GUI
  619. versions}
  620. *CursorHoldI*
  621. CursorHoldI Just like CursorHold, but in Insert mode.
  622. Not triggered when waiting for another key,
  623. e.g. after CTRL-V, and not when in CTRL-X mode
  624. |insert_expand|.
  625. *CursorMoved*
  626. CursorMoved After the cursor was moved in Normal or Visual
  627. mode. Also when the text of the cursor line
  628. has been changed, e.g., with "x", "rx" or "p".
  629. Not always triggered when there is typeahead,
  630. while executing commands in a script file,
  631. when an operator is pending or when moving to
  632. another window while remaining at the same
  633. cursor position.
  634. For an example see |match-parens|.
  635. Note: This can not be skipped with
  636. `:noautocmd`.
  637. Careful: This is triggered very often, don't
  638. do anything that the user does not expect or
  639. that is slow.
  640. *CursorMovedI*
  641. CursorMovedI After the cursor was moved in Insert mode.
  642. Not triggered when the popup menu is visible.
  643. Otherwise the same as CursorMoved.
  644. *DiffUpdated*
  645. DiffUpdated After diffs have been updated. Depending on
  646. what kind of diff is being used (internal or
  647. external) this can be triggered on every
  648. change or when doing |:diffupdate|.
  649. *DirChangedPre*
  650. DirChangedPre The working directory is going to be changed,
  651. as with |DirChanged|. The pattern is like
  652. with |DirChanged|. The new directory can be
  653. found in v:event.directory.
  654. *DirChanged*
  655. DirChanged The working directory has changed in response
  656. to the |:cd| or |:tcd| or |:lcd| commands, or
  657. as a result of the 'autochdir' option.
  658. The pattern can be:
  659. "window" to trigger on `:lcd`
  660. "tabpage" to trigger on `:tcd`
  661. "global" to trigger on `:cd`
  662. "auto" to trigger on 'autochdir'.
  663. "drop" to trigger on editing a file
  664. <afile> is set to the new directory name.
  665. *EncodingChanged*
  666. EncodingChanged Fires off after the 'encoding' option has been
  667. changed. Useful to set up fonts, for example.
  668. *ExitPre*
  669. ExitPre When using `:quit`, `:wq` in a way it makes
  670. Vim exit, or using `:qall`, just after
  671. |QuitPre|. Can be used to close any
  672. non-essential window. Exiting may still be
  673. cancelled if there is a modified buffer that
  674. isn't automatically saved, use |VimLeavePre|
  675. for really exiting.
  676. *FileAppendCmd*
  677. FileAppendCmd Before appending to a file. Should do the
  678. appending to the file. Use the '[ and ']
  679. marks for the range of lines. |Cmd-event|
  680. *FileAppendPost*
  681. FileAppendPost After appending to a file.
  682. *FileAppendPre*
  683. FileAppendPre Before appending to a file. Use the '[ and ']
  684. marks for the range of lines.
  685. *FileChangedRO*
  686. FileChangedRO Before making the first change to a read-only
  687. file. Can be used to check-out the file from
  688. a source control system. Not triggered when
  689. the change was caused by an autocommand.
  690. This event is triggered when making the first
  691. change in a buffer or the first change after
  692. 'readonly' was set, just before the change is
  693. applied to the text.
  694. WARNING: If the autocommand moves the cursor
  695. the effect of the change is undefined.
  696. *E788*
  697. It is not allowed to change to another buffer
  698. here. You can reload the buffer but not edit
  699. another one.
  700. *E881*
  701. If the number of lines changes saving for undo
  702. may fail and the change will be aborted.
  703. *FileChangedShell*
  704. FileChangedShell When Vim notices that the modification time of
  705. a file has changed since editing started.
  706. Also when the file attributes of the file
  707. change or when the size of the file changes.
  708. |timestamp|
  709. Mostly triggered after executing a shell
  710. command, but also with a |:checktime| command
  711. or when gvim regains input focus.
  712. This autocommand is triggered for each changed
  713. file. It is not used when 'autoread' is set
  714. and the buffer was not changed. If a
  715. FileChangedShell autocommand is present the
  716. warning message and prompt is not given.
  717. The |v:fcs_reason| variable is set to indicate
  718. what happened and |v:fcs_choice| can be used
  719. to tell Vim what to do next.
  720. NOTE: When this autocommand is executed, the
  721. current buffer "%" may be different from the
  722. buffer that was changed, which is in "<afile>".
  723. NOTE: The commands must not change the current
  724. buffer, jump to another buffer or delete a
  725. buffer. *E246* *E811*
  726. NOTE: This event never nests, to avoid an
  727. endless loop. This means that while executing
  728. commands for the FileChangedShell event no
  729. other FileChangedShell event will be
  730. triggered.
  731. *FileChangedShellPost*
  732. FileChangedShellPost After handling a file that was changed outside
  733. of Vim. Can be used to update the statusline.
  734. *FileEncoding*
  735. FileEncoding Obsolete. It still works and is equivalent
  736. to |EncodingChanged|.
  737. *FileReadCmd*
  738. FileReadCmd Before reading a file with a ":read" command.
  739. Should do the reading of the file. |Cmd-event|
  740. *FileReadPost*
  741. FileReadPost After reading a file with a ":read" command.
  742. Note that Vim sets the '[ and '] marks to the
  743. first and last line of the read. This can be
  744. used to operate on the lines just read.
  745. *FileReadPre*
  746. FileReadPre Before reading a file with a ":read" command.
  747. *FileType*
  748. FileType When the 'filetype' option has been set. The
  749. pattern is matched against the filetype.
  750. <afile> can be used for the name of the file
  751. where this option was set, and <amatch> for
  752. the new value of 'filetype'. Navigating to
  753. another window or buffer is not allowed.
  754. See |filetypes|.
  755. *FileWriteCmd*
  756. FileWriteCmd Before writing to a file, when not writing the
  757. whole buffer. Should do the writing to the
  758. file. Should not change the buffer. Use the
  759. '[ and '] marks for the range of lines.
  760. |Cmd-event|
  761. *FileWritePost*
  762. FileWritePost After writing to a file, when not writing the
  763. whole buffer.
  764. *FileWritePre*
  765. FileWritePre Before writing to a file, when not writing the
  766. whole buffer. Use the '[ and '] marks for the
  767. range of lines.
  768. *FilterReadPost*
  769. FilterReadPost After reading a file from a filter command.
  770. Vim checks the pattern against the name of
  771. the current buffer as with FilterReadPre.
  772. Not triggered when 'shelltemp' is off.
  773. *FilterReadPre* *E135*
  774. FilterReadPre Before reading a file from a filter command.
  775. Vim checks the pattern against the name of
  776. the current buffer, not the name of the
  777. temporary file that is the output of the
  778. filter command.
  779. Not triggered when 'shelltemp' is off.
  780. *FilterWritePost*
  781. FilterWritePost After writing a file for a filter command or
  782. making a diff with an external diff (see
  783. |DiffUpdated| for internal diff).
  784. Vim checks the pattern against the name of
  785. the current buffer as with FilterWritePre.
  786. Not triggered when 'shelltemp' is off.
  787. *FilterWritePre*
  788. FilterWritePre Before writing a file for a filter command or
  789. making a diff with an external diff.
  790. Vim checks the pattern against the name of
  791. the current buffer, not the name of the
  792. temporary file that is the output of the
  793. filter command.
  794. Not triggered when 'shelltemp' is off.
  795. *FocusGained*
  796. FocusGained When Vim got input focus. Only for the GUI
  797. version and a few console versions where this
  798. can be detected.
  799. *FocusLost*
  800. FocusLost When Vim lost input focus. Only for the GUI
  801. version and a few console versions where this
  802. can be detected. May also happen when a
  803. dialog pops up.
  804. *FuncUndefined*
  805. FuncUndefined When a user function is used but it isn't
  806. defined. Useful for defining a function only
  807. when it's used. The pattern is matched
  808. against the function name. Both <amatch> and
  809. <afile> are set to the name of the function.
  810. Not triggered when compiling a |Vim9|
  811. function.
  812. NOTE: When writing Vim scripts a better
  813. alternative is to use an autoloaded function.
  814. See |autoload-functions|.
  815. *GUIEnter*
  816. GUIEnter After starting the GUI successfully, and after
  817. opening the window. It is triggered before
  818. VimEnter when using gvim. Can be used to
  819. position the window from a .gvimrc file: >
  820. :autocmd GUIEnter * winpos 100 50
  821. < *GUIFailed*
  822. GUIFailed After starting the GUI failed. Vim may
  823. continue to run in the terminal, if possible
  824. (only on Unix and alikes, when connecting the
  825. X server fails). You may want to quit Vim: >
  826. :autocmd GUIFailed * qall
  827. < *InsertChange*
  828. InsertChange When typing <Insert> while in Insert or
  829. Replace mode. The |v:insertmode| variable
  830. indicates the new mode.
  831. Be careful not to move the cursor or do
  832. anything else that the user does not expect.
  833. *InsertCharPre*
  834. InsertCharPre When a character is typed in Insert mode,
  835. before inserting the char.
  836. The |v:char| variable indicates the char typed
  837. and can be changed during the event to insert
  838. a different character. When |v:char| is set
  839. to more than one character this text is
  840. inserted literally.
  841. It is not allowed to change the text |textlock|.
  842. The event is not triggered when 'paste' is
  843. set. {only with the +eval feature}
  844. *InsertEnter*
  845. InsertEnter Just before starting Insert mode. Also for
  846. Replace mode and Virtual Replace mode. The
  847. |v:insertmode| variable indicates the mode.
  848. Be careful not to do anything else that the
  849. user does not expect.
  850. The cursor is restored afterwards. If you do
  851. not want that set |v:char| to a non-empty
  852. string.
  853. *InsertLeavePre*
  854. InsertLeavePre Just before leaving Insert mode. Also when
  855. using CTRL-O |i_CTRL-O|. Be careful not to
  856. change mode or use `:normal`, it will likely
  857. cause trouble.
  858. *InsertLeave*
  859. InsertLeave Just after leaving Insert mode. Also when
  860. using CTRL-O |i_CTRL-O|. But not for |i_CTRL-C|.
  861. *MenuPopup*
  862. MenuPopup Just before showing the popup menu (under the
  863. right mouse button). Useful for adjusting the
  864. menu for what is under the cursor or mouse
  865. pointer.
  866. The pattern is matched against one or two
  867. characters representing the mode:
  868. n Normal
  869. v Visual
  870. o Operator-pending
  871. i Insert
  872. c Command line
  873. tl Terminal
  874. *ModeChanged*
  875. ModeChanged After changing the mode. The pattern is
  876. matched against `'old_mode:new_mode'`, for
  877. example match against `*:c*` to simulate
  878. |CmdlineEnter|.
  879. The following values of |v:event| are set:
  880. old_mode The mode before it changed.
  881. new_mode The new mode as also returned
  882. by |mode()| called with a
  883. non-zero argument.
  884. When ModeChanged is triggered, old_mode will
  885. have the value of new_mode when the event was
  886. last triggered.
  887. This will be triggered on every minor mode
  888. change.
  889. Usage example to use relative line numbers
  890. when entering Visual mode: >
  891. :au ModeChanged [vV\x16]*:* let &l:rnu = mode() =~# '^[vV\x16]'
  892. :au ModeChanged *:[vV\x16]* let &l:rnu = mode() =~# '^[vV\x16]'
  893. :au WinEnter,WinLeave * let &l:rnu = mode() =~# '^[vV\x16]'
  894. < *OptionSet*
  895. OptionSet After setting an option. The pattern is
  896. matched against the long option name.
  897. |<amatch>| indicates what option has been set.
  898. |v:option_type| indicates whether it's global
  899. or local scoped.
  900. |v:option_command| indicates what type of
  901. set/let command was used (follow the tag to
  902. see the table).
  903. |v:option_new| indicates the newly set value.
  904. |v:option_oldlocal| has the old local value.
  905. |v:option_oldglobal| has the old global value.
  906. |v:option_old| indicates the old option value.
  907. |v:option_oldlocal| is only set when |:set|
  908. or |:setlocal| or a |modeline| was used to set
  909. the option. Similarly |v:option_oldglobal| is
  910. only set when |:set| or |:setglobal| was used.
  911. Note that when setting a |global-local| string
  912. option with |:set|, then |v:option_old| is the
  913. old global value. However, for all other kinds
  914. of options (local string options, global-local
  915. number options, ...) it is the old local
  916. value.
  917. OptionSet is not triggered on startup and for
  918. the 'key' option for obvious reasons.
  919. Usage example: Check for the existence of the
  920. directory in the 'backupdir' and 'undodir'
  921. options, create the directory if it doesn't
  922. exist yet.
  923. Note: It's a bad idea to reset an option
  924. during this autocommand, this may break a
  925. plugin. You can always use `:noa` to prevent
  926. triggering this autocommand.
  927. When using |:set| in the autocommand the event
  928. is not triggered again.
  929. *QuickFixCmdPre*
  930. QuickFixCmdPre Before a quickfix command is run (|:make|,
  931. |:lmake|, |:grep|, |:lgrep|, |:grepadd|,
  932. |:lgrepadd|, |:vimgrep|, |:lvimgrep|,
  933. |:vimgrepadd|, |:lvimgrepadd|, |:cscope|,
  934. |:cfile|, |:cgetfile|, |:caddfile|, |:lfile|,
  935. |:lgetfile|, |:laddfile|, |:helpgrep|,
  936. |:lhelpgrep|, |:cexpr|, |:cgetexpr|,
  937. |:caddexpr|, |:cbuffer|, |:cgetbuffer|,
  938. |:caddbuffer|).
  939. The pattern is matched against the command
  940. being run. When |:grep| is used but 'grepprg'
  941. is set to "internal" it still matches "grep".
  942. This command cannot be used to set the
  943. 'makeprg' and 'grepprg' variables.
  944. If this command causes an error, the quickfix
  945. command is not executed.
  946. *QuickFixCmdPost*
  947. QuickFixCmdPost Like QuickFixCmdPre, but after a quickfix
  948. command is run, before jumping to the first
  949. location. For |:cfile| and |:lfile| commands
  950. it is run after the error file is read and
  951. before moving to the first error.
  952. See |QuickFixCmdPost-example|.
  953. *QuitPre*
  954. QuitPre When using `:quit`, `:wq` or `:qall`, before
  955. deciding whether it closes the current window
  956. or quits Vim. For `:wq` the buffer is written
  957. before QuitPre is triggered. Can be used to
  958. close any non-essential window if the current
  959. window is the last ordinary window.
  960. Also see |ExitPre|.
  961. *RemoteReply*
  962. RemoteReply When a reply from a Vim that functions as
  963. server was received |server2client()|. The
  964. pattern is matched against the {serverid}.
  965. <amatch> is equal to the {serverid} from which
  966. the reply was sent, and <afile> is the actual
  967. reply string.
  968. Note that even if an autocommand is defined,
  969. the reply should be read with |remote_read()|
  970. to consume it.
  971. *SafeState*
  972. SafeState When nothing is pending, going to wait for the
  973. user to type a character.
  974. This will not be triggered when:
  975. - an operator is pending
  976. - a register was entered with "r
  977. - halfway executing a command
  978. - executing a mapping
  979. - there is typeahead
  980. - Insert mode completion is active
  981. - Command line completion is active
  982. You can use `mode()` to find out what state
  983. Vim is in. That may be:
  984. - VIsual mode
  985. - Normal mode
  986. - Insert mode
  987. - Command-line mode
  988. Depending on what you want to do, you may also
  989. check more with `state()`, e.g. whether the
  990. screen was scrolled for messages.
  991. *SafeStateAgain*
  992. SafeStateAgain Like SafeState but after processing any
  993. messages and invoking callbacks. This may be
  994. triggered often, don't do something that takes
  995. time.
  996. *SessionLoadPost*
  997. SessionLoadPost After loading the session file created using
  998. the |:mksession| command.
  999. *ShellCmdPost*
  1000. ShellCmdPost After executing a shell command with |:!cmd|,
  1001. |:shell|, |:make| and |:grep|. Can be used to
  1002. check for any changed files.
  1003. *ShellFilterPost*
  1004. ShellFilterPost After executing a shell command with
  1005. ":{range}!cmd", ":w !cmd" or ":r !cmd".
  1006. Can be used to check for any changed files.
  1007. *SourcePre*
  1008. SourcePre Before sourcing a Vim script. |:source|
  1009. <afile> is the name of the file being sourced.
  1010. *SourcePost*
  1011. SourcePost After sourcing a Vim script. |:source|
  1012. <afile> is the name of the file being sourced.
  1013. Not triggered when sourcing was interrupted.
  1014. Also triggered after a SourceCmd autocommand
  1015. was triggered.
  1016. *SourceCmd*
  1017. SourceCmd When sourcing a Vim script. |:source|
  1018. <afile> is the name of the file being sourced.
  1019. The autocommand must source this file.
  1020. |Cmd-event|
  1021. *SpellFileMissing*
  1022. SpellFileMissing When trying to load a spell checking file and
  1023. it can't be found. The pattern is matched
  1024. against the language. <amatch> is the
  1025. language, 'encoding' also matters. See
  1026. |spell-SpellFileMissing|.
  1027. *StdinReadPost*
  1028. StdinReadPost After reading from the stdin into the buffer,
  1029. before executing the modelines. Only used
  1030. when the "-" argument was used when Vim was
  1031. started |--|.
  1032. *StdinReadPre*
  1033. StdinReadPre Before reading from stdin into the buffer.
  1034. Only used when the "-" argument was used when
  1035. Vim was started |--|.
  1036. *SwapExists*
  1037. SwapExists Detected an existing swap file when starting
  1038. to edit a file. Only when it is possible to
  1039. select a way to handle the situation, when Vim
  1040. would ask the user what to do.
  1041. The |v:swapname| variable holds the name of
  1042. the swap file found, <afile> the file being
  1043. edited. |v:swapcommand| may contain a command
  1044. to be executed in the opened file.
  1045. The commands should set the |v:swapchoice|
  1046. variable to a string with one character to
  1047. tell Vim what should be done next:
  1048. 'o' open read-only
  1049. 'e' edit the file anyway
  1050. 'r' recover
  1051. 'd' delete the swap file
  1052. 'q' quit, don't edit the file
  1053. 'a' abort, like hitting CTRL-C
  1054. When set to an empty string the user will be
  1055. asked, as if there was no SwapExists autocmd.
  1056. *E812*
  1057. It is not allowed to change to another buffer,
  1058. change a buffer name or change directory
  1059. here.
  1060. {only available with the +eval feature}
  1061. *Syntax*
  1062. Syntax When the 'syntax' option has been set. The
  1063. pattern is matched against the syntax name.
  1064. <afile> can be used for the name of the file
  1065. where this option was set, and <amatch> for
  1066. the new value of 'syntax'.
  1067. See |:syn-on|.
  1068. *TabClosed*
  1069. TabClosed After closing a tab page.
  1070. *TabEnter*
  1071. TabEnter Just after entering a tab page. |tab-page|
  1072. After triggering the WinEnter and before
  1073. triggering the BufEnter event.
  1074. *TabLeave*
  1075. TabLeave Just before leaving a tab page. |tab-page|
  1076. A WinLeave event will have been triggered
  1077. first.
  1078. *TabNew*
  1079. TabNew When a tab page was created. |tab-page|
  1080. A WinEnter event will have been triggered
  1081. first, TabEnter follows.
  1082. *TermChanged*
  1083. TermChanged After the value of 'term' has changed. Useful
  1084. for re-loading the syntax file to update the
  1085. colors, fonts and other terminal-dependent
  1086. settings. Executed for all loaded buffers.
  1087. *TerminalOpen*
  1088. TerminalOpen Just after a terminal buffer was created, with
  1089. `:terminal` or |term_start()|. This event is
  1090. triggered even if the buffer is created
  1091. without a window, with the ++hidden option.
  1092. *TerminalWinOpen*
  1093. TerminalWinOpen Just after a terminal buffer was created, with
  1094. `:terminal` or |term_start()|. This event is
  1095. triggered only if the buffer is created
  1096. with a window. Can be used to set window
  1097. local options for the terminal window.
  1098. *TermResponse*
  1099. TermResponse After the response to |t_RV| is received from
  1100. the terminal. The value of |v:termresponse|
  1101. can be used to do things depending on the
  1102. terminal version. Note that this event may be
  1103. triggered halfway executing another event,
  1104. especially if file I/O, a shell command or
  1105. anything else that takes time is involved.
  1106. *TextChanged*
  1107. TextChanged After a change was made to the text in the
  1108. current buffer in Normal mode. That is after
  1109. |b:changedtick| has changed (also when that
  1110. happened before the TextChanged autocommand
  1111. was defined).
  1112. Not triggered when there is typeahead or when
  1113. an operator is pending.
  1114. Note: This can not be skipped with
  1115. `:noautocmd`.
  1116. Careful: This is triggered very often, don't
  1117. do anything that the user does not expect or
  1118. that is slow.
  1119. *TextChangedI*
  1120. TextChangedI After a change was made to the text in the
  1121. current buffer in Insert mode.
  1122. Not triggered when the popup menu is visible.
  1123. Otherwise the same as TextChanged.
  1124. *TextChangedP*
  1125. TextChangedP After a change was made to the text in the
  1126. current buffer in Insert mode, only when the
  1127. popup menu is visible. Otherwise the same as
  1128. TextChanged.
  1129. *TextChangedT*
  1130. TextChangedT After a change was made to the text in the
  1131. current buffer in Terminal mode.
  1132. Otherwise the same as TextChanged.
  1133. *TextYankPost*
  1134. TextYankPost After text has been yanked or deleted in the
  1135. current buffer. The following values of
  1136. |v:event| can be used to determine the operation
  1137. that triggered this autocmd:
  1138. inclusive TRUE if the motion is
  1139. |inclusive| else the motion is
  1140. |exclusive|.
  1141. operator The operation performed.
  1142. regcontents Text that was stored in the
  1143. register, as a list of lines,
  1144. like with: >
  1145. getreg(r, 1, 1)
  1146. < regname Name of the register or empty
  1147. string for the unnamed
  1148. register, see |registers|.
  1149. regtype Type of the register, see
  1150. |getregtype()|.
  1151. visual True if the operation is
  1152. performed on a |Visual| area.
  1153. Not triggered when |quote_| is used nor when
  1154. called recursively.
  1155. It is not allowed to change the buffer text,
  1156. see |textlock|. *E1064*
  1157. {only when compiled with the +eval feature}
  1158. *User*
  1159. User Never executed automatically. To be used for
  1160. autocommands that are only executed with
  1161. ":doautocmd".
  1162. Note that when `:doautocmd User MyEvent` is
  1163. used while there are no matching autocommands,
  1164. you will get an error. If you don't want
  1165. that, either check whether an autocommand is
  1166. defined using `exists('#User#MyEvent')` or
  1167. define a dummy autocommand yourself.
  1168. Example: >
  1169. if exists('#User#MyEvent')
  1170. doautocmd User MyEvent
  1171. endif
  1172. <
  1173. *SigUSR1*
  1174. SigUSR1 After the SIGUSR1 signal has been detected.
  1175. Could be used if other ways of notifying Vim
  1176. are not feasible. E.g. to check for the
  1177. result of a build that takes a long time, or
  1178. when a motion sensor is triggered.
  1179. {only on Unix}
  1180. *UserGettingBored*
  1181. UserGettingBored When the user presses the same key 42 times.
  1182. Just kidding! :-)
  1183. *VimEnter*
  1184. VimEnter After doing all the startup stuff, including
  1185. loading .vimrc files, executing the "-c cmd"
  1186. arguments, creating all windows and loading
  1187. the buffers in them.
  1188. Just before this event is triggered the
  1189. |v:vim_did_enter| variable is set, so that you
  1190. can do: >
  1191. if v:vim_did_enter
  1192. call s:init()
  1193. else
  1194. au VimEnter * call s:init()
  1195. endif
  1196. < *VimLeave*
  1197. VimLeave Before exiting Vim, just after writing the
  1198. .viminfo file. Executed only once, like
  1199. VimLeavePre.
  1200. To detect an abnormal exit use |v:dying|.
  1201. When v:dying is 2 or more this event is not
  1202. triggered.
  1203. To get the exit code use |v:exiting|.
  1204. *VimLeavePre*
  1205. VimLeavePre Before exiting Vim, just before writing the
  1206. .viminfo file. This is executed only once,
  1207. if there is a match with the name of what
  1208. happens to be the current buffer when exiting.
  1209. Mostly useful with a "*" pattern. >
  1210. :autocmd VimLeavePre * call CleanupStuff()
  1211. < To detect an abnormal exit use |v:dying|.
  1212. When v:dying is 2 or more this event is not
  1213. triggered.
  1214. To get the exit code use |v:exiting|.
  1215. *VimResized*
  1216. VimResized After the Vim window was resized, thus 'lines'
  1217. and/or 'columns' changed. Not when starting
  1218. up though.
  1219. *VimResume*
  1220. VimResume When the Vim instance is resumed after being
  1221. suspended and |VimSuspend| was triggered.
  1222. Useful for triggering |:checktime| and ensure
  1223. the buffers content did not change while Vim
  1224. was suspended: >
  1225. :autocmd VimResume * checktime
  1226. < *VimSuspend*
  1227. VimSuspend When the Vim instance is suspended. Only when
  1228. CTRL-Z was typed inside Vim, or when the SIGTSTP
  1229. signal was sent to Vim, but not for SIGSTOP.
  1230. *WinClosed*
  1231. WinClosed When closing a window, just before it is
  1232. removed from the window layout. The pattern
  1233. is matched against the |window-ID|. Both
  1234. <amatch> and <afile> are set to the
  1235. |window-ID|. Non-recursive (event cannot
  1236. trigger itself).
  1237. *WinEnter*
  1238. WinEnter After entering another window. Not done for
  1239. the first window, when Vim has just started.
  1240. Useful for setting the window height.
  1241. If the window is for another buffer, Vim
  1242. executes the BufEnter autocommands after the
  1243. WinEnter autocommands.
  1244. Note: For split and tabpage commands the
  1245. WinEnter event is triggered after the split
  1246. or tab command but before the file is loaded.
  1247. *WinLeave*
  1248. WinLeave Before leaving a window. If the window to be
  1249. entered next is for a different buffer, Vim
  1250. executes the BufLeave autocommands before the
  1251. WinLeave autocommands (but not for ":new").
  1252. Not used for ":qa" or ":q" when exiting Vim.
  1253. *WinNew*
  1254. WinNew When a new window was created. Not done for
  1255. the first window, when Vim has just started.
  1256. Before a WinEnter event.
  1257. *WinScrolled*
  1258. WinScrolled After any window in the current tab page
  1259. scrolled the text (horizontally or vertically)
  1260. or changed width or height. See
  1261. |win-scrolled-resized|.
  1262. The pattern is matched against the |window-ID|
  1263. of the first window that scrolled or resized.
  1264. Both <amatch> and <afile> are set to the
  1265. |window-ID|.
  1266. |v:event| is set with information about size
  1267. and scroll changes. |WinScrolled-event|
  1268. Only starts triggering after startup finished
  1269. and the first screen redraw was done.
  1270. Does not trigger when defining the first
  1271. WinScrolled or WinResized event, but may
  1272. trigger when adding more.
  1273. Non-recursive: the event will not trigger
  1274. while executing commands for the WinScrolled
  1275. event. However, if the command causes a
  1276. window to scroll or change size, then another
  1277. WinScrolled event will be triggered later.
  1278. *WinResized*
  1279. WinResized After a window in the current tab page changed
  1280. width or height.
  1281. See |win-scrolled-resized|.
  1282. |v:event| is set with information about size
  1283. changes. |WinResized-event|
  1284. Same behavior as |WinScrolled| for the
  1285. pattern, triggering and recursiveness.
  1286. ==============================================================================
  1287. 6. Patterns *autocmd-patterns* *{aupat}*
  1288. The {aupat} argument of `:autocmd` can be a comma-separated list. This works as
  1289. if the command was given with each pattern separately. Thus this command: >
  1290. :autocmd BufRead *.txt,*.info set et
  1291. Is equivalent to: >
  1292. :autocmd BufRead *.txt set et
  1293. :autocmd BufRead *.info set et
  1294. The file pattern {aupat} is tested for a match against the file name in one of
  1295. two ways:
  1296. 1. When there is no '/' in the pattern, Vim checks for a match against only
  1297. the tail part of the file name (without its leading directory path).
  1298. 2. When there is a '/' in the pattern, Vim checks for a match against both the
  1299. short file name (as you typed it) and the full file name (after expanding
  1300. it to a full path and resolving symbolic links).
  1301. The special pattern <buffer> or <buffer=N> is used for buffer-local
  1302. autocommands |autocmd-buflocal|. This pattern is not matched against the name
  1303. of a buffer.
  1304. Examples: >
  1305. :autocmd BufRead *.txt set et
  1306. Set the 'et' option for all text files. >
  1307. :autocmd BufRead /vim/src/*.c set cindent
  1308. Set the 'cindent' option for C files in the /vim/src directory. >
  1309. :autocmd BufRead /tmp/*.c set ts=5
  1310. If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", and
  1311. you start editing "/tmp/test.c", this autocommand will match.
  1312. Note: To match part of a path, but not from the root directory, use a '*' as
  1313. the first character. Example: >
  1314. :autocmd BufRead */doc/*.txt set tw=78
  1315. This autocommand will for example be executed for "/tmp/doc/xx.txt" and
  1316. "/usr/home/piet/doc/yy.txt". The number of directories does not matter here.
  1317. The file name that the pattern is matched against is after expanding
  1318. wildcards. Thus if you issue this command: >
  1319. :e $ROOTDIR/main.$EXT
  1320. The argument is first expanded to: >
  1321. /usr/root/main.py
  1322. Before it's matched with the pattern of the autocommand. Careful with this
  1323. when using events like FileReadCmd, the value of <amatch> may not be what you
  1324. expect.
  1325. Environment variables can be used in a pattern: >
  1326. :autocmd BufRead $VIMRUNTIME/doc/*.txt set expandtab
  1327. And ~ can be used for the home directory (if $HOME is defined): >
  1328. :autocmd BufWritePost ~/.vimrc so ~/.vimrc
  1329. :autocmd BufRead ~archive/* set readonly
  1330. The environment variable is expanded when the autocommand is defined, not when
  1331. the autocommand is executed. This is different from the command!
  1332. *file-pattern*
  1333. The pattern is interpreted like mostly used in file names:
  1334. * matches any sequence of characters; Unusual: includes path
  1335. separators
  1336. ? matches any single character
  1337. \? matches a '?'
  1338. . matches a '.'
  1339. ~ matches a '~'
  1340. , separates patterns
  1341. \, matches a ','
  1342. { } like \( \) in a |pattern|
  1343. , inside { }: like \| in a |pattern|
  1344. \} literal }
  1345. \{ literal {
  1346. \\\{n,m\} like \{n,m} in a |pattern|
  1347. \ special meaning like in a |pattern|
  1348. [ch] matches 'c' or 'h'
  1349. [^ch] match any character but 'c' and 'h'
  1350. Note that for all systems the '/' character is used for path separator (even
  1351. for MS-Windows). This was done because the backslash is difficult to use in a
  1352. pattern and to make the autocommands portable across different systems.
  1353. It is possible to use |pattern| items, but they may not work as expected,
  1354. because of the translation done for the above.
  1355. *autocmd-changes*
  1356. Matching with the pattern is done when an event is triggered. Changing the
  1357. buffer name in one of the autocommands, or even deleting the buffer, does not
  1358. change which autocommands will be executed. Example: >
  1359. au BufEnter *.foo bdel
  1360. au BufEnter *.foo set modified
  1361. This will delete the current buffer and then set 'modified' in what has become
  1362. the current buffer instead. Vim doesn't take into account that "*.foo"
  1363. doesn't match with that buffer name. It matches "*.foo" with the name of the
  1364. buffer at the moment the event was triggered.
  1365. However, buffer-local autocommands will not be executed for a buffer that has
  1366. been wiped out with |:bwipe|. After deleting the buffer with |:bdel| the
  1367. buffer actually still exists (it becomes unlisted), thus the autocommands are
  1368. still executed.
  1369. ==============================================================================
  1370. 7. Buffer-local autocommands *autocmd-buflocal* *autocmd-buffer-local*
  1371. *<buffer=N>* *<buffer=abuf>* *E680*
  1372. Buffer-local autocommands are attached to a specific buffer. They are useful
  1373. if the buffer does not have a name and when the name does not match a specific
  1374. pattern. But it also means they must be explicitly added to each buffer.
  1375. Instead of a pattern buffer-local autocommands use one of these forms:
  1376. <buffer> current buffer
  1377. <buffer=99> buffer number 99
  1378. <buffer=abuf> using <abuf> (only when executing autocommands)
  1379. |<abuf>|
  1380. Examples: >
  1381. :au CursorHold <buffer> echo 'hold'
  1382. :au CursorHold <buffer=33> echo 'hold'
  1383. :au BufNewFile * au CursorHold <buffer=abuf> echo 'hold'
  1384. All the commands for autocommands also work with buffer-local autocommands,
  1385. simply use the special string instead of the pattern. Examples: >
  1386. :au! * <buffer> " remove buffer-local autocommands for
  1387. " current buffer
  1388. :au! * <buffer=33> " remove buffer-local autocommands for
  1389. " buffer #33
  1390. :bufdo :au! CursorHold <buffer> " remove autocmd for given event for all
  1391. " buffers
  1392. :au * <buffer> " list buffer-local autocommands for
  1393. " current buffer
  1394. Note that when an autocommand is defined for the current buffer, it is stored
  1395. with the buffer number. Thus it uses the form "<buffer=12>", where 12 is the
  1396. number of the current buffer. You will see this when listing autocommands,
  1397. for example.
  1398. To test for presence of buffer-local autocommands use the |exists()| function
  1399. as follows: >
  1400. :if exists("#CursorHold#<buffer=12>") | ... | endif
  1401. :if exists("#CursorHold#<buffer>") | ... | endif " for current buffer
  1402. When a buffer is wiped out its buffer-local autocommands are also gone, of
  1403. course. Note that when deleting a buffer, e.g., with ":bdel", it is only
  1404. unlisted, the autocommands are still present. In order to see the removal of
  1405. buffer-local autocommands: >
  1406. :set verbose=6
  1407. It is not possible to define buffer-local autocommands for a non-existent
  1408. buffer.
  1409. ==============================================================================
  1410. 8. Groups *autocmd-groups*
  1411. Autocommands can be put together in a group. This is useful for removing or
  1412. executing a group of autocommands. For example, all the autocommands for
  1413. syntax highlighting are put in the "highlight" group, to be able to execute
  1414. ":doautoall highlight BufRead" when the GUI starts.
  1415. When no specific group is selected, Vim uses the default group. The default
  1416. group does not have a name. You cannot execute the autocommands from the
  1417. default group separately; you can execute them only by executing autocommands
  1418. for all groups.
  1419. Normally, when executing autocommands automatically, Vim uses the autocommands
  1420. for all groups. The group only matters when executing autocommands with
  1421. ":doautocmd" or ":doautoall", or when defining or deleting autocommands.
  1422. The group name can contain any characters except white space. The group name
  1423. "end" is reserved (also in uppercase).
  1424. The group name is case sensitive. Note that this is different from the event
  1425. name!
  1426. *:aug* *:augroup*
  1427. :aug[roup] {name} Define the autocmd group name for the
  1428. following ":autocmd" commands. The name "end"
  1429. or "END" selects the default group.
  1430. To avoid confusion, the name should be
  1431. different from existing {event} names, as this
  1432. most likely will not do what you intended.
  1433. *:augroup-delete* *E367* *W19* *E936*
  1434. :aug[roup]! {name} Delete the autocmd group {name}. Don't use
  1435. this if there is still an autocommand using
  1436. this group! You will get a warning if doing
  1437. it anyway. When the group is the current
  1438. group you will get error E936.
  1439. To enter autocommands for a specific group, use this method:
  1440. 1. Select the group with ":augroup {name}".
  1441. 2. Delete any old autocommands with ":au!".
  1442. 3. Define the autocommands.
  1443. 4. Go back to the default group with "augroup END".
  1444. Example: >
  1445. :augroup uncompress
  1446. : au!
  1447. : au BufEnter *.gz %!gunzip
  1448. :augroup END
  1449. This prevents having the autocommands defined twice (e.g., after sourcing the
  1450. .vimrc file again).
  1451. *FileExplorer*
  1452. There is one group that is recognized by Vim: FileExplorer. If this group
  1453. exists Vim assumes that editing a directory is possible and will trigger a
  1454. plugin that lists the files in that directory. This is used by the |netrw|
  1455. plugin. This allows you to do: >
  1456. browse edit
  1457. ==============================================================================
  1458. 9. Executing autocommands *autocmd-execute*
  1459. Vim can also execute Autocommands non-automatically. This is useful if you
  1460. have changed autocommands, or when Vim has executed the wrong autocommands
  1461. (e.g., the file pattern match was wrong).
  1462. Note that the 'eventignore' option applies here too. Events listed in this
  1463. option will not cause any commands to be executed.
  1464. *:do* *:doau* *:doaut* *:doautocmd* *E217*
  1465. :do[autocmd] [<nomodeline>] [group] {event} [fname]
  1466. Apply the autocommands matching [fname] (default:
  1467. current file name) for {event} to the current buffer.
  1468. You can use this when the current file name does not
  1469. match the right pattern, after changing settings, or
  1470. to execute autocommands for a certain event.
  1471. It's possible to use this inside an autocommand too,
  1472. so you can base the autocommands for one extension on
  1473. another extension. Example: >
  1474. :au BufEnter *.cpp so ~/.vimrc_cpp
  1475. :au BufEnter *.cpp doau BufEnter x.c
  1476. < Be careful to avoid endless loops. See
  1477. |autocmd-nested|.
  1478. When the [group] argument is not given, Vim executes
  1479. the autocommands for all groups. When the [group]
  1480. argument is included, Vim executes only the matching
  1481. autocommands for that group. Note: if you use an
  1482. undefined group name, Vim gives you an error message.
  1483. *<nomodeline>*
  1484. After applying the autocommands the modelines are
  1485. processed, so that their settings overrule the
  1486. settings from autocommands, like what happens when
  1487. editing a file. This is skipped when the <nomodeline>
  1488. argument is present. You probably want to use
  1489. <nomodeline> for events that are not used when loading
  1490. a buffer, such as |User|.
  1491. Processing modelines is also skipped when no
  1492. matching autocommands were executed.
  1493. *:doautoa* *:doautoall*
  1494. :doautoa[ll] [<nomodeline>] [group] {event} [fname]
  1495. Like ":doautocmd", but apply the autocommands to each
  1496. loaded buffer. The current buffer is done last.
  1497. Note that [fname] is used to select the autocommands,
  1498. not the buffers to which they are applied. Example: >
  1499. augroup mine
  1500. autocmd!
  1501. autocmd FileType * echo expand('<amatch>')
  1502. augroup END
  1503. doautoall mine FileType Loaded-Buffer
  1504. < Sourcing this script, you'll see as many
  1505. "Loaded-Buffer" echoed as there are loaded buffers.
  1506. Careful: Don't use this for autocommands that delete a
  1507. buffer, change to another buffer or change the
  1508. contents of a buffer; the result is unpredictable.
  1509. This command is intended for autocommands that set
  1510. options, change highlighting, and things like that.
  1511. ==============================================================================
  1512. 10. Using autocommands *autocmd-use*
  1513. For WRITING FILES there are four possible sets of events. Vim uses only one
  1514. of these sets for a write command:
  1515. BufWriteCmd BufWritePre BufWritePost writing the whole buffer
  1516. FilterWritePre FilterWritePost writing to filter temp file
  1517. FileAppendCmd FileAppendPre FileAppendPost appending to a file
  1518. FileWriteCmd FileWritePre FileWritePost any other file write
  1519. When there is a matching "*Cmd" autocommand, it is assumed it will do the
  1520. writing. No further writing is done and the other events are not triggered.
  1521. |Cmd-event|
  1522. Note that the *WritePost commands should undo any changes to the buffer that
  1523. were caused by the *WritePre commands; otherwise, writing the file will have
  1524. the side effect of changing the buffer.
  1525. Before executing the autocommands, the buffer from which the lines are to be
  1526. written temporarily becomes the current buffer. Unless the autocommands
  1527. change the current buffer or delete the previously current buffer, the
  1528. previously current buffer is made the current buffer again.
  1529. The *WritePre and *AppendPre autocommands must not delete the buffer from
  1530. which the lines are to be written.
  1531. The '[ and '] marks have a special position:
  1532. - Before the *ReadPre event the '[ mark is set to the line just above where
  1533. the new lines will be inserted.
  1534. - Before the *ReadPost event the '[ mark is set to the first line that was
  1535. just read, the '] mark to the last line.
  1536. - Before executing the *WriteCmd, *WritePre and *AppendPre autocommands the '[
  1537. mark is set to the first line that will be written, the '] mark to the last
  1538. line.
  1539. Careful: '[ and '] change when using commands that change the buffer.
  1540. In commands which expect a file name, you can use "<afile>" for the file name
  1541. that is being read |:<afile>| (you can also use "%" for the current file
  1542. name). "<abuf>" can be used for the buffer number of the currently effective
  1543. buffer. This also works for buffers that don't have a name. But it doesn't
  1544. work for files without a buffer (e.g., with ":r file").
  1545. *gzip-example*
  1546. Examples for reading and writing compressed files: >
  1547. :augroup gzip
  1548. : autocmd!
  1549. : autocmd BufReadPre,FileReadPre *.gz set bin
  1550. : autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip
  1551. : autocmd BufReadPost,FileReadPost *.gz set nobin
  1552. : autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " .. expand("%:r")
  1553. : autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r
  1554. : autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r
  1555. : autocmd FileAppendPre *.gz !gunzip <afile>
  1556. : autocmd FileAppendPre *.gz !mv <afile>:r <afile>
  1557. : autocmd FileAppendPost *.gz !mv <afile> <afile>:r
  1558. : autocmd FileAppendPost *.gz !gzip <afile>:r
  1559. :augroup END
  1560. The "gzip" group is used to be able to delete any existing autocommands with
  1561. ":autocmd!", for when the file is sourced twice.
  1562. ("<afile>:r" is the file name without the extension, see |:_%:|)
  1563. The commands executed for the BufNewFile, BufRead/BufReadPost, BufWritePost,
  1564. FileAppendPost and VimLeave events do not set or reset the changed flag of the
  1565. buffer. When you decompress the buffer with the BufReadPost autocommands, you
  1566. can still exit with ":q". When you use ":undo" in BufWritePost to undo the
  1567. changes made by BufWritePre commands, you can still do ":q" (this also makes
  1568. "ZZ" work). If you do want the buffer to be marked as modified, set the
  1569. 'modified' option.
  1570. To execute Normal mode commands from an autocommand, use the ":normal"
  1571. command. Use with care! If the Normal mode command is not finished, the user
  1572. needs to type characters (e.g., after ":normal m" you need to type a mark
  1573. name).
  1574. If you want the buffer to be unmodified after changing it, reset the
  1575. 'modified' option. This makes it possible to exit the buffer with ":q"
  1576. instead of ":q!".
  1577. *autocmd-nested* *E218*
  1578. By default, autocommands do not nest. For example, if you use ":e" or ":w" in
  1579. an autocommand, Vim does not execute the BufRead and BufWrite autocommands for
  1580. those commands. If you do want this, use the "nested" flag for those commands
  1581. in which you want nesting. For example: >
  1582. :autocmd FileChangedShell *.c ++nested e!
  1583. The nesting is limited to 10 levels to get out of recursive loops.
  1584. It's possible to use the ":au" command in an autocommand. This can be a
  1585. self-modifying command! This can be useful for an autocommand that should
  1586. execute only once.
  1587. If you want to skip autocommands for one command, use the |:noautocmd| command
  1588. modifier or the 'eventignore' option.
  1589. Note: When reading a file (with ":read file" or with a filter command) and the
  1590. last line in the file does not have an <EOL>, Vim remembers this. At the next
  1591. write (with ":write file" or with a filter command), if the same line is
  1592. written again as the last line in a file AND 'binary' is set, Vim does not
  1593. supply an <EOL>. This makes a filter command on the just read lines write the
  1594. same file as was read, and makes a write command on just filtered lines write
  1595. the same file as was read from the filter. For example, another way to write
  1596. a compressed file: >
  1597. :autocmd FileWritePre *.gz set bin|'[,']!gzip
  1598. :autocmd FileWritePost *.gz undo|set nobin
  1599. <
  1600. *autocommand-pattern*
  1601. You can specify multiple patterns, separated by commas. Here are some
  1602. examples: >
  1603. :autocmd BufRead * set tw=79 nocin ic infercase fo=2croq
  1604. :autocmd BufRead .letter set tw=72 fo=2tcrq
  1605. :autocmd BufEnter .letter set dict=/usr/lib/dict/words
  1606. :autocmd BufLeave .letter set dict=
  1607. :autocmd BufRead,BufNewFile *.c,*.h set tw=0 cin noic
  1608. :autocmd BufEnter *.c,*.h abbr FOR for (i = 0; i < 3; ++i)<CR>{<CR>}<Esc>O
  1609. :autocmd BufLeave *.c,*.h unabbr FOR
  1610. For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.): >
  1611. :autocmd BufEnter ?akefile* set include=^s\=include
  1612. :autocmd BufLeave ?akefile* set include&
  1613. To always start editing C files at the first function: >
  1614. :autocmd BufRead *.c,*.h 1;/^{
  1615. Without the "1;" above, the search would start from wherever the file was
  1616. entered, rather than from the start of the file.
  1617. *skeleton* *template*
  1618. To read a skeleton (template) file when opening a new file: >
  1619. :autocmd BufNewFile *.c 0r ~/vim/skeleton.c
  1620. :autocmd BufNewFile *.h 0r ~/vim/skeleton.h
  1621. :autocmd BufNewFile *.java 0r ~/vim/skeleton.java
  1622. To insert the current date and time in a *.html file when writing it: >
  1623. :autocmd BufWritePre,FileWritePre *.html ks|call LastMod()|'s
  1624. :fun LastMod()
  1625. : if line("$") > 20
  1626. : let l = 20
  1627. : else
  1628. : let l = line("$")
  1629. : endif
  1630. : exe "1," .. l .. "g/Last modified: /s/Last modified: .*/Last modified: " ..
  1631. : \ strftime("%Y %b %d")
  1632. :endfun
  1633. You need to have a line "Last modified: <date time>" in the first 20 lines
  1634. of the file for this to work. Vim replaces <date time> (and anything in the
  1635. same line after it) with the current date and time. Explanation:
  1636. ks mark current position with mark 's'
  1637. call LastMod() call the LastMod() function to do the work
  1638. 's return the cursor to the old position
  1639. The LastMod() function checks if the file is shorter than 20 lines, and then
  1640. uses the ":g" command to find lines that contain "Last modified: ". For those
  1641. lines the ":s" command is executed to replace the existing date with the
  1642. current one. The ":execute" command is used to be able to use an expression
  1643. for the ":g" and ":s" commands. The date is obtained with the strftime()
  1644. function. You can change its argument to get another date string.
  1645. When entering :autocmd on the command-line, completion of events and command
  1646. names may be done (with <Tab>, CTRL-D, etc.) where appropriate.
  1647. Vim executes all matching autocommands in the order that you specify them.
  1648. It is recommended that your first autocommand be used for all files by using
  1649. "*" as the file pattern. This means that you can define defaults you like
  1650. here for any settings, and if there is another matching autocommand it will
  1651. override these. But if there is no other matching autocommand, then at least
  1652. your default settings are recovered (if entering this file from another for
  1653. which autocommands did match). Note that "*" will also match files starting
  1654. with ".", unlike Unix shells.
  1655. *autocmd-searchpat*
  1656. Autocommands do not change the current search patterns. Vim saves the current
  1657. search patterns before executing autocommands then restores them after the
  1658. autocommands finish. This means that autocommands do not affect the strings
  1659. highlighted with the 'hlsearch' option. Within autocommands, you can still
  1660. use search patterns normally, e.g., with the "n" command.
  1661. If you want an autocommand to set the search pattern, such that it is used
  1662. after the autocommand finishes, use the ":let @/ =" command.
  1663. The search-highlighting cannot be switched off with ":nohlsearch" in an
  1664. autocommand. Use the 'h' flag in the 'viminfo' option to disable search-
  1665. highlighting when starting Vim.
  1666. *Cmd-event*
  1667. When using one of the "*Cmd" events, the matching autocommands are expected to
  1668. do the file reading, writing or sourcing. This can be used when working with
  1669. a special kind of file, for example on a remote system.
  1670. CAREFUL: If you use these events in a wrong way, it may have the effect of
  1671. making it impossible to read or write the matching files! Make sure you test
  1672. your autocommands properly. Best is to use a pattern that will never match a
  1673. normal file name, for example "ftp://*".
  1674. When defining a BufReadCmd it will be difficult for Vim to recover a crashed
  1675. editing session. When recovering from the original file, Vim reads only those
  1676. parts of a file that are not found in the swap file. Since that is not
  1677. possible with a BufReadCmd, use the |:preserve| command to make sure the
  1678. original file isn't needed for recovery. You might want to do this only when
  1679. you expect the file to be modified.
  1680. For file read and write commands the |v:cmdarg| variable holds the "++enc="
  1681. and "++ff=" argument that are effective. These should be used for the command
  1682. that reads/writes the file. The |v:cmdbang| variable is one when "!" was
  1683. used, zero otherwise.
  1684. See the $VIMRUNTIME/plugin/netrwPlugin.vim for examples.
  1685. ==============================================================================
  1686. 11. Disabling autocommands *autocmd-disable*
  1687. To disable autocommands for some time use the 'eventignore' option. Note that
  1688. this may cause unexpected behavior, make sure you restore 'eventignore'
  1689. afterwards, using a |:try| block with |:finally|.
  1690. *:noautocmd* *:noa*
  1691. To disable autocommands for just one command use the ":noautocmd" command
  1692. modifier. This will set 'eventignore' to "all" for the duration of the
  1693. following command. Example: >
  1694. :noautocmd w fname.gz
  1695. This will write the file without triggering the autocommands defined by the
  1696. gzip plugin.
  1697. Note that some autocommands are not triggered right away, but only later.
  1698. This specifically applies to |CursorMoved| and |TextChanged|.
  1699. vim:tw=78:ts=8:noet:ft=help:norl: