autocmd.txt 64 KB

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