blob: 978686a3aa5a4265a425029a4756f1391a23fb7b [file] [log] [blame]
Bram Moolenaar9048f942007-05-12 14:32:25 +00001*autocmd.txt* For Vim version 7.1. Last change: 2007 Mar 27
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Automatic commands *autocommand*
8
9For a basic explanation, see section |40.3| in the user manual.
10
111. Introduction |autocmd-intro|
122. Defining autocommands |autocmd-define|
133. Removing autocommands |autocmd-remove|
144. Listing autocommands |autocmd-list|
155. Events |autocmd-events|
166. Patterns |autocmd-patterns|
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000177. Buffer-local autocommands |autocmd-buflocal|
188. Groups |autocmd-groups|
199. Executing autocommands |autocmd-execute|
2010. Using autocommands |autocmd-use|
Bram Moolenaarb3480382005-12-11 21:33:32 +00002111. Disabling autocommands |autocmd-disable|
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
23{Vi does not have any of these commands}
24{only when the |+autocmd| feature has not been disabled at compile time}
25
26==============================================================================
271. Introduction *autocmd-intro*
28
Bram Moolenaard4755bb2004-09-02 19:12:26 +000029You can specify commands to be executed automatically when reading or writing
30a file, when entering or leaving a buffer or window, and when exiting Vim.
31For example, you can create an autocommand to set the 'cindent' option for
32files matching *.c. You can also use autocommands to implement advanced
Bram Moolenaar071d4272004-06-13 20:20:40 +000033features, such as editing compressed files (see |gzip-example|). The usual
34place to put autocommands is in your .vimrc or .exrc file.
35
36 *E203* *E204* *E143*
37WARNING: Using autocommands is very powerful, and may lead to unexpected side
38effects. Be careful not to destroy your text.
39- It's a good idea to do some testing on an expendable copy of a file first.
40 For example: If you use autocommands to decompress a file when starting to
41 edit it, make sure that the autocommands for compressing when writing work
42 correctly.
43- Be prepared for an error halfway through (e.g., disk full). Vim will mostly
44 be able to undo the changes to the buffer, but you may have to clean up the
45 changes to other files by hand (e.g., compress a file that has been
46 decompressed).
47- If the BufRead* events allow you to edit a compressed file, the FileRead*
48 events should do the same (this makes recovery possible in some rare cases).
49 It's a good idea to use the same autocommands for the File* and Buf* events
50 when possible.
51
52==============================================================================
532. Defining autocommands *autocmd-define*
54
55Note: The ":autocmd" command cannot be followed by another command, since any
56'|' is considered part of the command.
57
58 *:au* *:autocmd*
59:au[tocmd] [group] {event} {pat} [nested] {cmd}
60 Add {cmd} to the list of commands that Vim will
61 execute automatically on {event} for a file matching
62 {pat}. Vim always adds the {cmd} after existing
63 autocommands, so that the autocommands execute in the
64 order in which they were given. See |autocmd-nested|
65 for [nested].
66
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000067The special pattern <buffer> or <buffer=N> defines a buffer-local autocommand.
68See |autocmd-buflocal|.
69
Bram Moolenaar071d4272004-06-13 20:20:40 +000070Note that special characters (e.g., "%", "<cword>") in the ":autocmd"
71arguments are not expanded when the autocommand is defined. These will be
72expanded when the Event is recognized, and the {cmd} is executed. The only
73exception is that "<sfile>" is expanded when the autocmd is defined. Example:
74>
75 :au BufNewFile,BufRead *.html so <sfile>:h/html.vim
76
77Here Vim expands <sfile> to the name of the file containing this line.
78
79When your .vimrc file is sourced twice, the autocommands will appear twice.
80To avoid this, put this command in your .vimrc file, before defining
81autocommands: >
82
83 :autocmd! " Remove ALL autocommands for the current group.
84
85If you don't want to remove all autocommands, you can instead use a variable
86to ensure that Vim includes the autocommands only once: >
87
88 :if !exists("autocommands_loaded")
89 : let autocommands_loaded = 1
90 : au ...
91 :endif
92
93When the [group] argument is not given, Vim uses the current group (as defined
94with ":augroup"); otherwise, Vim uses the group defined with [group]. Note
95that [group] must have been defined before. You cannot define a new group
96with ":au group ..."; use ":augroup" for that.
97
98While testing autocommands, you might find the 'verbose' option to be useful: >
99 :set verbose=9
100This setting makes Vim echo the autocommands as it executes them.
101
102When defining an autocommand in a script, it will be able to call functions
103local to the script and use mappings local to the script. When the event is
104triggered and the command executed, it will run in the context of the script
105it was defined in. This matters if |<SID>| is used in a command.
106
107When executing the commands, the messages from one command overwrites a
108previous message. This is different from when executing the commands
109manually. Mostly the screen will not scroll up, thus there is no hit-enter
110prompt. When one command outputs two messages this can happen anyway.
111
112==============================================================================
1133. Removing autocommands *autocmd-remove*
114
115:au[tocmd]! [group] {event} {pat} [nested] {cmd}
116 Remove all autocommands associated with {event} and
117 {pat}, and add the command {cmd}. See
118 |autocmd-nested| for [nested].
119
120:au[tocmd]! [group] {event} {pat}
121 Remove all autocommands associated with {event} and
122 {pat}.
123
124:au[tocmd]! [group] * {pat}
125 Remove all autocommands associated with {pat} for all
126 events.
127
128:au[tocmd]! [group] {event}
129 Remove ALL autocommands for {event}.
130
131:au[tocmd]! [group] Remove ALL autocommands.
132
133When the [group] argument is not given, Vim uses the current group (as defined
134with ":augroup"); otherwise, Vim uses the group defined with [group].
135
136==============================================================================
1374. Listing autocommands *autocmd-list*
138
139:au[tocmd] [group] {event} {pat}
140 Show the autocommands associated with {event} and
141 {pat}.
142
143:au[tocmd] [group] * {pat}
144 Show the autocommands associated with {pat} for all
145 events.
146
147:au[tocmd] [group] {event}
148 Show all autocommands for {event}.
149
150:au[tocmd] [group] Show all autocommands.
151
152If you provide the [group] argument, Vim lists only the autocommands for
153[group]; otherwise, Vim lists the autocommands for ALL groups. Note that this
154argument behavior differs from that for defining and removing autocommands.
155
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000156In order to list buffer-local autocommands, use a pattern in the form <buffer>
157or <buffer=N>. See |autocmd-buflocal|.
158
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000159 *:autocmd-verbose*
160When 'verbose' is non-zero, listing an autocommand will also display where it
161was last defined. Example: >
162
163 :verbose autocmd BufEnter
164 FileExplorer BufEnter
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000165 * call s:LocalBrowse(expand("<amatch>"))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000166 Last set from /usr/share/vim/vim-7.0/plugin/NetrwPlugin.vim
167<
168See |:verbose-cmd| for more information.
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170==============================================================================
1715. Events *autocmd-events* *E215* *E216*
172
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000173You can specify a comma-separated list of event names. No white space can be
174used in this list. The command applies to all the events in the list.
175
176For READING FILES there are four kinds of events possible:
177 BufNewFile starting to edit a non-existent file
178 BufReadPre BufReadPost starting to edit an existing file
179 FilterReadPre FilterReadPost read the temp file with filter output
180 FileReadPre FileReadPost any other file read
181Vim uses only one of these four kinds when reading a file. The "Pre" and
182"Post" events are both triggered, before and after reading the file.
183
184Note that the autocommands for the *ReadPre events and all the Filter events
185are not allowed to change the current buffer (you will get an error message if
186this happens). This is to prevent the file to be read into the wrong buffer.
187
188Note that the 'modified' flag is reset AFTER executing the BufReadPost
189and BufNewFile autocommands. But when the 'modified' option was set by the
190autocommands, this doesn't happen.
191
192You can use the 'eventignore' option to ignore a number of events or all
193events.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 *autocommand-events* *{event}*
195Vim recognizes the following events. Vim ignores the case of event names
196(e.g., you can use "BUFread" or "bufread" instead of "BufRead").
197
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000198First an overview by function with a short explanation. Then the list
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000199alphabetically with full explanations |autocmd-events-abc|.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000200
201Name triggered by ~
202
203 Reading
204|BufNewFile| starting to edit a file that doesn't exist
205|BufReadPre| starting to edit a new buffer, before reading the file
206|BufRead| starting to edit a new buffer, after reading the file
207|BufReadPost| starting to edit a new buffer, after reading the file
208|BufReadCmd| before starting to edit a new buffer |Cmd-event|
209
210|FileReadPre| before reading a file with a ":read" command
211|FileReadPost| after reading a file with a ":read" command
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000212|FileReadCmd| before reading a file with a ":read" command |Cmd-event|
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000213
214|FilterReadPre| before reading a file from a filter command
215|FilterReadPost| after reading a file from a filter command
216
217|StdinReadPre| before reading from stdin into the buffer
218|StdinReadPost| After reading from the stdin into the buffer
219
220 Writing
221|BufWrite| starting to write the whole buffer to a file
222|BufWritePre| starting to write the whole buffer to a file
223|BufWritePost| after writing the whole buffer to a file
224|BufWriteCmd| before writing the whole buffer to a file |Cmd-event|
225
226|FileWritePre| starting to write part of a buffer to a file
227|FileWritePost| after writing part of a buffer to a file
228|FileWriteCmd| before writing part of a buffer to a file |Cmd-event|
229
230|FileAppendPre| starting to append to a file
231|FileAppendPost| after appending to a file
232|FileAppendCmd| before appending to a file |Cmd-event|
233
234|FilterWritePre| starting to write a file for a filter command or diff
235|FilterWritePost| after writing a file for a filter command or diff
236
237 Buffers
238|BufAdd| just after adding a buffer to the buffer list
239|BufCreate| just after adding a buffer to the buffer list
240|BufDelete| before deleting a buffer from the buffer list
241|BufWipeout| before completely deleting a buffer
242
243|BufFilePre| before changing the name of the current buffer
244|BufFilePost| after changing the name of the current buffer
245
246|BufEnter| after entering a buffer
247|BufLeave| before leaving to another buffer
248|BufWinEnter| after a buffer is displayed in a window
249|BufWinLeave| before a buffer is removed from a window
250
251|BufUnload| before unloading a buffer
252|BufHidden| just after a buffer has become hidden
253|BufNew| just after creating a new buffer
254
255|SwapExists| detected an existing swap file
256
257 Options
258|FileType| when the 'filetype' option has been set
259|Syntax| when the 'syntax' option has been set
260|EncodingChanged| after the 'encoding' option has been changed
261|TermChanged| after the value of 'term' has changed
262
263 Startup and exit
264|VimEnter| after doing all the startup stuff
265|GUIEnter| after starting the GUI successfully
Bram Moolenaard7afed32007-05-06 13:26:41 +0000266|TermResponse| after the terminal response to |t_RV| is received
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000267
268|VimLeavePre| before exiting Vim, before writing the viminfo file
269|VimLeave| before exiting Vim, after writing the viminfo file
270
271 Various
272|FileChangedShell| Vim notices that a file changed since editing started
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000273|FileChangedShellPost| After handling a file changed since editing started
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000274|FileChangedRO| before making the first change to a read-only file
275
Bram Moolenaara94bc432006-03-10 21:42:59 +0000276|ShellCmdPost| after executing a shell command
277|ShellFilterPost| after filtering with a shell command
278
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000279|FuncUndefined| a user function is used but it isn't defined
Bram Moolenaarafeb4fa2006-02-01 21:51:12 +0000280|SpellFileMissing| a spell file is used but it can't be found
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000281|SourcePre| before sourcing a Vim script
Bram Moolenaar8dd1aa52007-01-16 20:33:19 +0000282|SourceCmd| before sourcing a Vim script |Cmd-event|
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000283
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000284|VimResized| after the Vim window size changed
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000285|FocusGained| Vim got input focus
286|FocusLost| Vim lost input focus
287|CursorHold| the user doesn't press a key for a while
Bram Moolenaar754b5602006-02-09 23:53:20 +0000288|CursorHoldI| the user doesn't press a key for a while in Insert mode
289|CursorMoved| the cursor was moved in Normal mode
290|CursorMovedI| the cursor was moved in Insert mode
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000291
292|WinEnter| after entering another window
293|WinLeave| before leaving a window
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000294|TabEnter| after entering another tab page
295|TabLeave| before leaving a tab page
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000296|CmdwinEnter| after entering the command-line window
297|CmdwinLeave| before leaving the command-line window
298
299|InsertEnter| starting Insert mode
300|InsertChange| when typing <Insert> while in Insert or Replace mode
301|InsertLeave| when leaving Insert mode
302
303|ColorScheme| after loading a color scheme
304
305|RemoteReply| a reply from a server Vim was received
306
307|QuickFixCmdPre| before a quickfix command is run
308|QuickFixCmdPost| after a quickfix command is run
309
310|SessionLoadPost| after loading a session file
311
312|MenuPopup| just before showing the popup menu
313
314|User| to be used in combination with ":doautocmd"
315
316
317The alphabetical list of autocommand events: *autocmd-events-abc*
318
319 *BufCreate* *BufAdd*
320BufAdd or BufCreate Just after creating a new buffer which is
321 added to the buffer list, or adding a buffer
322 to the buffer list.
323 Also used just after a buffer in the buffer
324 list has been renamed.
325 The BufCreate event is for historic reasons.
326 NOTE: When this autocommand is executed, the
327 current buffer "%" may be different from the
328 buffer being created "<afile>".
329 *BufDelete*
330BufDelete Before deleting a buffer from the buffer list.
331 The BufUnload may be called first (if the
332 buffer was loaded).
333 Also used just before a buffer in the buffer
334 list is renamed.
335 NOTE: When this autocommand is executed, the
336 current buffer "%" may be different from the
337 buffer being deleted "<afile>".
338 *BufEnter*
339BufEnter After entering a buffer. Useful for setting
340 options for a file type. Also executed when
341 starting to edit a buffer, after the
342 BufReadPost autocommands.
343 *BufFilePost*
344BufFilePost After changing the name of the current buffer
345 with the ":file" or ":saveas" command.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000346 *BufFilePre*
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000347BufFilePre Before changing the name of the current buffer
348 with the ":file" or ":saveas" command.
349 *BufHidden*
350BufHidden Just after a buffer has become hidden. That
351 is, when there are no longer windows that show
352 the buffer, but the buffer is not unloaded or
353 deleted. Not used for ":qa" or ":q" when
354 exiting Vim.
355 NOTE: When this autocommand is executed, the
356 current buffer "%" may be different from the
357 buffer being unloaded "<afile>".
358 *BufLeave*
359BufLeave Before leaving to another buffer. Also when
360 leaving or closing the current window and the
361 new current window is not for the same buffer.
362 Not used for ":qa" or ":q" when exiting Vim.
363 *BufNew*
364BufNew Just after creating a new buffer. Also used
365 just after a buffer has been renamed. When
366 the buffer is added to the buffer list BufAdd
367 will be triggered too.
368 NOTE: When this autocommand is executed, the
369 current buffer "%" may be different from the
370 buffer being created "<afile>".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 *BufNewFile*
372BufNewFile When starting to edit a file that doesn't
373 exist. Can be used to read in a skeleton
374 file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 *BufRead* *BufReadPost*
376BufRead or BufReadPost When starting to edit a new buffer, after
377 reading the file into the buffer, before
378 executing the modelines. See |BufWinEnter|
379 for when you need to do something after
380 processing the modelines.
381 This does NOT work for ":r file". Not used
382 when the file doesn't exist. Also used after
383 successfully recovering a file.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000384 *BufReadCmd*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385BufReadCmd Before starting to edit a new buffer. Should
386 read the file into the buffer. |Cmd-event|
Bram Moolenaar4770d092006-01-12 23:22:24 +0000387 *BufReadPre* *E200* *E201*
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000388BufReadPre When starting to edit a new buffer, before
389 reading the file into the buffer. Not used
390 if the file doesn't exist.
391 *BufUnload*
392BufUnload Before unloading a buffer. This is when the
393 text in the buffer is going to be freed. This
394 may be after a BufWritePost and before a
395 BufDelete. Also used for all buffers that are
396 loaded when Vim is going to exit.
397 NOTE: When this autocommand is executed, the
398 current buffer "%" may be different from the
399 buffer being unloaded "<afile>".
400 *BufWinEnter*
401BufWinEnter After a buffer is displayed in a window. This
402 can be when the buffer is loaded (after
403 processing the modelines), when a hidden
404 buffer is displayed in a window (and is no
405 longer hidden) or a buffer already visible in
406 a window is also displayed in another window.
407 *BufWinLeave*
408BufWinLeave Before a buffer is removed from a window.
409 Not when it's still visible in another window.
410 Also triggered when exiting. It's triggered
411 before BufUnload or BufHidden.
412 NOTE: When this autocommand is executed, the
413 current buffer "%" may be different from the
414 buffer being unloaded "<afile>".
415 *BufWipeout*
416BufWipeout Before completely deleting a buffer. The
417 BufUnload and BufDelete events may be called
418 first (if the buffer was loaded and was in the
419 buffer list). Also used just before a buffer
420 is renamed (also when it's not in the buffer
421 list).
422 NOTE: When this autocommand is executed, the
423 current buffer "%" may be different from the
424 buffer being deleted "<afile>".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 *BufWrite* *BufWritePre*
426BufWrite or BufWritePre Before writing the whole buffer to a file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 *BufWriteCmd*
428BufWriteCmd Before writing the whole buffer to a file.
429 Should do the writing of the file and reset
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000430 'modified' if successful, unless '+' is in
431 'cpo' and writing to another file |cpo-+|.
432 The buffer contents should not be changed.
433 |Cmd-event|
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000434 *BufWritePost*
435BufWritePost After writing the whole buffer to a file
436 (should undo the commands for BufWritePre).
437 *CmdwinEnter*
438CmdwinEnter After entering the command-line window.
439 Useful for setting options specifically for
440 this special type of window. This is
441 triggered _instead_ of BufEnter and WinEnter.
442 <afile> is set to a single character,
443 indicating the type of command-line.
444 |cmdwin-char|
445 *CmdwinLeave*
446CmdwinLeave Before leaving the command-line window.
447 Useful to clean up any global setting done
448 with CmdwinEnter. This is triggered _instead_
449 of BufLeave and WinLeave.
450 <afile> is set to a single character,
451 indicating the type of command-line.
452 |cmdwin-char|
453 *ColorScheme*
454ColorScheme After loading a color scheme. |:colorscheme|
Bram Moolenaar754b5602006-02-09 23:53:20 +0000455
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000456 *CursorHold*
457CursorHold When the user doesn't press a key for the time
458 specified with 'updatetime'. Not re-triggered
459 until the user has pressed a key (i.e. doesn't
460 fire every 'updatetime' ms if you leave Vim to
461 make some coffee. :) See |CursorHold-example|
462 for previewing tags.
463 This event is only triggered in Normal mode.
Bram Moolenaard7afed32007-05-06 13:26:41 +0000464 It is not triggered when waiting for a command
465 argument to be typed, or a movement after an
466 operator.
Bram Moolenaare3226be2005-12-18 22:10:00 +0000467 While recording the CursorHold event is not
468 triggered. |q|
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000469 Note: Interactive commands cannot be used for
470 this event. There is no hit-enter prompt,
471 the screen is updated directly (when needed).
472 Note: In the future there will probably be
473 another option to set the time.
474 Hint: to force an update of the status lines
475 use: >
476 :let &ro = &ro
477< {only on Amiga, Unix, Win32, MSDOS and all GUI
478 versions}
Bram Moolenaar754b5602006-02-09 23:53:20 +0000479 *CursorHoldI*
480CursorHoldI Just like CursorHold, but in Insert mode.
481
482 *CursorMoved*
483CursorMoved After the cursor was moved in Normal mode.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000484 Also when the text of the cursor line has been
485 changed, e.g., with "x", "rx" or "p".
Bram Moolenaar754b5602006-02-09 23:53:20 +0000486 Not triggered when there is typeahead or when
487 an operator is pending.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000488 For an example see |match-parens|.
Bram Moolenaar754b5602006-02-09 23:53:20 +0000489 Careful: Don't do anything that the user does
490 not expect or that is slow.
491 *CursorMovedI*
492CursorMovedI After the cursor was moved in Insert mode.
493 Otherwise the same as CursorMoved.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000494 *EncodingChanged*
495EncodingChanged Fires off after the 'encoding' option has been
496 changed. Useful to set up fonts, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 *FileAppendCmd*
498FileAppendCmd Before appending to a file. Should do the
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000499 appending to the file. Use the '[ and ']
500 marks for the range of lines.|Cmd-event|
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000501 *FileAppendPost*
502FileAppendPost After appending to a file.
503 *FileAppendPre*
504FileAppendPre Before appending to a file. Use the '[ and ']
505 marks for the range of lines.
506 *FileChangedRO*
507FileChangedRO Before making the first change to a read-only
508 file. Can be used to check-out the file from
509 a source control system. Not triggered when
510 the change was caused by an autocommand.
511 This event is triggered when making the first
512 change in a buffer or the first change after
Bram Moolenaar61660ea2006-04-07 21:40:07 +0000513 'readonly' was set, just before the change is
514 applied to the text.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000515 WARNING: If the autocommand moves the cursor
516 the effect of the change is undefined.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000517 *E788*
518 It is not allowed to change to another buffer
519 here. You can reload the buffer but not edit
520 another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 *FileChangedShell*
522FileChangedShell When Vim notices that the modification time of
523 a file has changed since editing started.
524 Also when the file attributes of the file
525 change. |timestamp|
526 Mostly triggered after executing a shell
527 command, but also with a |:checktime| command
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000528 or when Gvim regains input focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 This autocommand is triggered for each changed
530 file. It is not used when 'autoread' is set
531 and the buffer was not changed. If a
532 FileChangedShell autocommand is present the
533 warning message and prompt is not given.
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000534 The |v:fcs_reason| variable is set to indicate
535 what happened and |v:fcs_choice| can be used
536 to tell Vim what to do next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 NOTE: When this autocommand is executed, the
538 current buffer "%" may be different from the
539 buffer that was changed "<afile>".
540 NOTE: The commands must not change the current
541 buffer, jump to another buffer or delete a
542 buffer. *E246*
543 NOTE: This event never nests, to avoid an
544 endless loop. This means that while executing
545 commands for the FileChangedShell event no
546 other FileChangedShell event will be
547 triggered.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000548 *FileChangedShellPost*
549FileChangedShellPost After handling a file that was changed outside
550 of Vim. Can be used to update the statusline.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000551 *FileEncoding*
552FileEncoding Obsolete. It still works and is equivalent
553 to |EncodingChanged|.
554 *FileReadCmd*
555FileReadCmd Before reading a file with a ":read" command.
556 Should do the reading of the file. |Cmd-event|
557 *FileReadPost*
558FileReadPost After reading a file with a ":read" command.
559 Note that Vim sets the '[ and '] marks to the
560 first and last line of the read. This can be
561 used to operate on the lines just read.
562 *FileReadPre*
563FileReadPre Before reading a file with a ":read" command.
564 *FileType*
Bram Moolenaard7afed32007-05-06 13:26:41 +0000565FileType When the 'filetype' option has been set. The
566 pattern is matched against the filetype.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000567 <afile> can be used for the name of the file
568 where this option was set, and <amatch> for
569 the new value of 'filetype'.
570 See |filetypes|.
571 *FileWriteCmd*
572FileWriteCmd Before writing to a file, when not writing the
573 whole buffer. Should do the writing to the
574 file. Should not change the buffer. Use the
575 '[ and '] marks for the range of lines.
576 |Cmd-event|
577 *FileWritePost*
578FileWritePost After writing to a file, when not writing the
579 whole buffer.
580 *FileWritePre*
581FileWritePre Before writing to a file, when not writing the
582 whole buffer. Use the '[ and '] marks for the
583 range of lines.
584 *FilterReadPost*
585FilterReadPost After reading a file from a filter command.
586 Vim checks the pattern against the name of
587 the current buffer as with FilterReadPre.
588 Not triggered when 'shelltemp' is off.
589 *FilterReadPre* *E135*
590FilterReadPre Before reading a file from a filter command.
591 Vim checks the pattern against the name of
592 the current buffer, not the name of the
593 temporary file that is the output of the
594 filter command.
595 Not triggered when 'shelltemp' is off.
596 *FilterWritePost*
597FilterWritePost After writing a file for a filter command or
598 making a diff.
599 Vim checks the pattern against the name of
600 the current buffer as with FilterWritePre.
601 Not triggered when 'shelltemp' is off.
602 *FilterWritePre*
603FilterWritePre Before writing a file for a filter command or
604 making a diff.
605 Vim checks the pattern against the name of
606 the current buffer, not the name of the
607 temporary file that is the output of the
608 filter command.
609 Not triggered when 'shelltemp' is off.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 *FocusGained*
611FocusGained When Vim got input focus. Only for the GUI
612 version and a few console versions where this
613 can be detected.
614 *FocusLost*
615FocusLost When Vim lost input focus. Only for the GUI
616 version and a few console versions where this
Bram Moolenaar843ee412004-06-30 16:16:41 +0000617 can be detected. May also happen when a
618 dialog pops up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 *FuncUndefined*
620FuncUndefined When a user function is used but it isn't
621 defined. Useful for defining a function only
Bram Moolenaard7afed32007-05-06 13:26:41 +0000622 when it's used. The pattern is matched
623 against the function name. Both <amatch> and
624 <afile> are set to the name of the function.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000625 See |autoload-functions|.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000626 *GUIEnter*
627GUIEnter After starting the GUI successfully, and after
628 opening the window. It is triggered before
629 VimEnter when using gvim. Can be used to
630 position the window from a .gvimrc file: >
631 :autocmd GUIEnter * winpos 100 50
Bram Moolenaard7afed32007-05-06 13:26:41 +0000632< *GUIFailed*
633GUIFailed After starting the GUI failed. Vim may
634 continue to run in the terminal, if possible
635 (only on Unix and alikes, when connecting the
636 X server fails). You may want to quit Vim: >
637 :autocmd GUIFailed * qall
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000638< *InsertChange*
639InsertChange When typing <Insert> while in Insert or
640 Replace mode. The |v:insertmode| variable
641 indicates the new mode.
642 Be careful not to move the cursor or do
643 anything else that the user does not expect.
644 *InsertEnter*
Bram Moolenaard7afed32007-05-06 13:26:41 +0000645InsertEnter Just before starting Insert mode. Also for
646 Replace mode and Virtual Replace mode. The
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000647 |v:insertmode| variable indicates the mode.
648 Be careful not to move the cursor or do
649 anything else that the user does not expect.
650 *InsertLeave*
651InsertLeave When leaving Insert mode. Also when using
652 CTRL-O |i_CTRL-O|. But not for |i_CTRL-C|.
653 *MenuPopup*
654MenuPopup Just before showing the popup menu (under the
655 right mouse button). Useful for adjusting the
656 menu for what is under the cursor or mouse
657 pointer.
658 The pattern is matched against a single
659 character representing the mode:
660 n Normal
661 v Visual
662 o Operator-pending
663 i Insert
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000664 c Command line
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000665 *QuickFixCmdPre*
666QuickFixCmdPre Before a quickfix command is run (|:make|,
Bram Moolenaara6557602006-02-04 22:43:20 +0000667 |:lmake|, |:grep|, |:lgrep|, |:grepadd|,
668 |:lgrepadd|, |:vimgrep|, |:lvimgrep|,
Bram Moolenaard7afed32007-05-06 13:26:41 +0000669 |:vimgrepadd|, |:lvimgrepadd|). The pattern is
Bram Moolenaara6557602006-02-04 22:43:20 +0000670 matched against the command being run. When
671 |:grep| is used but 'grepprg' is set to
672 "internal" it still matches "grep".
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000673 This command cannot be used to set the
674 'makeprg' and 'grepprg' variables.
675 If this command causes an error, the quickfix
676 command is not executed.
677 *QuickFixCmdPost*
678QuickFixCmdPost Like QuickFixCmdPre, but after a quickfix
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000679 command is run, before jumping to the first
680 location.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000681 *RemoteReply*
682RemoteReply When a reply from a Vim that functions as
Bram Moolenaard7afed32007-05-06 13:26:41 +0000683 server was received |server2client()|. The
684 pattern is matched against the {serverid}.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000685 <amatch> is equal to the {serverid} from which
686 the reply was sent, and <afile> is the actual
687 reply string.
688 Note that even if an autocommand is defined,
689 the reply should be read with |remote_read()|
690 to consume it.
691 *SessionLoadPost*
692SessionLoadPost After loading the session file created using
693 the |:mksession| command.
Bram Moolenaara94bc432006-03-10 21:42:59 +0000694 *ShellCmdPost*
695ShellCmdPost After executing a shell command with |:!cmd|,
696 |:shell|, |:make| and |:grep|. Can be used to
697 check for any changed files.
698 *ShellFilterPost*
699ShellFilterPost After executing a shell command with
700 ":{range}!cmd", ":w !cmd" or ":r !cmd".
701 Can be used to check for any changed files.
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000702 *SourcePre*
703SourcePre Before sourcing a Vim script. |:source|
Bram Moolenaar8dd1aa52007-01-16 20:33:19 +0000704 <afile> is the name of the file being sourced.
705 *SourceCmd*
706SourceCmd When sourcing a Vim script. |:source|
707 <afile> is the name of the file being sourced.
708 The autocommand must source this file.
709 |Cmd-event|
Bram Moolenaarafeb4fa2006-02-01 21:51:12 +0000710 *SpellFileMissing*
711SpellFileMissing When trying to load a spell checking file and
Bram Moolenaar8dd1aa52007-01-16 20:33:19 +0000712 it can't be found. The pattern is matched
713 against the language. <amatch> is the
714 language, 'encoding' also matters. See
Bram Moolenaarafeb4fa2006-02-01 21:51:12 +0000715 |spell-SpellFileMissing|.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000716 *StdinReadPost*
717StdinReadPost After reading from the stdin into the buffer,
718 before executing the modelines. Only used
719 when the "-" argument was used when Vim was
720 started |--|.
721 *StdinReadPre*
722StdinReadPre Before reading from stdin into the buffer.
723 Only used when the "-" argument was used when
724 Vim was started |--|.
725 *SwapExists*
726SwapExists Detected an existing swap file when starting
727 to edit a file. Only when it is possible to
728 select a way to handle the situation, when Vim
729 would ask the user what to do.
730 The |v:swapname| variable holds the name of
Bram Moolenaarb3480382005-12-11 21:33:32 +0000731 the swap file found, <afile> the file being
732 edited. |v:swapcommand| may contain a command
733 to be executed in the opened file.
734 The commands should set the |v:swapchoice|
735 variable to a string with one character to
736 tell Vim what should be done next:
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000737 'o' open read-only
738 'e' edit the file anyway
739 'r' recover
740 'd' delete the swap file
741 'q' quit, don't edit the file
742 'a' abort, like hitting CTRL-C
743 When set to an empty string the user will be
744 asked, as if there was no SwapExists autocmd.
745 Note: Do not try to change the buffer, the
746 results are unpredictable.
747 *Syntax*
Bram Moolenaard7afed32007-05-06 13:26:41 +0000748Syntax When the 'syntax' option has been set. The
749 pattern is matched against the syntax name.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000750 <afile> can be used for the name of the file
751 where this option was set, and <amatch> for
752 the new value of 'syntax'.
753 See |:syn-on|.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000754 *TabEnter*
755TabEnter Just after entering a tab page. |tab-page|
Bram Moolenaar56a907a2006-05-06 21:44:30 +0000756 After triggering the WinEnter and before
757 triggering the BufEnter event.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000758 *TabLeave*
759TabLeave Just before leaving a tab page. |tab-page|
760 A WinLeave event will have been triggered
761 first.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000762 *TermChanged*
763TermChanged After the value of 'term' has changed. Useful
764 for re-loading the syntax file to update the
765 colors, fonts and other terminal-dependent
766 settings. Executed for all loaded buffers.
767 *TermResponse*
768TermResponse After the response to |t_RV| is received from
769 the terminal. The value of |v:termresponse|
770 can be used to do things depending on the
771 terminal version.
772 *User*
773User Never executed automatically. To be used for
774 autocommands that are only executed with
775 ":doautocmd".
776 *UserGettingBored*
777UserGettingBored When the user hits CTRL-C. Just kidding! :-)
778 *VimEnter*
779VimEnter After doing all the startup stuff, including
780 loading .vimrc files, executing the "-c cmd"
781 arguments, creating all windows and loading
782 the buffers in them.
783 *VimLeave*
784VimLeave Before exiting Vim, just after writing the
785 .viminfo file. Executed only once, like
786 VimLeavePre.
787 To detect an abnormal exit use |v:dying|.
788 *VimLeavePre*
789VimLeavePre Before exiting Vim, just before writing the
790 .viminfo file. This is executed only once,
791 if there is a match with the name of what
792 happens to be the current buffer when exiting.
793 Mostly useful with a "*" pattern. >
794 :autocmd VimLeavePre * call CleanupStuff()
795< To detect an abnormal exit use |v:dying|.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000796 *VimResized*
797VimResized After the Vim window was resized, thus 'lines'
798 and/or 'columns' changed. Not when starting
799 up though.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 *WinEnter*
801WinEnter After entering another window. Not done for
802 the first window, when Vim has just started.
803 Useful for setting the window height.
804 If the window is for another buffer, Vim
805 executes the BufEnter autocommands after the
806 WinEnter autocommands.
807 Note: When using ":split fname" the WinEnter
808 event is triggered after the split but before
809 the file "fname" is loaded.
810 *WinLeave*
811WinLeave Before leaving a window. If the window to be
812 entered next is for a different buffer, Vim
813 executes the BufLeave autocommands before the
814 WinLeave autocommands (but not for ":new").
815 Not used for ":qa" or ":q" when exiting Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817==============================================================================
8186. Patterns *autocmd-patterns* *{pat}*
819
820The file pattern {pat} is tested for a match against the file name in one of
821two ways:
8221. When there is no '/' in the pattern, Vim checks for a match against only
823 the tail part of the file name (without its leading directory path).
8242. When there is a '/' in the pattern, Vim checks for a match against the
825 both short file name (as you typed it) and the full file name (after
826 expanding it to a full path and resolving symbolic links).
827
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000828The special pattern <buffer> or <buffer=N> is used for buffer-local
829autocommands |autocmd-buflocal|. This pattern is not matched against the name
830of a buffer.
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832Examples: >
833 :autocmd BufRead *.txt set et
834Set the 'et' option for all text files. >
835
836 :autocmd BufRead /vim/src/*.c set cindent
837Set the 'cindent' option for C files in the /vim/src directory. >
838
839 :autocmd BufRead /tmp/*.c set ts=5
840If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", and
841you start editing "/tmp/test.c", this autocommand will match.
842
843Note: To match part of a path, but not from the root directory, use a '*' as
844the first character. Example: >
845 :autocmd BufRead */doc/*.txt set tw=78
846This autocommand will for example be executed for "/tmp/doc/xx.txt" and
847"/usr/home/piet/doc/yy.txt". The number of directories does not matter here.
848
849
850The file name that the pattern is matched against is after expanding
851wildcards. Thus is you issue this command: >
852 :e $ROOTDIR/main.$EXT
853The argument is first expanded to: >
854 /usr/root/main.py
855Before it's matched with the pattern of the autocommand. Careful with this
856when using events like FileReadCmd, the value of <amatch> may not be what you
857expect.
858
859
860Environment variables can be used in a pattern: >
861 :autocmd BufRead $VIMRUNTIME/doc/*.txt set expandtab
862And ~ can be used for the home directory (if $HOME is defined): >
863 :autocmd BufWritePost ~/.vimrc so ~/.vimrc
864 :autocmd BufRead ~archive/* set readonly
865The environment variable is expanded when the autocommand is defined, not when
866the autocommand is executed. This is different from the command!
867
868 *file-pattern*
869The pattern is interpreted like mostly used in file names:
870 * matches any sequence of characters
871 ? matches any single character
872 \? matches a '?'
873 . matches a '.'
874 ~ matches a '~'
875 , separates patterns
876 \, matches a ','
877 { } like \( \) in a |pattern|
878 , inside { }: like \| in a |pattern|
879 \ special meaning like in a |pattern|
880 [ch] matches 'c' or 'h'
881 [^ch] match any character but 'c' and 'h'
882
883Note that for all systems the '/' character is used for path separator (even
884MS-DOS and OS/2). This was done because the backslash is difficult to use
885in a pattern and to make the autocommands portable across different systems.
886
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000887 *autocmd-changes*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888Matching with the pattern is done when an event is triggered. Changing the
889buffer name in one of the autocommands, or even deleting the buffer, does not
890change which autocommands will be executed. Example: >
891
892 au BufEnter *.foo bdel
893 au BufEnter *.foo set modified
894
895This will delete the current buffer and then set 'modified' in what has become
896the current buffer instead. Vim doesn't take into account that "*.foo"
897doesn't match with that buffer name. It matches "*.foo" with the name of the
898buffer at the moment the event was triggered.
899
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000900However, buffer-local autocommands will not be executed for a buffer that has
901been wiped out with |:bwipe|. After deleting the buffer with |:bdel| the
902buffer actually still exists (it becomes unlisted), thus the autocommands are
903still executed.
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905==============================================================================
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009067. Buffer-local autocommands *autocmd-buflocal* *autocmd-buffer-local*
907 *<buffer=N>* *<buffer=abuf>* *E680*
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000908
909Buffer-local autocommands are attached to a specific buffer. They are useful
910if the buffer does not have a name and when the name does not match a specific
911pattern. But it also means they must be explicitly added to each buffer.
912
913Instead of a pattern buffer-local autocommands use one of these forms:
914 <buffer> current buffer
915 <buffer=99> buffer number 99
916 <buffer=abuf> using <abuf> (only when executing autocommands)
917 |<abuf>|
918
919Examples: >
920 :au CursorHold <buffer> echo 'hold'
921 :au CursorHold <buffer=33> echo 'hold'
922 :au CursorHold <buffer=abuf> echo 'hold'
923
924All the commands for autocommands also work with buffer-local autocommands,
925simply use the special string instead of the pattern. Examples: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000926 :au! * <buffer> " remove buffer-local autocommands for
927 " current buffer
928 :au! * <buffer=33> " remove buffer-local autocommands for
929 " buffer #33
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000930 :dobuf :au! CursorHold <buffer> " remove autocmd for given event for all
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000931 " buffers
932 :au * <buffer> " list buffer-local autocommands for
933 " current buffer
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000934
935Note that when an autocommand is defined for the current buffer, it is stored
936with the buffer number. Thus it uses the form "<buffer=12>", where 12 is the
937number of the current buffer. You will see this when listing autocommands,
938for example.
939
940To test for presence of buffer-local autocommands use the |exists()| function
941as follows: >
942 :if exists("#CursorHold#<buffer=12>") | ... | endif
943 :if exists("#CursorHold#<buffer>") | ... | endif " for current buffer
944
945When a buffer is wiped out its buffer-local autocommands are also gone, of
946course. Note that when deleting a buffer, e.g., with ":bdel", it is only
947unlisted, the autocommands are still present. In order to see the removal of
948buffer-local autocommands: >
949 :set verbose=6
950
951It is not possible to define buffer-local autocommands for a non-existent
952buffer.
953
954==============================================================================
9558. Groups *autocmd-groups*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957Autocommands can be put together in a group. This is useful for removing or
958executing a group of autocommands. For example, all the autocommands for
959syntax highlighting are put in the "highlight" group, to be able to execute
960":doautoall highlight BufRead" when the GUI starts.
961
962When no specific group is selected, Vim uses the default group. The default
963group does not have a name. You cannot execute the autocommands from the
964default group separately; you can execute them only by executing autocommands
965for all groups.
966
967Normally, when executing autocommands automatically, Vim uses the autocommands
968for all groups. The group only matters when executing autocommands with
969":doautocmd" or ":doautoall", or when defining or deleting autocommands.
970
971The group name can contain any characters except white space. The group name
972"end" is reserved (also in uppercase).
973
974The group name is case sensitive. Note that this is different from the event
975name!
976
977 *:aug* *:augroup*
978:aug[roup] {name} Define the autocmd group name for the
979 following ":autocmd" commands. The name "end"
980 or "END" selects the default group.
981
982 *:augroup-delete* *E367*
983:aug[roup]! {name} Delete the autocmd group {name}. Don't use
984 this if there is still an autocommand using
985 this group! This is not checked.
986
987To enter autocommands for a specific group, use this method:
9881. Select the group with ":augroup {name}".
9892. Delete any old autocommands with ":au!".
9903. Define the autocommands.
9914. Go back to the default group with "augroup END".
992
993Example: >
994 :augroup uncompress
995 : au!
996 : au BufEnter *.gz %!gunzip
997 :augroup END
998
999This prevents having the autocommands defined twice (e.g., after sourcing the
1000.vimrc file again).
1001
1002==============================================================================
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010039. Executing autocommands *autocmd-execute*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005Vim can also execute Autocommands non-automatically. This is useful if you
1006have changed autocommands, or when Vim has executed the wrong autocommands
1007(e.g., the file pattern match was wrong).
1008
1009Note that the 'eventignore' option applies here too. Events listed in this
1010option will not cause any commands to be executed.
1011
1012 *:do* *:doau* *:doautocmd* *E217*
1013:do[autocmd] [group] {event} [fname]
1014 Apply the autocommands matching [fname] (default:
1015 current file name) for {event} to the current buffer.
1016 You can use this when the current file name does not
1017 match the right pattern, after changing settings, or
1018 to execute autocommands for a certain event.
1019 It's possible to use this inside an autocommand too,
1020 so you can base the autocommands for one extension on
1021 another extension. Example: >
1022 :au Bufenter *.cpp so ~/.vimrc_cpp
1023 :au Bufenter *.cpp doau BufEnter x.c
1024< Be careful to avoid endless loops. See
1025 |autocmd-nested|.
1026
1027 When the [group] argument is not given, Vim executes
1028 the autocommands for all groups. When the [group]
1029 argument is included, Vim executes only the matching
1030 autocommands for that group. Note: if you use an
1031 undefined group name, Vim gives you an error message.
1032
Bram Moolenaara5792f52005-11-23 21:25:05 +00001033 After applying the autocommands the modelines are
1034 processed, so that their overrule the settings from
1035 autocommands, like what happens when editing a file.
1036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 *:doautoa* *:doautoall*
1038:doautoa[ll] [group] {event} [fname]
1039 Like ":doautocmd", but apply the autocommands to each
1040 loaded buffer. Note that {fname} is used to select
1041 the autocommands, not the buffers to which they are
1042 applied.
1043 Careful: Don't use this for autocommands that delete a
1044 buffer, change to another buffer or change the
1045 contents of a buffer; the result is unpredictable.
1046 This command is intended for autocommands that set
1047 options, change highlighting, and things like that.
1048
1049==============================================================================
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000105010. Using autocommands *autocmd-use*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
1052For WRITING FILES there are four possible sets of events. Vim uses only one
1053of these sets for a write command:
1054
1055BufWriteCmd BufWritePre BufWritePost writing the whole buffer
1056 FilterWritePre FilterWritePost writing to filter temp file
1057FileAppendCmd FileAppendPre FileAppendPost appending to a file
1058FileWriteCmd FileWritePre FileWritePost any other file write
1059
1060When there is a matching "*Cmd" autocommand, it is assumed it will do the
1061writing. No further writing is done and the other events are not triggered.
1062|Cmd-event|
1063
1064Note that the *WritePost commands should undo any changes to the buffer that
1065were caused by the *WritePre commands; otherwise, writing the file will have
1066the side effect of changing the buffer.
1067
1068Before executing the autocommands, the buffer from which the lines are to be
1069written temporarily becomes the current buffer. Unless the autocommands
1070change the current buffer or delete the previously current buffer, the
1071previously current buffer is made the current buffer again.
1072
1073The *WritePre and *AppendPre autocommands must not delete the buffer from
1074which the lines are to be written.
1075
1076The '[ and '] marks have a special position:
1077- Before the *ReadPre event the '[ mark is set to the line just above where
1078 the new lines will be inserted.
1079- Before the *ReadPost event the '[ mark is set to the first line that was
1080 just read, the '] mark to the last line.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001081- Before executing the *WriteCmd, *WritePre and *AppendPre autocommands the '[
1082 mark is set to the first line that will be written, the '] mark to the last
1083 line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084Careful: '[ and '] change when using commands that change the buffer.
1085
1086In commands which expect a file name, you can use "<afile>" for the file name
1087that is being read |:<afile>| (you can also use "%" for the current file
1088name). "<abuf>" can be used for the buffer number of the currently effective
1089buffer. This also works for buffers that doesn't have a name. But it doesn't
1090work for files without a buffer (e.g., with ":r file").
1091
1092 *gzip-example*
1093Examples for reading and writing compressed files: >
1094 :augroup gzip
1095 : autocmd!
1096 : autocmd BufReadPre,FileReadPre *.gz set bin
1097 : autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip
1098 : autocmd BufReadPost,FileReadPost *.gz set nobin
1099 : autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " . expand("%:r")
1100 : autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r
1101 : autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r
1102
1103 : autocmd FileAppendPre *.gz !gunzip <afile>
1104 : autocmd FileAppendPre *.gz !mv <afile>:r <afile>
1105 : autocmd FileAppendPost *.gz !mv <afile> <afile>:r
1106 : autocmd FileAppendPost *.gz !gzip <afile>:r
1107 :augroup END
1108
1109The "gzip" group is used to be able to delete any existing autocommands with
1110":autocmd!", for when the file is sourced twice.
1111
1112("<afile>:r" is the file name without the extension, see |:_%:|)
1113
1114The commands executed for the BufNewFile, BufRead/BufReadPost, BufWritePost,
1115FileAppendPost and VimLeave events do not set or reset the changed flag of the
1116buffer. When you decompress the buffer with the BufReadPost autocommands, you
1117can still exit with ":q". When you use ":undo" in BufWritePost to undo the
1118changes made by BufWritePre commands, you can still do ":q" (this also makes
1119"ZZ" work). If you do want the buffer to be marked as modified, set the
1120'modified' option.
1121
1122To execute Normal mode commands from an autocommand, use the ":normal"
1123command. Use with care! If the Normal mode command is not finished, the user
1124needs to type characters (e.g., after ":normal m" you need to type a mark
1125name).
1126
1127If you want the buffer to be unmodified after changing it, reset the
1128'modified' option. This makes it possible to exit the buffer with ":q"
1129instead of ":q!".
1130
1131 *autocmd-nested* *E218*
1132By default, autocommands do not nest. If you use ":e" or ":w" in an
1133autocommand, Vim does not execute the BufRead and BufWrite autocommands for
1134those commands. If you do want this, use the "nested" flag for those commands
1135in which you want nesting. For example: >
1136 :autocmd FileChangedShell *.c nested e!
1137The nesting is limited to 10 levels to get out of recursive loops.
1138
1139It's possible to use the ":au" command in an autocommand. This can be a
1140self-modifying command! This can be useful for an autocommand that should
1141execute only once.
1142
Bram Moolenaarb3480382005-12-11 21:33:32 +00001143If you want to skip autocommands for one command, use the |:noautocmd| command
1144modifier or the 'eventignore' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146Note: When reading a file (with ":read file" or with a filter command) and the
1147last line in the file does not have an <EOL>, Vim remembers this. At the next
1148write (with ":write file" or with a filter command), if the same line is
1149written again as the last line in a file AND 'binary' is set, Vim does not
1150supply an <EOL>. This makes a filter command on the just read lines write the
1151same file as was read, and makes a write command on just filtered lines write
1152the same file as was read from the filter. For example, another way to write
1153a compressed file: >
1154
1155 :autocmd FileWritePre *.gz set bin|'[,']!gzip
1156 :autocmd FileWritePost *.gz undo|set nobin
1157<
1158 *autocommand-pattern*
1159You can specify multiple patterns, separated by commas. Here are some
1160examples: >
1161
1162 :autocmd BufRead * set tw=79 nocin ic infercase fo=2croq
1163 :autocmd BufRead .letter set tw=72 fo=2tcrq
1164 :autocmd BufEnter .letter set dict=/usr/lib/dict/words
1165 :autocmd BufLeave .letter set dict=
1166 :autocmd BufRead,BufNewFile *.c,*.h set tw=0 cin noic
1167 :autocmd BufEnter *.c,*.h abbr FOR for (i = 0; i < 3; ++i)<CR>{<CR>}<Esc>O
1168 :autocmd BufLeave *.c,*.h unabbr FOR
1169
1170For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.): >
1171
1172 :autocmd BufEnter ?akefile* set include=^s\=include
1173 :autocmd BufLeave ?akefile* set include&
1174
1175To always start editing C files at the first function: >
1176
1177 :autocmd BufRead *.c,*.h 1;/^{
1178
1179Without the "1;" above, the search would start from wherever the file was
1180entered, rather than from the start of the file.
1181
1182 *skeleton* *template*
1183To read a skeleton (template) file when opening a new file: >
1184
1185 :autocmd BufNewFile *.c 0r ~/vim/skeleton.c
1186 :autocmd BufNewFile *.h 0r ~/vim/skeleton.h
1187 :autocmd BufNewFile *.java 0r ~/vim/skeleton.java
1188
1189To insert the current date and time in a *.html file when writing it: >
1190
1191 :autocmd BufWritePre,FileWritePre *.html ks|call LastMod()|'s
1192 :fun LastMod()
1193 : if line("$") > 20
1194 : let l = 20
1195 : else
1196 : let l = line("$")
1197 : endif
1198 : exe "1," . l . "g/Last modified: /s/Last modified: .*/Last modified: " .
1199 : \ strftime("%Y %b %d")
1200 :endfun
1201
1202You need to have a line "Last modified: <date time>" in the first 20 lines
1203of the file for this to work. Vim replaces <date time> (and anything in the
1204same line after it) with the current date and time. Explanation:
1205 ks mark current position with mark 's'
1206 call LastMod() call the LastMod() function to do the work
1207 's return the cursor to the old position
1208The LastMod() function checks if the file is shorter than 20 lines, and then
1209uses the ":g" command to find lines that contain "Last modified: ". For those
1210lines the ":s" command is executed to replace the existing date with the
1211current one. The ":execute" command is used to be able to use an expression
1212for the ":g" and ":s" commands. The date is obtained with the strftime()
1213function. You can change its argument to get another date string.
1214
1215When entering :autocmd on the command-line, completion of events and command
1216names may be done (with <Tab>, CTRL-D, etc.) where appropriate.
1217
1218Vim executes all matching autocommands in the order that you specify them.
1219It is recommended that your first autocommand be used for all files by using
1220"*" as the file pattern. This means that you can define defaults you like
1221here for any settings, and if there is another matching autocommand it will
1222override these. But if there is no other matching autocommand, then at least
1223your default settings are recovered (if entering this file from another for
1224which autocommands did match). Note that "*" will also match files starting
1225with ".", unlike Unix shells.
1226
1227 *autocmd-searchpat*
1228Autocommands do not change the current search patterns. Vim saves the current
1229search patterns before executing autocommands then restores them after the
1230autocommands finish. This means that autocommands do not affect the strings
1231highlighted with the 'hlsearch' option. Within autocommands, you can still
1232use search patterns normally, e.g., with the "n" command.
1233If you want an autocommand to set the search pattern, such that it is used
1234after the autocommand finishes, use the ":let @/ =" command.
1235The search-highlighting cannot be switched off with ":nohlsearch" in an
1236autocommand. Use the 'h' flag in the 'viminfo' option to disable search-
1237highlighting when starting Vim.
1238
1239 *Cmd-event*
1240When using one of the "*Cmd" events, the matching autocommands are expected to
Bram Moolenaar8dd1aa52007-01-16 20:33:19 +00001241do the file reading, writing or sourcing. This can be used when working with
1242a special kind of file, for example on a remote system.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243CAREFUL: If you use these events in a wrong way, it may have the effect of
1244making it impossible to read or write the matching files! Make sure you test
1245your autocommands properly. Best is to use a pattern that will never match a
1246normal file name, for example "ftp://*".
1247
1248When defining a BufReadCmd it will be difficult for Vim to recover a crashed
1249editing session. When recovering from the original file, Vim reads only those
1250parts of a file that are not found in the swap file. Since that is not
1251possible with a BufReadCmd, use the |:preserve| command to make sure the
1252original file isn't needed for recovery. You might want to do this only when
1253you expect the file to be modified.
1254
Bram Moolenaar8dd1aa52007-01-16 20:33:19 +00001255For file read and write commands the |v:cmdarg| variable holds the "++enc="
1256and "++ff=" argument that are effective. These should be used for the command
1257that reads/writes the file. The |v:cmdbang| variable is one when "!" was
1258used, zero otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
1260See the $VIMRUNTIME/plugin/netrw.vim for examples.
1261
Bram Moolenaarb3480382005-12-11 21:33:32 +00001262==============================================================================
126311. Disabling autocommands *autocmd-disable*
1264
1265To disable autocommands for some time use the 'eventignore' option. Note that
1266this may cause unexpected behavior, make sure you restore 'eventignore'
1267afterwards, using a |:try| block with |:finally|.
1268
1269 *:noautocmd* *:noa*
1270To disable autocommands for just one command use the ":noautocmd" command
1271modifier. This will set 'eventignore' to "all" for the duration of the
1272following command. Example: >
1273
1274 :noautocmd w fname.gz
1275
1276This will write the file without triggering the autocommands defined by the
1277gzip plugin.
1278
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 vim:tw=78:ts=8:ft=help:norl: