blob: e2ca67a48ed21f6ea2b93584b6fcaea2f82b6988 [file] [log] [blame]
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00001*autocmd.txt* For Vim version 7.0aa. Last change: 2005 Oct 10
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 Moolenaar071d4272004-06-13 20:20:40 +000021
22{Vi does not have any of these commands}
23{only when the |+autocmd| feature has not been disabled at compile time}
24
25==============================================================================
261. Introduction *autocmd-intro*
27
Bram Moolenaard4755bb2004-09-02 19:12:26 +000028You can specify commands to be executed automatically when reading or writing
29a file, when entering or leaving a buffer or window, and when exiting Vim.
30For example, you can create an autocommand to set the 'cindent' option for
31files matching *.c. You can also use autocommands to implement advanced
Bram Moolenaar071d4272004-06-13 20:20:40 +000032features, such as editing compressed files (see |gzip-example|). The usual
33place to put autocommands is in your .vimrc or .exrc file.
34
35 *E203* *E204* *E143*
36WARNING: Using autocommands is very powerful, and may lead to unexpected side
37effects. Be careful not to destroy your text.
38- It's a good idea to do some testing on an expendable copy of a file first.
39 For example: If you use autocommands to decompress a file when starting to
40 edit it, make sure that the autocommands for compressing when writing work
41 correctly.
42- Be prepared for an error halfway through (e.g., disk full). Vim will mostly
43 be able to undo the changes to the buffer, but you may have to clean up the
44 changes to other files by hand (e.g., compress a file that has been
45 decompressed).
46- If the BufRead* events allow you to edit a compressed file, the FileRead*
47 events should do the same (this makes recovery possible in some rare cases).
48 It's a good idea to use the same autocommands for the File* and Buf* events
49 when possible.
50
51==============================================================================
522. Defining autocommands *autocmd-define*
53
54Note: The ":autocmd" command cannot be followed by another command, since any
55'|' is considered part of the command.
56
57 *:au* *:autocmd*
58:au[tocmd] [group] {event} {pat} [nested] {cmd}
59 Add {cmd} to the list of commands that Vim will
60 execute automatically on {event} for a file matching
61 {pat}. Vim always adds the {cmd} after existing
62 autocommands, so that the autocommands execute in the
63 order in which they were given. See |autocmd-nested|
64 for [nested].
65
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000066The special pattern <buffer> or <buffer=N> defines a buffer-local autocommand.
67See |autocmd-buflocal|.
68
Bram Moolenaar071d4272004-06-13 20:20:40 +000069Note that special characters (e.g., "%", "<cword>") in the ":autocmd"
70arguments are not expanded when the autocommand is defined. These will be
71expanded when the Event is recognized, and the {cmd} is executed. The only
72exception is that "<sfile>" is expanded when the autocmd is defined. Example:
73>
74 :au BufNewFile,BufRead *.html so <sfile>:h/html.vim
75
76Here Vim expands <sfile> to the name of the file containing this line.
77
78When your .vimrc file is sourced twice, the autocommands will appear twice.
79To avoid this, put this command in your .vimrc file, before defining
80autocommands: >
81
82 :autocmd! " Remove ALL autocommands for the current group.
83
84If you don't want to remove all autocommands, you can instead use a variable
85to ensure that Vim includes the autocommands only once: >
86
87 :if !exists("autocommands_loaded")
88 : let autocommands_loaded = 1
89 : au ...
90 :endif
91
92When the [group] argument is not given, Vim uses the current group (as defined
93with ":augroup"); otherwise, Vim uses the group defined with [group]. Note
94that [group] must have been defined before. You cannot define a new group
95with ":au group ..."; use ":augroup" for that.
96
97While testing autocommands, you might find the 'verbose' option to be useful: >
98 :set verbose=9
99This setting makes Vim echo the autocommands as it executes them.
100
101When defining an autocommand in a script, it will be able to call functions
102local to the script and use mappings local to the script. When the event is
103triggered and the command executed, it will run in the context of the script
104it was defined in. This matters if |<SID>| is used in a command.
105
106When executing the commands, the messages from one command overwrites a
107previous message. This is different from when executing the commands
108manually. Mostly the screen will not scroll up, thus there is no hit-enter
109prompt. When one command outputs two messages this can happen anyway.
110
111==============================================================================
1123. Removing autocommands *autocmd-remove*
113
114:au[tocmd]! [group] {event} {pat} [nested] {cmd}
115 Remove all autocommands associated with {event} and
116 {pat}, and add the command {cmd}. See
117 |autocmd-nested| for [nested].
118
119:au[tocmd]! [group] {event} {pat}
120 Remove all autocommands associated with {event} and
121 {pat}.
122
123:au[tocmd]! [group] * {pat}
124 Remove all autocommands associated with {pat} for all
125 events.
126
127:au[tocmd]! [group] {event}
128 Remove ALL autocommands for {event}.
129
130:au[tocmd]! [group] Remove ALL autocommands.
131
132When the [group] argument is not given, Vim uses the current group (as defined
133with ":augroup"); otherwise, Vim uses the group defined with [group].
134
135==============================================================================
1364. Listing autocommands *autocmd-list*
137
138:au[tocmd] [group] {event} {pat}
139 Show the autocommands associated with {event} and
140 {pat}.
141
142:au[tocmd] [group] * {pat}
143 Show the autocommands associated with {pat} for all
144 events.
145
146:au[tocmd] [group] {event}
147 Show all autocommands for {event}.
148
149:au[tocmd] [group] Show all autocommands.
150
151If you provide the [group] argument, Vim lists only the autocommands for
152[group]; otherwise, Vim lists the autocommands for ALL groups. Note that this
153argument behavior differs from that for defining and removing autocommands.
154
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000155In order to list buffer-local autocommands, use a pattern in the form <buffer>
156or <buffer=N>. See |autocmd-buflocal|.
157
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000158 *:autocmd-verbose*
159When 'verbose' is non-zero, listing an autocommand will also display where it
160was last defined. Example: >
161
162 :verbose autocmd BufEnter
163 FileExplorer BufEnter
164 * call s:LocalBrowse(expand("<amatch>"))
165 Last set from /usr/share/vim/vim-7.0/plugin/NetrwPlugin.vim
166<
167See |:verbose-cmd| for more information.
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169==============================================================================
1705. Events *autocmd-events* *E215* *E216*
171
172 *autocommand-events* *{event}*
173Vim recognizes the following events. Vim ignores the case of event names
174(e.g., you can use "BUFread" or "bufread" instead of "BufRead").
175
176 *BufNewFile*
177BufNewFile When starting to edit a file that doesn't
178 exist. Can be used to read in a skeleton
179 file.
180 *BufReadPre* *E200* *E201*
181BufReadPre When starting to edit a new buffer, before
182 reading the file into the buffer. Not used
183 if the file doesn't exist.
184 *BufRead* *BufReadPost*
185BufRead or BufReadPost When starting to edit a new buffer, after
186 reading the file into the buffer, before
187 executing the modelines. See |BufWinEnter|
188 for when you need to do something after
189 processing the modelines.
190 This does NOT work for ":r file". Not used
191 when the file doesn't exist. Also used after
192 successfully recovering a file.
193 *BufReadCmd*
194BufReadCmd Before starting to edit a new buffer. Should
195 read the file into the buffer. |Cmd-event|
196 *BufFilePre*
197BufFilePre Before changing the name of the current buffer
198 with the ":file" or ":saveas" command.
199 *BufFilePost*
200BufFilePost After changing the name of the current buffer
201 with the ":file" or ":saveas" command.
202 *FileReadPre*
203FileReadPre Before reading a file with a ":read" command.
204 *FileReadPost*
205FileReadPost After reading a file with a ":read" command.
206 Note that Vim sets the '[ and '] marks to the
207 first and last line of the read. This can be
208 used to operate on the lines just read.
209 *FileReadCmd*
210FileReadCmd Before reading a file with a ":read" command.
211 Should do the reading of the file. |Cmd-event|
212 *FilterReadPre* *E135*
213FilterReadPre Before reading a file from a filter command.
214 Vim checks the pattern against the name of
215 the current buffer, not the name of the
216 temporary file that is the output of the
217 filter command.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000218 Not triggered when 'shelltemp' is off.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 *FilterReadPost*
220FilterReadPost After reading a file from a filter command.
221 Vim checks the pattern against the name of
222 the current buffer as with FilterReadPre.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000223 Not triggered when 'shelltemp' is off.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 *FileType*
225FileType When the 'filetype' option has been set.
226 <afile> can be used for the name of the file
227 where this option was set, and <amatch> for
228 the new value of 'filetype'.
229 See |filetypes|.
230 *Syntax*
231Syntax When the 'syntax' option has been set.
232 <afile> can be used for the name of the file
233 where this option was set, and <amatch> for
234 the new value of 'syntax'.
235 See |:syn-on|.
236 *StdinReadPre*
237StdinReadPre Before reading from stdin into the buffer.
238 Only used when the "-" argument was used when
239 Vim was started |--|.
240 *StdinReadPost*
241StdinReadPost After reading from the stdin into the buffer,
242 before executing the modelines. Only used
243 when the "-" argument was used when Vim was
244 started |--|.
245 *BufWrite* *BufWritePre*
246BufWrite or BufWritePre Before writing the whole buffer to a file.
247 *BufWritePost*
248BufWritePost After writing the whole buffer to a file
249 (should undo the commands for BufWritePre).
250 *BufWriteCmd*
251BufWriteCmd Before writing the whole buffer to a file.
252 Should do the writing of the file and reset
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000253 'modified' if successful, unless '+' is in
254 'cpo' and writing to another file |cpo-+|.
255 The buffer contents should not be changed.
256 |Cmd-event|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 *FileWritePre*
258FileWritePre Before writing to a file, when not writing the
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000259 whole buffer. Use the '[ and '] marks for the
260 range of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 *FileWritePost*
262FileWritePost After writing to a file, when not writing the
263 whole buffer.
264 *FileWriteCmd*
265FileWriteCmd Before writing to a file, when not writing the
266 whole buffer. Should do the writing to the
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000267 file. Should not change the buffer. Use the
268 '[ and '] marks for the range of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 |Cmd-event|
270 *FileAppendPre*
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000271FileAppendPre Before appending to a file. Use the '[ and ']
272 marks for the range of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 *FileAppendPost*
274FileAppendPost After appending to a file.
275 *FileAppendCmd*
276FileAppendCmd Before appending to a file. Should do the
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000277 appending to the file. Use the '[ and ']
278 marks for the range of lines.|Cmd-event|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 *FilterWritePre*
280FilterWritePre Before writing a file for a filter command or
281 making a diff.
282 Vim checks the pattern against the name of
283 the current buffer, not the name of the
284 temporary file that is the output of the
285 filter command.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000286 Not triggered when 'shelltemp' is off.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 *FilterWritePost*
288FilterWritePost After writing a file for a filter command or
289 making a diff.
290 Vim checks the pattern against the name of
291 the current buffer as with FilterWritePre.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000292 Not triggered when 'shelltemp' is off.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 *FileChangedShell*
294FileChangedShell When Vim notices that the modification time of
295 a file has changed since editing started.
296 Also when the file attributes of the file
297 change. |timestamp|
298 Mostly triggered after executing a shell
299 command, but also with a |:checktime| command
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000300 or when Gvim regains input focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 This autocommand is triggered for each changed
302 file. It is not used when 'autoread' is set
303 and the buffer was not changed. If a
304 FileChangedShell autocommand is present the
305 warning message and prompt is not given.
306 This is useful for reloading related buffers
307 which are affected by a single command.
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000308 The |v:fcs_reason| variable is set to indicate
309 what happened and |v:fcs_choice| can be used
310 to tell Vim what to do next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 NOTE: When this autocommand is executed, the
312 current buffer "%" may be different from the
313 buffer that was changed "<afile>".
314 NOTE: The commands must not change the current
315 buffer, jump to another buffer or delete a
316 buffer. *E246*
317 NOTE: This event never nests, to avoid an
318 endless loop. This means that while executing
319 commands for the FileChangedShell event no
320 other FileChangedShell event will be
321 triggered.
322 *FileChangedRO*
323FileChangedRO Before making the first change to a read-only
324 file. Can be used to check-out the file from
325 a source control system. Not triggered when
326 the change was caused by an autocommand.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000327 This event is triggered when making the first
328 change in a buffer or the first change after
329 'readonly' was set,
330 just before the change is applied to the text.
331 WARNING: If the autocommand moves the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 the effect of the change is undefined.
333 *FocusGained*
334FocusGained When Vim got input focus. Only for the GUI
335 version and a few console versions where this
336 can be detected.
337 *FocusLost*
338FocusLost When Vim lost input focus. Only for the GUI
339 version and a few console versions where this
Bram Moolenaar843ee412004-06-30 16:16:41 +0000340 can be detected. May also happen when a
341 dialog pops up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 *FuncUndefined*
343FuncUndefined When a user function is used but it isn't
344 defined. Useful for defining a function only
345 when it's used. Both <amatch> and <afile> are
346 set to the name of the function.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000347 See |autoload-functions|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 *CursorHold*
349CursorHold When the user doesn't press a key for the time
350 specified with 'updatetime'. Not re-triggered
351 until the user has pressed a key (i.e. doesn't
352 fire every 'updatetime' ms if you leave Vim to
353 make some coffee. :) See |CursorHold-example|
354 for previewing tags.
355 This event is only triggered in Normal mode.
356 Note: Interactive commands cannot be used for
357 this event. There is no hit-enter prompt,
358 the screen is updated directly (when needed).
359 Note: In the future there will probably be
360 another option to set the time.
361 Hint: to force an update of the status lines
362 use: >
363 :let &ro = &ro
364< {only on Amiga, Unix, Win32, MSDOS and all GUI
365 versions}
366 *BufEnter*
367BufEnter After entering a buffer. Useful for setting
368 options for a file type. Also executed when
369 starting to edit a buffer, after the
370 BufReadPost autocommands.
371 *BufLeave*
372BufLeave Before leaving to another buffer. Also when
373 leaving or closing the current window and the
374 new current window is not for the same buffer.
375 Not used for ":qa" or ":q" when exiting Vim.
376 *BufWinEnter*
377BufWinEnter After a buffer is displayed in a window. This
378 can be when the buffer is loaded (after
379 processing the modelines), when a hidden
380 buffer is displayed in a window (and is no
381 longer hidden) or a buffer already visible in
382 a window is also displayed in another window.
383 *BufWinLeave*
384BufWinLeave Before a buffer is removed from a window.
385 Not when it's still visible in another window.
386 Also triggered when exiting. It's triggered
387 before BufUnload or BufHidden.
388 NOTE: When this autocommand is executed, the
389 current buffer "%" may be different from the
390 buffer being unloaded "<afile>".
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 *BufHidden*
401BufHidden Just after a buffer has become hidden. That
402 is, when there are no longer windows that show
403 the buffer, but the buffer is not unloaded or
404 deleted. Not used for ":qa" or ":q" when
405 exiting Vim.
406 NOTE: When this autocommand is executed, the
407 current buffer "%" may be different from the
408 buffer being unloaded "<afile>".
409 *BufNew*
410BufNew Just after creating a new buffer. Also used
411 just after a buffer has been renamed. When
412 the buffer is added to the buffer list BufAdd
413 will be triggered too.
414 NOTE: When this autocommand is executed, the
415 current buffer "%" may be different from the
416 buffer being created "<afile>".
417 *BufCreate* *BufAdd*
418BufAdd or BufCreate Just after creating a new buffer which is
419 added to the buffer list, or adding a buffer
420 to the buffer list.
421 Also used just after a buffer in the buffer
422 list has been renamed.
423 The BufCreate event is for historic reasons.
424 NOTE: When this autocommand is executed, the
425 current buffer "%" may be different from the
426 buffer being created "<afile>".
427 *BufDelete*
428BufDelete Before deleting a buffer from the buffer list.
429 The BufUnload may be called first (if the
430 buffer was loaded).
431 Also used just before a buffer in the buffer
432 list is renamed.
433 NOTE: When this autocommand is executed, the
434 current buffer "%" may be different from the
435 buffer being deleted "<afile>".
436 *BufWipeout*
437BufWipeout Before completely deleting a buffer. The
438 BufUnload and BufDelete events may be called
439 first (if the buffer was loaded and was in the
440 buffer list). Also used just before a buffer
441 is renamed (also when it's not in the buffer
442 list).
443 NOTE: When this autocommand is executed, the
444 current buffer "%" may be different from the
445 buffer being deleted "<afile>".
446 *WinEnter*
447WinEnter After entering another window. Not done for
448 the first window, when Vim has just started.
449 Useful for setting the window height.
450 If the window is for another buffer, Vim
451 executes the BufEnter autocommands after the
452 WinEnter autocommands.
453 Note: When using ":split fname" the WinEnter
454 event is triggered after the split but before
455 the file "fname" is loaded.
456 *WinLeave*
457WinLeave Before leaving a window. If the window to be
458 entered next is for a different buffer, Vim
459 executes the BufLeave autocommands before the
460 WinLeave autocommands (but not for ":new").
461 Not used for ":qa" or ":q" when exiting Vim.
462 *CmdwinEnter*
463CmdwinEnter After entering the command-line window.
464 Useful for setting options specifically for
465 this special type of window. This is
466 triggered _instead_ of BufEnter and WinEnter.
467 <afile> is set to a single character,
468 indicating the type of command-line.
469 |cmdwin-char|
470 *CmdwinLeave*
471CmdwinLeave Before leaving the command-line window.
472 Useful to clean up any global setting done
473 with CmdwinEnter. This is triggered _instead_
474 of BufLeave and WinLeave.
475 <afile> is set to a single character,
476 indicating the type of command-line.
477 |cmdwin-char|
478 *GUIEnter*
479GUIEnter After starting the GUI successfully, and after
480 opening the window. It is triggered before
481 VimEnter when using gvim. Can be used to
482 position the window from a .gvimrc file: >
483 :autocmd GUIEnter * winpos 100 50
484< *VimEnter*
485VimEnter After doing all the startup stuff, including
486 loading .vimrc files, executing the "-c cmd"
487 arguments, creating all windows and loading
488 the buffers in them.
489 *VimLeavePre*
490VimLeavePre Before exiting Vim, just before writing the
491 .viminfo file. This is executed only once,
492 if there is a match with the name of what
493 happens to be the current buffer when exiting.
494 Mostly useful with a "*" pattern. >
495 :autocmd VimLeavePre * call CleanupStuff()
496< To detect an abnormal exit use |v:dying|.
497 *VimLeave*
498VimLeave Before exiting Vim, just after writing the
499 .viminfo file. Executed only once, like
500 VimLeavePre.
501 To detect an abnormal exit use |v:dying|.
502 *EncodingChanged*
Bram Moolenaar5a8684e2005-07-30 22:43:24 +0000503EncodingChanged Fires off after the 'encoding' option has been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 changed. Useful to set up fonts, for example.
Bram Moolenaar843ee412004-06-30 16:16:41 +0000505 *InsertEnter*
506InsertEnter When starting Insert mode. Also for Replace
507 mode and Virtual Replace mode. The
508 |v:insertmode| variable indicates the mode.
509 Be careful not to move the cursor or do
510 anything else that the user does not expect.
511 *InsertChange*
512InsertChange When typing <Insert> while in Insert or
513 Replace mode. The |v:insertmode| variable
514 indicates the new mode.
515 Be careful not to move the cursor or do
516 anything else that the user does not expect.
517 *InsertLeave*
518InsertLeave When leaving Insert mode. Also when using
Bram Moolenaar677ee682005-01-27 14:41:15 +0000519 CTRL-O |i_CTRL-O|. But not for |i_CTRL-C|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 *FileEncoding*
521FileEncoding Obsolete. It still works and is equivalent
522 to |EncodingChanged|.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000523 *ColorScheme*
524ColorScheme After loading a color scheme. |:colorscheme|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 *RemoteReply*
526RemoteReply When a reply from a Vim that functions as
527 server was received |server2client()|.
528 <amatch> is equal to the {serverid} from which
529 the reply was sent, and <afile> is the actual
530 reply string.
531 Note that even if an autocommand is defined,
532 the reply should be read with |remote_read()|
533 to consume it.
534 *TermChanged*
535TermChanged After the value of 'term' has changed. Useful
536 for re-loading the syntax file to update the
537 colors, fonts and other terminal-dependent
538 settings. Executed for all loaded buffers.
539 *TermResponse*
540TermResponse After the response to |t_RV| is received from
541 the terminal. The value of |v:termresponse|
542 can be used to do things depending on the
543 terminal version.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000544QuickFixCmdPre *QuickFixCmdPre*
545 Before a quickfix command is run (|:make|,
546 |:grep|, |:grepadd|, |:vimgrep|,
547 |:vimgrepadd|). The pattern is matched against
548 the command being run. When |:grep| is used
549 but 'grepprg' is set to "internal" it still
550 matches "grep".
551 This command cannot be used to set the
552 'makeprg' and 'grepprg' variables.
553 If this command causes an error, the quickfix
554 command is not executed.
555QuickFixCmdPost *QuickFixCmdPost*
556 like QuickFixCmdPre, but after a quickfix
557 command is run.
Bram Moolenaar45360022005-07-21 21:08:21 +0000558 *MenuPopup*
559MenuPopup Just before showing the popup menu (under the
560 right mouse button). Useful for adjusting the
561 menu for what is under the cursor or mouse
562 pointer.
563 The pattern is matched against a single
564 character representing the mode:
565 n Normal
566 v Visual
567 o Operator-pending
568 i Insert
569 c Commmand line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 *UserGettingBored*
571UserGettingBored When the user hits CTRL-C. Just kidding! :-)
572 *User*
573User Never executed automatically. To be used for
574 autocommands that are only executed with
575 ":doautocmd".
576
577You can specify a comma-separated list of event names. No white space can be
578used in this list. The command applies to all the events in the list.
579
580For READING FILES there are four kinds of events possible:
581 BufNewFile starting to edit a non-existent file
582 BufReadPre BufReadPost starting to edit an existing file
583 FilterReadPre FilterReadPost read the temp file with filter output
584 FileReadPre FileReadPost any other file read
585Vim uses only one of these four kinds when reading a file. The "Pre" and
586"Post" events are both triggered, before and after reading the file.
587
588Note that the autocommands for the *ReadPre events and all the Filter events
589are not allowed to change the current buffer (you will get an error message if
590this happens). This is to prevent the file to be read into the wrong buffer.
591
592Note that the 'modified' flag is reset AFTER executing the BufReadPost
593and BufNewFile autocommands. But when the 'modified' option was set by the
594autocommands, this doesn't happen.
595
596You can use the 'eventignore' option to ignore a number of events or all
597events.
598
599==============================================================================
6006. Patterns *autocmd-patterns* *{pat}*
601
602The file pattern {pat} is tested for a match against the file name in one of
603two ways:
6041. When there is no '/' in the pattern, Vim checks for a match against only
605 the tail part of the file name (without its leading directory path).
6062. When there is a '/' in the pattern, Vim checks for a match against the
607 both short file name (as you typed it) and the full file name (after
608 expanding it to a full path and resolving symbolic links).
609
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000610The special pattern <buffer> or <buffer=N> is used for buffer-local
611autocommands |autocmd-buflocal|. This pattern is not matched against the name
612of a buffer.
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614Examples: >
615 :autocmd BufRead *.txt set et
616Set the 'et' option for all text files. >
617
618 :autocmd BufRead /vim/src/*.c set cindent
619Set the 'cindent' option for C files in the /vim/src directory. >
620
621 :autocmd BufRead /tmp/*.c set ts=5
622If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", and
623you start editing "/tmp/test.c", this autocommand will match.
624
625Note: To match part of a path, but not from the root directory, use a '*' as
626the first character. Example: >
627 :autocmd BufRead */doc/*.txt set tw=78
628This autocommand will for example be executed for "/tmp/doc/xx.txt" and
629"/usr/home/piet/doc/yy.txt". The number of directories does not matter here.
630
631
632The file name that the pattern is matched against is after expanding
633wildcards. Thus is you issue this command: >
634 :e $ROOTDIR/main.$EXT
635The argument is first expanded to: >
636 /usr/root/main.py
637Before it's matched with the pattern of the autocommand. Careful with this
638when using events like FileReadCmd, the value of <amatch> may not be what you
639expect.
640
641
642Environment variables can be used in a pattern: >
643 :autocmd BufRead $VIMRUNTIME/doc/*.txt set expandtab
644And ~ can be used for the home directory (if $HOME is defined): >
645 :autocmd BufWritePost ~/.vimrc so ~/.vimrc
646 :autocmd BufRead ~archive/* set readonly
647The environment variable is expanded when the autocommand is defined, not when
648the autocommand is executed. This is different from the command!
649
650 *file-pattern*
651The pattern is interpreted like mostly used in file names:
652 * matches any sequence of characters
653 ? matches any single character
654 \? matches a '?'
655 . matches a '.'
656 ~ matches a '~'
657 , separates patterns
658 \, matches a ','
659 { } like \( \) in a |pattern|
660 , inside { }: like \| in a |pattern|
661 \ special meaning like in a |pattern|
662 [ch] matches 'c' or 'h'
663 [^ch] match any character but 'c' and 'h'
664
665Note that for all systems the '/' character is used for path separator (even
666MS-DOS and OS/2). This was done because the backslash is difficult to use
667in a pattern and to make the autocommands portable across different systems.
668
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000669 *autocmd-changes*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670Matching with the pattern is done when an event is triggered. Changing the
671buffer name in one of the autocommands, or even deleting the buffer, does not
672change which autocommands will be executed. Example: >
673
674 au BufEnter *.foo bdel
675 au BufEnter *.foo set modified
676
677This will delete the current buffer and then set 'modified' in what has become
678the current buffer instead. Vim doesn't take into account that "*.foo"
679doesn't match with that buffer name. It matches "*.foo" with the name of the
680buffer at the moment the event was triggered.
681
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000682However, buffer-local autocommands will not be executed for a buffer that has
683been wiped out with |:bwipe|. After deleting the buffer with |:bdel| the
684buffer actually still exists (it becomes unlisted), thus the autocommands are
685still executed.
686
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687==============================================================================
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006887. Buffer-local autocommands *autocmd-buflocal* *autocmd-buffer-local*
689 *<buffer=N>* *<buffer=abuf>* *E680*
690
691Buffer-local autocommands are attached to a specific buffer. They are useful
692if the buffer does not have a name and when the name does not match a specific
693pattern. But it also means they must be explicitly added to each buffer.
694
695Instead of a pattern buffer-local autocommands use one of these forms:
696 <buffer> current buffer
697 <buffer=99> buffer number 99
698 <buffer=abuf> using <abuf> (only when executing autocommands)
699 |<abuf>|
700
701Examples: >
702 :au CursorHold <buffer> echo 'hold'
703 :au CursorHold <buffer=33> echo 'hold'
704 :au CursorHold <buffer=abuf> echo 'hold'
705
706All the commands for autocommands also work with buffer-local autocommands,
707simply use the special string instead of the pattern. Examples: >
708 :au! * <buffer> " remove buffer-local autotommands for
709 " current buffer
710 :au! * <buffer=33> " remove buffer-local autotommands for
711 " buffer #33
712 :dobuf :au! CursorHold <buffer> " remove autocmd for given event for all
713 " buffers
714 :au * <buffer> " list buffer-local autocommands for
715 " current buffer
716
717Note that when an autocommand is defined for the current buffer, it is stored
718with the buffer number. Thus it uses the form "<buffer=12>", where 12 is the
719number of the current buffer. You will see this when listing autocommands,
720for example.
721
722To test for presence of buffer-local autocommands use the |exists()| function
723as follows: >
724 :if exists("#CursorHold#<buffer=12>") | ... | endif
725 :if exists("#CursorHold#<buffer>") | ... | endif " for current buffer
726
727When a buffer is wiped out its buffer-local autocommands are also gone, of
728course. Note that when deleting a buffer, e.g., with ":bdel", it is only
729unlisted, the autocommands are still present. In order to see the removal of
730buffer-local autocommands: >
731 :set verbose=6
732
733It is not possible to define buffer-local autocommands for a non-existent
734buffer.
735
736==============================================================================
7378. Groups *autocmd-groups*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
739Autocommands can be put together in a group. This is useful for removing or
740executing a group of autocommands. For example, all the autocommands for
741syntax highlighting are put in the "highlight" group, to be able to execute
742":doautoall highlight BufRead" when the GUI starts.
743
744When no specific group is selected, Vim uses the default group. The default
745group does not have a name. You cannot execute the autocommands from the
746default group separately; you can execute them only by executing autocommands
747for all groups.
748
749Normally, when executing autocommands automatically, Vim uses the autocommands
750for all groups. The group only matters when executing autocommands with
751":doautocmd" or ":doautoall", or when defining or deleting autocommands.
752
753The group name can contain any characters except white space. The group name
754"end" is reserved (also in uppercase).
755
756The group name is case sensitive. Note that this is different from the event
757name!
758
759 *:aug* *:augroup*
760:aug[roup] {name} Define the autocmd group name for the
761 following ":autocmd" commands. The name "end"
762 or "END" selects the default group.
763
764 *:augroup-delete* *E367*
765:aug[roup]! {name} Delete the autocmd group {name}. Don't use
766 this if there is still an autocommand using
767 this group! This is not checked.
768
769To enter autocommands for a specific group, use this method:
7701. Select the group with ":augroup {name}".
7712. Delete any old autocommands with ":au!".
7723. Define the autocommands.
7734. Go back to the default group with "augroup END".
774
775Example: >
776 :augroup uncompress
777 : au!
778 : au BufEnter *.gz %!gunzip
779 :augroup END
780
781This prevents having the autocommands defined twice (e.g., after sourcing the
782.vimrc file again).
783
784==============================================================================
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007859. Executing autocommands *autocmd-execute*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787Vim can also execute Autocommands non-automatically. This is useful if you
788have changed autocommands, or when Vim has executed the wrong autocommands
789(e.g., the file pattern match was wrong).
790
791Note that the 'eventignore' option applies here too. Events listed in this
792option will not cause any commands to be executed.
793
794 *:do* *:doau* *:doautocmd* *E217*
795:do[autocmd] [group] {event} [fname]
796 Apply the autocommands matching [fname] (default:
797 current file name) for {event} to the current buffer.
798 You can use this when the current file name does not
799 match the right pattern, after changing settings, or
800 to execute autocommands for a certain event.
801 It's possible to use this inside an autocommand too,
802 so you can base the autocommands for one extension on
803 another extension. Example: >
804 :au Bufenter *.cpp so ~/.vimrc_cpp
805 :au Bufenter *.cpp doau BufEnter x.c
806< Be careful to avoid endless loops. See
807 |autocmd-nested|.
808
809 When the [group] argument is not given, Vim executes
810 the autocommands for all groups. When the [group]
811 argument is included, Vim executes only the matching
812 autocommands for that group. Note: if you use an
813 undefined group name, Vim gives you an error message.
814
815 *:doautoa* *:doautoall*
816:doautoa[ll] [group] {event} [fname]
817 Like ":doautocmd", but apply the autocommands to each
818 loaded buffer. Note that {fname} is used to select
819 the autocommands, not the buffers to which they are
820 applied.
821 Careful: Don't use this for autocommands that delete a
822 buffer, change to another buffer or change the
823 contents of a buffer; the result is unpredictable.
824 This command is intended for autocommands that set
825 options, change highlighting, and things like that.
826
827==============================================================================
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000082810. Using autocommands *autocmd-use*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
830For WRITING FILES there are four possible sets of events. Vim uses only one
831of these sets for a write command:
832
833BufWriteCmd BufWritePre BufWritePost writing the whole buffer
834 FilterWritePre FilterWritePost writing to filter temp file
835FileAppendCmd FileAppendPre FileAppendPost appending to a file
836FileWriteCmd FileWritePre FileWritePost any other file write
837
838When there is a matching "*Cmd" autocommand, it is assumed it will do the
839writing. No further writing is done and the other events are not triggered.
840|Cmd-event|
841
842Note that the *WritePost commands should undo any changes to the buffer that
843were caused by the *WritePre commands; otherwise, writing the file will have
844the side effect of changing the buffer.
845
846Before executing the autocommands, the buffer from which the lines are to be
847written temporarily becomes the current buffer. Unless the autocommands
848change the current buffer or delete the previously current buffer, the
849previously current buffer is made the current buffer again.
850
851The *WritePre and *AppendPre autocommands must not delete the buffer from
852which the lines are to be written.
853
854The '[ and '] marks have a special position:
855- Before the *ReadPre event the '[ mark is set to the line just above where
856 the new lines will be inserted.
857- Before the *ReadPost event the '[ mark is set to the first line that was
858 just read, the '] mark to the last line.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000859- Before executing the *WriteCmd, *WritePre and *AppendPre autocommands the '[
860 mark is set to the first line that will be written, the '] mark to the last
861 line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862Careful: '[ and '] change when using commands that change the buffer.
863
864In commands which expect a file name, you can use "<afile>" for the file name
865that is being read |:<afile>| (you can also use "%" for the current file
866name). "<abuf>" can be used for the buffer number of the currently effective
867buffer. This also works for buffers that doesn't have a name. But it doesn't
868work for files without a buffer (e.g., with ":r file").
869
870 *gzip-example*
871Examples for reading and writing compressed files: >
872 :augroup gzip
873 : autocmd!
874 : autocmd BufReadPre,FileReadPre *.gz set bin
875 : autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip
876 : autocmd BufReadPost,FileReadPost *.gz set nobin
877 : autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " . expand("%:r")
878 : autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r
879 : autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r
880
881 : autocmd FileAppendPre *.gz !gunzip <afile>
882 : autocmd FileAppendPre *.gz !mv <afile>:r <afile>
883 : autocmd FileAppendPost *.gz !mv <afile> <afile>:r
884 : autocmd FileAppendPost *.gz !gzip <afile>:r
885 :augroup END
886
887The "gzip" group is used to be able to delete any existing autocommands with
888":autocmd!", for when the file is sourced twice.
889
890("<afile>:r" is the file name without the extension, see |:_%:|)
891
892The commands executed for the BufNewFile, BufRead/BufReadPost, BufWritePost,
893FileAppendPost and VimLeave events do not set or reset the changed flag of the
894buffer. When you decompress the buffer with the BufReadPost autocommands, you
895can still exit with ":q". When you use ":undo" in BufWritePost to undo the
896changes made by BufWritePre commands, you can still do ":q" (this also makes
897"ZZ" work). If you do want the buffer to be marked as modified, set the
898'modified' option.
899
900To execute Normal mode commands from an autocommand, use the ":normal"
901command. Use with care! If the Normal mode command is not finished, the user
902needs to type characters (e.g., after ":normal m" you need to type a mark
903name).
904
905If you want the buffer to be unmodified after changing it, reset the
906'modified' option. This makes it possible to exit the buffer with ":q"
907instead of ":q!".
908
909 *autocmd-nested* *E218*
910By default, autocommands do not nest. If you use ":e" or ":w" in an
911autocommand, Vim does not execute the BufRead and BufWrite autocommands for
912those commands. If you do want this, use the "nested" flag for those commands
913in which you want nesting. For example: >
914 :autocmd FileChangedShell *.c nested e!
915The nesting is limited to 10 levels to get out of recursive loops.
916
917It's possible to use the ":au" command in an autocommand. This can be a
918self-modifying command! This can be useful for an autocommand that should
919execute only once.
920
921There is currently no way to disable the autocommands. If you want to write a
922file without executing the autocommands for that type of file, write it under
923another name and rename it with a shell command. In some situations you can
924use the 'eventignore' option.
925
926Note: When reading a file (with ":read file" or with a filter command) and the
927last line in the file does not have an <EOL>, Vim remembers this. At the next
928write (with ":write file" or with a filter command), if the same line is
929written again as the last line in a file AND 'binary' is set, Vim does not
930supply an <EOL>. This makes a filter command on the just read lines write the
931same file as was read, and makes a write command on just filtered lines write
932the same file as was read from the filter. For example, another way to write
933a compressed file: >
934
935 :autocmd FileWritePre *.gz set bin|'[,']!gzip
936 :autocmd FileWritePost *.gz undo|set nobin
937<
938 *autocommand-pattern*
939You can specify multiple patterns, separated by commas. Here are some
940examples: >
941
942 :autocmd BufRead * set tw=79 nocin ic infercase fo=2croq
943 :autocmd BufRead .letter set tw=72 fo=2tcrq
944 :autocmd BufEnter .letter set dict=/usr/lib/dict/words
945 :autocmd BufLeave .letter set dict=
946 :autocmd BufRead,BufNewFile *.c,*.h set tw=0 cin noic
947 :autocmd BufEnter *.c,*.h abbr FOR for (i = 0; i < 3; ++i)<CR>{<CR>}<Esc>O
948 :autocmd BufLeave *.c,*.h unabbr FOR
949
950For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.): >
951
952 :autocmd BufEnter ?akefile* set include=^s\=include
953 :autocmd BufLeave ?akefile* set include&
954
955To always start editing C files at the first function: >
956
957 :autocmd BufRead *.c,*.h 1;/^{
958
959Without the "1;" above, the search would start from wherever the file was
960entered, rather than from the start of the file.
961
962 *skeleton* *template*
963To read a skeleton (template) file when opening a new file: >
964
965 :autocmd BufNewFile *.c 0r ~/vim/skeleton.c
966 :autocmd BufNewFile *.h 0r ~/vim/skeleton.h
967 :autocmd BufNewFile *.java 0r ~/vim/skeleton.java
968
969To insert the current date and time in a *.html file when writing it: >
970
971 :autocmd BufWritePre,FileWritePre *.html ks|call LastMod()|'s
972 :fun LastMod()
973 : if line("$") > 20
974 : let l = 20
975 : else
976 : let l = line("$")
977 : endif
978 : exe "1," . l . "g/Last modified: /s/Last modified: .*/Last modified: " .
979 : \ strftime("%Y %b %d")
980 :endfun
981
982You need to have a line "Last modified: <date time>" in the first 20 lines
983of the file for this to work. Vim replaces <date time> (and anything in the
984same line after it) with the current date and time. Explanation:
985 ks mark current position with mark 's'
986 call LastMod() call the LastMod() function to do the work
987 's return the cursor to the old position
988The LastMod() function checks if the file is shorter than 20 lines, and then
989uses the ":g" command to find lines that contain "Last modified: ". For those
990lines the ":s" command is executed to replace the existing date with the
991current one. The ":execute" command is used to be able to use an expression
992for the ":g" and ":s" commands. The date is obtained with the strftime()
993function. You can change its argument to get another date string.
994
995When entering :autocmd on the command-line, completion of events and command
996names may be done (with <Tab>, CTRL-D, etc.) where appropriate.
997
998Vim executes all matching autocommands in the order that you specify them.
999It is recommended that your first autocommand be used for all files by using
1000"*" as the file pattern. This means that you can define defaults you like
1001here for any settings, and if there is another matching autocommand it will
1002override these. But if there is no other matching autocommand, then at least
1003your default settings are recovered (if entering this file from another for
1004which autocommands did match). Note that "*" will also match files starting
1005with ".", unlike Unix shells.
1006
1007 *autocmd-searchpat*
1008Autocommands do not change the current search patterns. Vim saves the current
1009search patterns before executing autocommands then restores them after the
1010autocommands finish. This means that autocommands do not affect the strings
1011highlighted with the 'hlsearch' option. Within autocommands, you can still
1012use search patterns normally, e.g., with the "n" command.
1013If you want an autocommand to set the search pattern, such that it is used
1014after the autocommand finishes, use the ":let @/ =" command.
1015The search-highlighting cannot be switched off with ":nohlsearch" in an
1016autocommand. Use the 'h' flag in the 'viminfo' option to disable search-
1017highlighting when starting Vim.
1018
1019 *Cmd-event*
1020When using one of the "*Cmd" events, the matching autocommands are expected to
1021do the file reading or writing. This can be used when working with a special
1022kind of file, for example on a remote system.
1023CAREFUL: If you use these events in a wrong way, it may have the effect of
1024making it impossible to read or write the matching files! Make sure you test
1025your autocommands properly. Best is to use a pattern that will never match a
1026normal file name, for example "ftp://*".
1027
1028When defining a BufReadCmd it will be difficult for Vim to recover a crashed
1029editing session. When recovering from the original file, Vim reads only those
1030parts of a file that are not found in the swap file. Since that is not
1031possible with a BufReadCmd, use the |:preserve| command to make sure the
1032original file isn't needed for recovery. You might want to do this only when
1033you expect the file to be modified.
1034
1035The |v:cmdarg| variable holds the "++enc=" and "++ff=" argument that are
1036effective. These should be used for the command that reads/writes the file.
1037The |v:cmdbang| variable is one when "!" was used, zero otherwise.
1038
1039See the $VIMRUNTIME/plugin/netrw.vim for examples.
1040
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 vim:tw=78:ts=8:ft=help:norl: