blob: edf3c1e54ebfcaf8f6bcf75583dd3e4be91eb748 [file] [log] [blame]
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001*autocmd.txt* For Vim version 7.0aa. Last change: 2006 Mar 07
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
165 * call s:LocalBrowse(expand("<amatch>"))
166 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
199alpabetically with full explanations |autocmd-events-abc|.
200
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
212|FileReadCmd| before reading a file with a ":read" comman |Cmd-event|
213
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
266|TermResponse| after the termainal response to |t_RV| is received
267
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
273|FileChangedRO| before making the first change to a read-only file
274
275|FuncUndefined| a user function is used but it isn't defined
Bram Moolenaarafeb4fa2006-02-01 21:51:12 +0000276|SpellFileMissing| a spell file is used but it can't be found
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000277|SourcePre| before sourcing a Vim script
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000278
279|FocusGained| Vim got input focus
280|FocusLost| Vim lost input focus
281|CursorHold| the user doesn't press a key for a while
Bram Moolenaar754b5602006-02-09 23:53:20 +0000282|CursorHoldI| the user doesn't press a key for a while in Insert mode
283|CursorMoved| the cursor was moved in Normal mode
284|CursorMovedI| the cursor was moved in Insert mode
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000285
286|WinEnter| after entering another window
287|WinLeave| before leaving a window
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000288|TabEnter| after entering another tab page
289|TabLeave| before leaving a tab page
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000290|CmdwinEnter| after entering the command-line window
291|CmdwinLeave| before leaving the command-line window
292
293|InsertEnter| starting Insert mode
294|InsertChange| when typing <Insert> while in Insert or Replace mode
295|InsertLeave| when leaving Insert mode
296
297|ColorScheme| after loading a color scheme
298
299|RemoteReply| a reply from a server Vim was received
300
301|QuickFixCmdPre| before a quickfix command is run
302|QuickFixCmdPost| after a quickfix command is run
303
304|SessionLoadPost| after loading a session file
305
306|MenuPopup| just before showing the popup menu
307
308|User| to be used in combination with ":doautocmd"
309
310
311The alphabetical list of autocommand events: *autocmd-events-abc*
312
313 *BufCreate* *BufAdd*
314BufAdd or BufCreate Just after creating a new buffer which is
315 added to the buffer list, or adding a buffer
316 to the buffer list.
317 Also used just after a buffer in the buffer
318 list has been renamed.
319 The BufCreate event is for historic reasons.
320 NOTE: When this autocommand is executed, the
321 current buffer "%" may be different from the
322 buffer being created "<afile>".
323 *BufDelete*
324BufDelete Before deleting a buffer from the buffer list.
325 The BufUnload may be called first (if the
326 buffer was loaded).
327 Also used just before a buffer in the buffer
328 list is renamed.
329 NOTE: When this autocommand is executed, the
330 current buffer "%" may be different from the
331 buffer being deleted "<afile>".
332 *BufEnter*
333BufEnter After entering a buffer. Useful for setting
334 options for a file type. Also executed when
335 starting to edit a buffer, after the
336 BufReadPost autocommands.
337 *BufFilePost*
338BufFilePost After changing the name of the current buffer
339 with the ":file" or ":saveas" command.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000340 *BufFilePre*
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000341BufFilePre Before changing the name of the current buffer
342 with the ":file" or ":saveas" command.
343 *BufHidden*
344BufHidden Just after a buffer has become hidden. That
345 is, when there are no longer windows that show
346 the buffer, but the buffer is not unloaded or
347 deleted. Not used for ":qa" or ":q" when
348 exiting Vim.
349 NOTE: When this autocommand is executed, the
350 current buffer "%" may be different from the
351 buffer being unloaded "<afile>".
352 *BufLeave*
353BufLeave Before leaving to another buffer. Also when
354 leaving or closing the current window and the
355 new current window is not for the same buffer.
356 Not used for ":qa" or ":q" when exiting Vim.
357 *BufNew*
358BufNew Just after creating a new buffer. Also used
359 just after a buffer has been renamed. When
360 the buffer is added to the buffer list BufAdd
361 will be triggered too.
362 NOTE: When this autocommand is executed, the
363 current buffer "%" may be different from the
364 buffer being created "<afile>".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 *BufNewFile*
366BufNewFile When starting to edit a file that doesn't
367 exist. Can be used to read in a skeleton
368 file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 *BufRead* *BufReadPost*
370BufRead or BufReadPost When starting to edit a new buffer, after
371 reading the file into the buffer, before
372 executing the modelines. See |BufWinEnter|
373 for when you need to do something after
374 processing the modelines.
375 This does NOT work for ":r file". Not used
376 when the file doesn't exist. Also used after
377 successfully recovering a file.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000378 *BufReadCmd*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379BufReadCmd Before starting to edit a new buffer. Should
380 read the file into the buffer. |Cmd-event|
Bram Moolenaar4770d092006-01-12 23:22:24 +0000381 *BufReadPre* *E200* *E201*
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000382BufReadPre When starting to edit a new buffer, before
383 reading the file into the buffer. Not used
384 if the file doesn't exist.
385 *BufUnload*
386BufUnload Before unloading a buffer. This is when the
387 text in the buffer is going to be freed. This
388 may be after a BufWritePost and before a
389 BufDelete. Also used for all buffers that are
390 loaded when Vim is going to exit.
391 NOTE: When this autocommand is executed, the
392 current buffer "%" may be different from the
393 buffer being unloaded "<afile>".
394 *BufWinEnter*
395BufWinEnter After a buffer is displayed in a window. This
396 can be when the buffer is loaded (after
397 processing the modelines), when a hidden
398 buffer is displayed in a window (and is no
399 longer hidden) or a buffer already visible in
400 a window is also displayed in another window.
401 *BufWinLeave*
402BufWinLeave Before a buffer is removed from a window.
403 Not when it's still visible in another window.
404 Also triggered when exiting. It's triggered
405 before BufUnload or BufHidden.
406 NOTE: When this autocommand is executed, the
407 current buffer "%" may be different from the
408 buffer being unloaded "<afile>".
409 *BufWipeout*
410BufWipeout Before completely deleting a buffer. The
411 BufUnload and BufDelete events may be called
412 first (if the buffer was loaded and was in the
413 buffer list). Also used just before a buffer
414 is renamed (also when it's not in the buffer
415 list).
416 NOTE: When this autocommand is executed, the
417 current buffer "%" may be different from the
418 buffer being deleted "<afile>".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 *BufWrite* *BufWritePre*
420BufWrite or BufWritePre Before writing the whole buffer to a file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 *BufWriteCmd*
422BufWriteCmd Before writing the whole buffer to a file.
423 Should do the writing of the file and reset
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000424 'modified' if successful, unless '+' is in
425 'cpo' and writing to another file |cpo-+|.
426 The buffer contents should not be changed.
427 |Cmd-event|
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000428 *BufWritePost*
429BufWritePost After writing the whole buffer to a file
430 (should undo the commands for BufWritePre).
431 *CmdwinEnter*
432CmdwinEnter After entering the command-line window.
433 Useful for setting options specifically for
434 this special type of window. This is
435 triggered _instead_ of BufEnter and WinEnter.
436 <afile> is set to a single character,
437 indicating the type of command-line.
438 |cmdwin-char|
439 *CmdwinLeave*
440CmdwinLeave Before leaving the command-line window.
441 Useful to clean up any global setting done
442 with CmdwinEnter. This is triggered _instead_
443 of BufLeave and WinLeave.
444 <afile> is set to a single character,
445 indicating the type of command-line.
446 |cmdwin-char|
447 *ColorScheme*
448ColorScheme After loading a color scheme. |:colorscheme|
Bram Moolenaar754b5602006-02-09 23:53:20 +0000449
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000450 *CursorHold*
451CursorHold When the user doesn't press a key for the time
452 specified with 'updatetime'. Not re-triggered
453 until the user has pressed a key (i.e. doesn't
454 fire every 'updatetime' ms if you leave Vim to
455 make some coffee. :) See |CursorHold-example|
456 for previewing tags.
457 This event is only triggered in Normal mode.
Bram Moolenaare3226be2005-12-18 22:10:00 +0000458 While recording the CursorHold event is not
459 triggered. |q|
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000460 Note: Interactive commands cannot be used for
461 this event. There is no hit-enter prompt,
462 the screen is updated directly (when needed).
463 Note: In the future there will probably be
464 another option to set the time.
465 Hint: to force an update of the status lines
466 use: >
467 :let &ro = &ro
468< {only on Amiga, Unix, Win32, MSDOS and all GUI
469 versions}
Bram Moolenaar754b5602006-02-09 23:53:20 +0000470 *CursorHoldI*
471CursorHoldI Just like CursorHold, but in Insert mode.
472
473 *CursorMoved*
474CursorMoved After the cursor was moved in Normal mode.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000475 Also when the text of the cursor line has been
476 changed, e.g., with "x", "rx" or "p".
Bram Moolenaar754b5602006-02-09 23:53:20 +0000477 Not triggered when there is typeahead or when
478 an operator is pending.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000479 For an example see |match-parens|.
Bram Moolenaar754b5602006-02-09 23:53:20 +0000480 Careful: Don't do anything that the user does
481 not expect or that is slow.
482 *CursorMovedI*
483CursorMovedI After the cursor was moved in Insert mode.
484 Otherwise the same as CursorMoved.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000485 *EncodingChanged*
486EncodingChanged Fires off after the 'encoding' option has been
487 changed. Useful to set up fonts, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 *FileAppendCmd*
489FileAppendCmd Before appending to a file. Should do the
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000490 appending to the file. Use the '[ and ']
491 marks for the range of lines.|Cmd-event|
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000492 *FileAppendPost*
493FileAppendPost After appending to a file.
494 *FileAppendPre*
495FileAppendPre Before appending to a file. Use the '[ and ']
496 marks for the range of lines.
497 *FileChangedRO*
498FileChangedRO Before making the first change to a read-only
499 file. Can be used to check-out the file from
500 a source control system. Not triggered when
501 the change was caused by an autocommand.
502 This event is triggered when making the first
503 change in a buffer or the first change after
504 'readonly' was set,
505 just before the change is applied to the text.
506 WARNING: If the autocommand moves the cursor
507 the effect of the change is undefined.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 *FileChangedShell*
509FileChangedShell When Vim notices that the modification time of
510 a file has changed since editing started.
511 Also when the file attributes of the file
512 change. |timestamp|
513 Mostly triggered after executing a shell
514 command, but also with a |:checktime| command
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000515 or when Gvim regains input focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 This autocommand is triggered for each changed
517 file. It is not used when 'autoread' is set
518 and the buffer was not changed. If a
519 FileChangedShell autocommand is present the
520 warning message and prompt is not given.
521 This is useful for reloading related buffers
522 which are affected by a single command.
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000523 The |v:fcs_reason| variable is set to indicate
524 what happened and |v:fcs_choice| can be used
525 to tell Vim what to do next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 NOTE: When this autocommand is executed, the
527 current buffer "%" may be different from the
528 buffer that was changed "<afile>".
529 NOTE: The commands must not change the current
530 buffer, jump to another buffer or delete a
531 buffer. *E246*
532 NOTE: This event never nests, to avoid an
533 endless loop. This means that while executing
534 commands for the FileChangedShell event no
535 other FileChangedShell event will be
536 triggered.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000537 *FileEncoding*
538FileEncoding Obsolete. It still works and is equivalent
539 to |EncodingChanged|.
540 *FileReadCmd*
541FileReadCmd Before reading a file with a ":read" command.
542 Should do the reading of the file. |Cmd-event|
543 *FileReadPost*
544FileReadPost After reading a file with a ":read" command.
545 Note that Vim sets the '[ and '] marks to the
546 first and last line of the read. This can be
547 used to operate on the lines just read.
548 *FileReadPre*
549FileReadPre Before reading a file with a ":read" command.
550 *FileType*
551FileType When the 'filetype' option has been set.
552 <afile> can be used for the name of the file
553 where this option was set, and <amatch> for
554 the new value of 'filetype'.
555 See |filetypes|.
556 *FileWriteCmd*
557FileWriteCmd Before writing to a file, when not writing the
558 whole buffer. Should do the writing to the
559 file. Should not change the buffer. Use the
560 '[ and '] marks for the range of lines.
561 |Cmd-event|
562 *FileWritePost*
563FileWritePost After writing to a file, when not writing the
564 whole buffer.
565 *FileWritePre*
566FileWritePre Before writing to a file, when not writing the
567 whole buffer. Use the '[ and '] marks for the
568 range of lines.
569 *FilterReadPost*
570FilterReadPost After reading a file from a filter command.
571 Vim checks the pattern against the name of
572 the current buffer as with FilterReadPre.
573 Not triggered when 'shelltemp' is off.
574 *FilterReadPre* *E135*
575FilterReadPre Before reading a file from a filter command.
576 Vim checks the pattern against the name of
577 the current buffer, not the name of the
578 temporary file that is the output of the
579 filter command.
580 Not triggered when 'shelltemp' is off.
581 *FilterWritePost*
582FilterWritePost After writing a file for a filter command or
583 making a diff.
584 Vim checks the pattern against the name of
585 the current buffer as with FilterWritePre.
586 Not triggered when 'shelltemp' is off.
587 *FilterWritePre*
588FilterWritePre Before writing a file for a filter command or
589 making a diff.
590 Vim checks the pattern against the name of
591 the current buffer, not the name of the
592 temporary file that is the output of the
593 filter command.
594 Not triggered when 'shelltemp' is off.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 *FocusGained*
596FocusGained When Vim got input focus. Only for the GUI
597 version and a few console versions where this
598 can be detected.
599 *FocusLost*
600FocusLost When Vim lost input focus. Only for the GUI
601 version and a few console versions where this
Bram Moolenaar843ee412004-06-30 16:16:41 +0000602 can be detected. May also happen when a
603 dialog pops up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 *FuncUndefined*
605FuncUndefined When a user function is used but it isn't
606 defined. Useful for defining a function only
607 when it's used. Both <amatch> and <afile> are
608 set to the name of the function.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000609 See |autoload-functions|.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000610 *GUIEnter*
611GUIEnter After starting the GUI successfully, and after
612 opening the window. It is triggered before
613 VimEnter when using gvim. Can be used to
614 position the window from a .gvimrc file: >
615 :autocmd GUIEnter * winpos 100 50
616< *InsertChange*
617InsertChange When typing <Insert> while in Insert or
618 Replace mode. The |v:insertmode| variable
619 indicates the new mode.
620 Be careful not to move the cursor or do
621 anything else that the user does not expect.
622 *InsertEnter*
623InsertEnter When starting Insert mode. Also for Replace
624 mode and Virtual Replace mode. The
625 |v:insertmode| variable indicates the mode.
626 Be careful not to move the cursor or do
627 anything else that the user does not expect.
628 *InsertLeave*
629InsertLeave When leaving Insert mode. Also when using
630 CTRL-O |i_CTRL-O|. But not for |i_CTRL-C|.
631 *MenuPopup*
632MenuPopup Just before showing the popup menu (under the
633 right mouse button). Useful for adjusting the
634 menu for what is under the cursor or mouse
635 pointer.
636 The pattern is matched against a single
637 character representing the mode:
638 n Normal
639 v Visual
640 o Operator-pending
641 i Insert
642 c Commmand line
643 *QuickFixCmdPre*
644QuickFixCmdPre Before a quickfix command is run (|:make|,
Bram Moolenaara6557602006-02-04 22:43:20 +0000645 |:lmake|, |:grep|, |:lgrep|, |:grepadd|,
646 |:lgrepadd|, |:vimgrep|, |:lvimgrep|,
647 |:vimgrepadd|, |:vimgrepadd|). The pattern is
648 matched against the command being run. When
649 |:grep| is used but 'grepprg' is set to
650 "internal" it still matches "grep".
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000651 This command cannot be used to set the
652 'makeprg' and 'grepprg' variables.
653 If this command causes an error, the quickfix
654 command is not executed.
655 *QuickFixCmdPost*
656QuickFixCmdPost Like QuickFixCmdPre, but after a quickfix
657 command is run.
658 *RemoteReply*
659RemoteReply When a reply from a Vim that functions as
660 server was received |server2client()|.
661 <amatch> is equal to the {serverid} from which
662 the reply was sent, and <afile> is the actual
663 reply string.
664 Note that even if an autocommand is defined,
665 the reply should be read with |remote_read()|
666 to consume it.
667 *SessionLoadPost*
668SessionLoadPost After loading the session file created using
669 the |:mksession| command.
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000670 *SourcePre*
671SourcePre Before sourcing a Vim script. |:source|
Bram Moolenaarafeb4fa2006-02-01 21:51:12 +0000672 *SpellFileMissing*
673SpellFileMissing When trying to load a spell checking file and
674 it can't be found. <amatch> is the language,
675 'encoding' also matters. See
676 |spell-SpellFileMissing|.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000677 *StdinReadPost*
678StdinReadPost After reading from the stdin into the buffer,
679 before executing the modelines. Only used
680 when the "-" argument was used when Vim was
681 started |--|.
682 *StdinReadPre*
683StdinReadPre Before reading from stdin into the buffer.
684 Only used when the "-" argument was used when
685 Vim was started |--|.
686 *SwapExists*
687SwapExists Detected an existing swap file when starting
688 to edit a file. Only when it is possible to
689 select a way to handle the situation, when Vim
690 would ask the user what to do.
691 The |v:swapname| variable holds the name of
Bram Moolenaarb3480382005-12-11 21:33:32 +0000692 the swap file found, <afile> the file being
693 edited. |v:swapcommand| may contain a command
694 to be executed in the opened file.
695 The commands should set the |v:swapchoice|
696 variable to a string with one character to
697 tell Vim what should be done next:
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000698 'o' open read-only
699 'e' edit the file anyway
700 'r' recover
701 'd' delete the swap file
702 'q' quit, don't edit the file
703 'a' abort, like hitting CTRL-C
704 When set to an empty string the user will be
705 asked, as if there was no SwapExists autocmd.
706 Note: Do not try to change the buffer, the
707 results are unpredictable.
708 *Syntax*
709Syntax When the 'syntax' option has been set.
710 <afile> can be used for the name of the file
711 where this option was set, and <amatch> for
712 the new value of 'syntax'.
713 See |:syn-on|.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000714 *TabEnter*
715TabEnter Just after entering a tab page. |tab-page|
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000716 Before triggering the WinEnter and BufEnter
717 events.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000718 *TabLeave*
719TabLeave Just before leaving a tab page. |tab-page|
720 A WinLeave event will have been triggered
721 first.
Bram Moolenaar4e330bb2005-12-07 21:04:31 +0000722 *TermChanged*
723TermChanged After the value of 'term' has changed. Useful
724 for re-loading the syntax file to update the
725 colors, fonts and other terminal-dependent
726 settings. Executed for all loaded buffers.
727 *TermResponse*
728TermResponse After the response to |t_RV| is received from
729 the terminal. The value of |v:termresponse|
730 can be used to do things depending on the
731 terminal version.
732 *User*
733User Never executed automatically. To be used for
734 autocommands that are only executed with
735 ":doautocmd".
736 *UserGettingBored*
737UserGettingBored When the user hits CTRL-C. Just kidding! :-)
738 *VimEnter*
739VimEnter After doing all the startup stuff, including
740 loading .vimrc files, executing the "-c cmd"
741 arguments, creating all windows and loading
742 the buffers in them.
743 *VimLeave*
744VimLeave Before exiting Vim, just after writing the
745 .viminfo file. Executed only once, like
746 VimLeavePre.
747 To detect an abnormal exit use |v:dying|.
748 *VimLeavePre*
749VimLeavePre Before exiting Vim, just before writing the
750 .viminfo file. This is executed only once,
751 if there is a match with the name of what
752 happens to be the current buffer when exiting.
753 Mostly useful with a "*" pattern. >
754 :autocmd VimLeavePre * call CleanupStuff()
755< To detect an abnormal exit use |v:dying|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 *WinEnter*
757WinEnter After entering another window. Not done for
758 the first window, when Vim has just started.
759 Useful for setting the window height.
760 If the window is for another buffer, Vim
761 executes the BufEnter autocommands after the
762 WinEnter autocommands.
763 Note: When using ":split fname" the WinEnter
764 event is triggered after the split but before
765 the file "fname" is loaded.
766 *WinLeave*
767WinLeave Before leaving a window. If the window to be
768 entered next is for a different buffer, Vim
769 executes the BufLeave autocommands before the
770 WinLeave autocommands (but not for ":new").
771 Not used for ":qa" or ":q" when exiting Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
773==============================================================================
7746. Patterns *autocmd-patterns* *{pat}*
775
776The file pattern {pat} is tested for a match against the file name in one of
777two ways:
7781. When there is no '/' in the pattern, Vim checks for a match against only
779 the tail part of the file name (without its leading directory path).
7802. When there is a '/' in the pattern, Vim checks for a match against the
781 both short file name (as you typed it) and the full file name (after
782 expanding it to a full path and resolving symbolic links).
783
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000784The special pattern <buffer> or <buffer=N> is used for buffer-local
785autocommands |autocmd-buflocal|. This pattern is not matched against the name
786of a buffer.
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788Examples: >
789 :autocmd BufRead *.txt set et
790Set the 'et' option for all text files. >
791
792 :autocmd BufRead /vim/src/*.c set cindent
793Set the 'cindent' option for C files in the /vim/src directory. >
794
795 :autocmd BufRead /tmp/*.c set ts=5
796If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", and
797you start editing "/tmp/test.c", this autocommand will match.
798
799Note: To match part of a path, but not from the root directory, use a '*' as
800the first character. Example: >
801 :autocmd BufRead */doc/*.txt set tw=78
802This autocommand will for example be executed for "/tmp/doc/xx.txt" and
803"/usr/home/piet/doc/yy.txt". The number of directories does not matter here.
804
805
806The file name that the pattern is matched against is after expanding
807wildcards. Thus is you issue this command: >
808 :e $ROOTDIR/main.$EXT
809The argument is first expanded to: >
810 /usr/root/main.py
811Before it's matched with the pattern of the autocommand. Careful with this
812when using events like FileReadCmd, the value of <amatch> may not be what you
813expect.
814
815
816Environment variables can be used in a pattern: >
817 :autocmd BufRead $VIMRUNTIME/doc/*.txt set expandtab
818And ~ can be used for the home directory (if $HOME is defined): >
819 :autocmd BufWritePost ~/.vimrc so ~/.vimrc
820 :autocmd BufRead ~archive/* set readonly
821The environment variable is expanded when the autocommand is defined, not when
822the autocommand is executed. This is different from the command!
823
824 *file-pattern*
825The pattern is interpreted like mostly used in file names:
826 * matches any sequence of characters
827 ? matches any single character
828 \? matches a '?'
829 . matches a '.'
830 ~ matches a '~'
831 , separates patterns
832 \, matches a ','
833 { } like \( \) in a |pattern|
834 , inside { }: like \| in a |pattern|
835 \ special meaning like in a |pattern|
836 [ch] matches 'c' or 'h'
837 [^ch] match any character but 'c' and 'h'
838
839Note that for all systems the '/' character is used for path separator (even
840MS-DOS and OS/2). This was done because the backslash is difficult to use
841in a pattern and to make the autocommands portable across different systems.
842
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000843 *autocmd-changes*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844Matching with the pattern is done when an event is triggered. Changing the
845buffer name in one of the autocommands, or even deleting the buffer, does not
846change which autocommands will be executed. Example: >
847
848 au BufEnter *.foo bdel
849 au BufEnter *.foo set modified
850
851This will delete the current buffer and then set 'modified' in what has become
852the current buffer instead. Vim doesn't take into account that "*.foo"
853doesn't match with that buffer name. It matches "*.foo" with the name of the
854buffer at the moment the event was triggered.
855
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000856However, buffer-local autocommands will not be executed for a buffer that has
857been wiped out with |:bwipe|. After deleting the buffer with |:bdel| the
858buffer actually still exists (it becomes unlisted), thus the autocommands are
859still executed.
860
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861==============================================================================
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008627. Buffer-local autocommands *autocmd-buflocal* *autocmd-buffer-local*
863 *<buffer=N>* *<buffer=abuf>* *E680*
864
865Buffer-local autocommands are attached to a specific buffer. They are useful
866if the buffer does not have a name and when the name does not match a specific
867pattern. But it also means they must be explicitly added to each buffer.
868
869Instead of a pattern buffer-local autocommands use one of these forms:
870 <buffer> current buffer
871 <buffer=99> buffer number 99
872 <buffer=abuf> using <abuf> (only when executing autocommands)
873 |<abuf>|
874
875Examples: >
876 :au CursorHold <buffer> echo 'hold'
877 :au CursorHold <buffer=33> echo 'hold'
878 :au CursorHold <buffer=abuf> echo 'hold'
879
880All the commands for autocommands also work with buffer-local autocommands,
881simply use the special string instead of the pattern. Examples: >
882 :au! * <buffer> " remove buffer-local autotommands for
883 " current buffer
884 :au! * <buffer=33> " remove buffer-local autotommands for
885 " buffer #33
886 :dobuf :au! CursorHold <buffer> " remove autocmd for given event for all
887 " buffers
888 :au * <buffer> " list buffer-local autocommands for
889 " current buffer
890
891Note that when an autocommand is defined for the current buffer, it is stored
892with the buffer number. Thus it uses the form "<buffer=12>", where 12 is the
893number of the current buffer. You will see this when listing autocommands,
894for example.
895
896To test for presence of buffer-local autocommands use the |exists()| function
897as follows: >
898 :if exists("#CursorHold#<buffer=12>") | ... | endif
899 :if exists("#CursorHold#<buffer>") | ... | endif " for current buffer
900
901When a buffer is wiped out its buffer-local autocommands are also gone, of
902course. Note that when deleting a buffer, e.g., with ":bdel", it is only
903unlisted, the autocommands are still present. In order to see the removal of
904buffer-local autocommands: >
905 :set verbose=6
906
907It is not possible to define buffer-local autocommands for a non-existent
908buffer.
909
910==============================================================================
9118. Groups *autocmd-groups*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913Autocommands can be put together in a group. This is useful for removing or
914executing a group of autocommands. For example, all the autocommands for
915syntax highlighting are put in the "highlight" group, to be able to execute
916":doautoall highlight BufRead" when the GUI starts.
917
918When no specific group is selected, Vim uses the default group. The default
919group does not have a name. You cannot execute the autocommands from the
920default group separately; you can execute them only by executing autocommands
921for all groups.
922
923Normally, when executing autocommands automatically, Vim uses the autocommands
924for all groups. The group only matters when executing autocommands with
925":doautocmd" or ":doautoall", or when defining or deleting autocommands.
926
927The group name can contain any characters except white space. The group name
928"end" is reserved (also in uppercase).
929
930The group name is case sensitive. Note that this is different from the event
931name!
932
933 *:aug* *:augroup*
934:aug[roup] {name} Define the autocmd group name for the
935 following ":autocmd" commands. The name "end"
936 or "END" selects the default group.
937
938 *:augroup-delete* *E367*
939:aug[roup]! {name} Delete the autocmd group {name}. Don't use
940 this if there is still an autocommand using
941 this group! This is not checked.
942
943To enter autocommands for a specific group, use this method:
9441. Select the group with ":augroup {name}".
9452. Delete any old autocommands with ":au!".
9463. Define the autocommands.
9474. Go back to the default group with "augroup END".
948
949Example: >
950 :augroup uncompress
951 : au!
952 : au BufEnter *.gz %!gunzip
953 :augroup END
954
955This prevents having the autocommands defined twice (e.g., after sourcing the
956.vimrc file again).
957
958==============================================================================
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009599. Executing autocommands *autocmd-execute*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961Vim can also execute Autocommands non-automatically. This is useful if you
962have changed autocommands, or when Vim has executed the wrong autocommands
963(e.g., the file pattern match was wrong).
964
965Note that the 'eventignore' option applies here too. Events listed in this
966option will not cause any commands to be executed.
967
968 *:do* *:doau* *:doautocmd* *E217*
969:do[autocmd] [group] {event} [fname]
970 Apply the autocommands matching [fname] (default:
971 current file name) for {event} to the current buffer.
972 You can use this when the current file name does not
973 match the right pattern, after changing settings, or
974 to execute autocommands for a certain event.
975 It's possible to use this inside an autocommand too,
976 so you can base the autocommands for one extension on
977 another extension. Example: >
978 :au Bufenter *.cpp so ~/.vimrc_cpp
979 :au Bufenter *.cpp doau BufEnter x.c
980< Be careful to avoid endless loops. See
981 |autocmd-nested|.
982
983 When the [group] argument is not given, Vim executes
984 the autocommands for all groups. When the [group]
985 argument is included, Vim executes only the matching
986 autocommands for that group. Note: if you use an
987 undefined group name, Vim gives you an error message.
988
Bram Moolenaara5792f52005-11-23 21:25:05 +0000989 After applying the autocommands the modelines are
990 processed, so that their overrule the settings from
991 autocommands, like what happens when editing a file.
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 *:doautoa* *:doautoall*
994:doautoa[ll] [group] {event} [fname]
995 Like ":doautocmd", but apply the autocommands to each
996 loaded buffer. Note that {fname} is used to select
997 the autocommands, not the buffers to which they are
998 applied.
999 Careful: Don't use this for autocommands that delete a
1000 buffer, change to another buffer or change the
1001 contents of a buffer; the result is unpredictable.
1002 This command is intended for autocommands that set
1003 options, change highlighting, and things like that.
1004
1005==============================================================================
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000100610. Using autocommands *autocmd-use*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008For WRITING FILES there are four possible sets of events. Vim uses only one
1009of these sets for a write command:
1010
1011BufWriteCmd BufWritePre BufWritePost writing the whole buffer
1012 FilterWritePre FilterWritePost writing to filter temp file
1013FileAppendCmd FileAppendPre FileAppendPost appending to a file
1014FileWriteCmd FileWritePre FileWritePost any other file write
1015
1016When there is a matching "*Cmd" autocommand, it is assumed it will do the
1017writing. No further writing is done and the other events are not triggered.
1018|Cmd-event|
1019
1020Note that the *WritePost commands should undo any changes to the buffer that
1021were caused by the *WritePre commands; otherwise, writing the file will have
1022the side effect of changing the buffer.
1023
1024Before executing the autocommands, the buffer from which the lines are to be
1025written temporarily becomes the current buffer. Unless the autocommands
1026change the current buffer or delete the previously current buffer, the
1027previously current buffer is made the current buffer again.
1028
1029The *WritePre and *AppendPre autocommands must not delete the buffer from
1030which the lines are to be written.
1031
1032The '[ and '] marks have a special position:
1033- Before the *ReadPre event the '[ mark is set to the line just above where
1034 the new lines will be inserted.
1035- Before the *ReadPost event the '[ mark is set to the first line that was
1036 just read, the '] mark to the last line.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001037- Before executing the *WriteCmd, *WritePre and *AppendPre autocommands the '[
1038 mark is set to the first line that will be written, the '] mark to the last
1039 line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040Careful: '[ and '] change when using commands that change the buffer.
1041
1042In commands which expect a file name, you can use "<afile>" for the file name
1043that is being read |:<afile>| (you can also use "%" for the current file
1044name). "<abuf>" can be used for the buffer number of the currently effective
1045buffer. This also works for buffers that doesn't have a name. But it doesn't
1046work for files without a buffer (e.g., with ":r file").
1047
1048 *gzip-example*
1049Examples for reading and writing compressed files: >
1050 :augroup gzip
1051 : autocmd!
1052 : autocmd BufReadPre,FileReadPre *.gz set bin
1053 : autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip
1054 : autocmd BufReadPost,FileReadPost *.gz set nobin
1055 : autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " . expand("%:r")
1056 : autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r
1057 : autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r
1058
1059 : autocmd FileAppendPre *.gz !gunzip <afile>
1060 : autocmd FileAppendPre *.gz !mv <afile>:r <afile>
1061 : autocmd FileAppendPost *.gz !mv <afile> <afile>:r
1062 : autocmd FileAppendPost *.gz !gzip <afile>:r
1063 :augroup END
1064
1065The "gzip" group is used to be able to delete any existing autocommands with
1066":autocmd!", for when the file is sourced twice.
1067
1068("<afile>:r" is the file name without the extension, see |:_%:|)
1069
1070The commands executed for the BufNewFile, BufRead/BufReadPost, BufWritePost,
1071FileAppendPost and VimLeave events do not set or reset the changed flag of the
1072buffer. When you decompress the buffer with the BufReadPost autocommands, you
1073can still exit with ":q". When you use ":undo" in BufWritePost to undo the
1074changes made by BufWritePre commands, you can still do ":q" (this also makes
1075"ZZ" work). If you do want the buffer to be marked as modified, set the
1076'modified' option.
1077
1078To execute Normal mode commands from an autocommand, use the ":normal"
1079command. Use with care! If the Normal mode command is not finished, the user
1080needs to type characters (e.g., after ":normal m" you need to type a mark
1081name).
1082
1083If you want the buffer to be unmodified after changing it, reset the
1084'modified' option. This makes it possible to exit the buffer with ":q"
1085instead of ":q!".
1086
1087 *autocmd-nested* *E218*
1088By default, autocommands do not nest. If you use ":e" or ":w" in an
1089autocommand, Vim does not execute the BufRead and BufWrite autocommands for
1090those commands. If you do want this, use the "nested" flag for those commands
1091in which you want nesting. For example: >
1092 :autocmd FileChangedShell *.c nested e!
1093The nesting is limited to 10 levels to get out of recursive loops.
1094
1095It's possible to use the ":au" command in an autocommand. This can be a
1096self-modifying command! This can be useful for an autocommand that should
1097execute only once.
1098
Bram Moolenaarb3480382005-12-11 21:33:32 +00001099If you want to skip autocommands for one command, use the |:noautocmd| command
1100modifier or the 'eventignore' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102Note: When reading a file (with ":read file" or with a filter command) and the
1103last line in the file does not have an <EOL>, Vim remembers this. At the next
1104write (with ":write file" or with a filter command), if the same line is
1105written again as the last line in a file AND 'binary' is set, Vim does not
1106supply an <EOL>. This makes a filter command on the just read lines write the
1107same file as was read, and makes a write command on just filtered lines write
1108the same file as was read from the filter. For example, another way to write
1109a compressed file: >
1110
1111 :autocmd FileWritePre *.gz set bin|'[,']!gzip
1112 :autocmd FileWritePost *.gz undo|set nobin
1113<
1114 *autocommand-pattern*
1115You can specify multiple patterns, separated by commas. Here are some
1116examples: >
1117
1118 :autocmd BufRead * set tw=79 nocin ic infercase fo=2croq
1119 :autocmd BufRead .letter set tw=72 fo=2tcrq
1120 :autocmd BufEnter .letter set dict=/usr/lib/dict/words
1121 :autocmd BufLeave .letter set dict=
1122 :autocmd BufRead,BufNewFile *.c,*.h set tw=0 cin noic
1123 :autocmd BufEnter *.c,*.h abbr FOR for (i = 0; i < 3; ++i)<CR>{<CR>}<Esc>O
1124 :autocmd BufLeave *.c,*.h unabbr FOR
1125
1126For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.): >
1127
1128 :autocmd BufEnter ?akefile* set include=^s\=include
1129 :autocmd BufLeave ?akefile* set include&
1130
1131To always start editing C files at the first function: >
1132
1133 :autocmd BufRead *.c,*.h 1;/^{
1134
1135Without the "1;" above, the search would start from wherever the file was
1136entered, rather than from the start of the file.
1137
1138 *skeleton* *template*
1139To read a skeleton (template) file when opening a new file: >
1140
1141 :autocmd BufNewFile *.c 0r ~/vim/skeleton.c
1142 :autocmd BufNewFile *.h 0r ~/vim/skeleton.h
1143 :autocmd BufNewFile *.java 0r ~/vim/skeleton.java
1144
1145To insert the current date and time in a *.html file when writing it: >
1146
1147 :autocmd BufWritePre,FileWritePre *.html ks|call LastMod()|'s
1148 :fun LastMod()
1149 : if line("$") > 20
1150 : let l = 20
1151 : else
1152 : let l = line("$")
1153 : endif
1154 : exe "1," . l . "g/Last modified: /s/Last modified: .*/Last modified: " .
1155 : \ strftime("%Y %b %d")
1156 :endfun
1157
1158You need to have a line "Last modified: <date time>" in the first 20 lines
1159of the file for this to work. Vim replaces <date time> (and anything in the
1160same line after it) with the current date and time. Explanation:
1161 ks mark current position with mark 's'
1162 call LastMod() call the LastMod() function to do the work
1163 's return the cursor to the old position
1164The LastMod() function checks if the file is shorter than 20 lines, and then
1165uses the ":g" command to find lines that contain "Last modified: ". For those
1166lines the ":s" command is executed to replace the existing date with the
1167current one. The ":execute" command is used to be able to use an expression
1168for the ":g" and ":s" commands. The date is obtained with the strftime()
1169function. You can change its argument to get another date string.
1170
1171When entering :autocmd on the command-line, completion of events and command
1172names may be done (with <Tab>, CTRL-D, etc.) where appropriate.
1173
1174Vim executes all matching autocommands in the order that you specify them.
1175It is recommended that your first autocommand be used for all files by using
1176"*" as the file pattern. This means that you can define defaults you like
1177here for any settings, and if there is another matching autocommand it will
1178override these. But if there is no other matching autocommand, then at least
1179your default settings are recovered (if entering this file from another for
1180which autocommands did match). Note that "*" will also match files starting
1181with ".", unlike Unix shells.
1182
1183 *autocmd-searchpat*
1184Autocommands do not change the current search patterns. Vim saves the current
1185search patterns before executing autocommands then restores them after the
1186autocommands finish. This means that autocommands do not affect the strings
1187highlighted with the 'hlsearch' option. Within autocommands, you can still
1188use search patterns normally, e.g., with the "n" command.
1189If you want an autocommand to set the search pattern, such that it is used
1190after the autocommand finishes, use the ":let @/ =" command.
1191The search-highlighting cannot be switched off with ":nohlsearch" in an
1192autocommand. Use the 'h' flag in the 'viminfo' option to disable search-
1193highlighting when starting Vim.
1194
1195 *Cmd-event*
1196When using one of the "*Cmd" events, the matching autocommands are expected to
1197do the file reading or writing. This can be used when working with a special
1198kind of file, for example on a remote system.
1199CAREFUL: If you use these events in a wrong way, it may have the effect of
1200making it impossible to read or write the matching files! Make sure you test
1201your autocommands properly. Best is to use a pattern that will never match a
1202normal file name, for example "ftp://*".
1203
1204When defining a BufReadCmd it will be difficult for Vim to recover a crashed
1205editing session. When recovering from the original file, Vim reads only those
1206parts of a file that are not found in the swap file. Since that is not
1207possible with a BufReadCmd, use the |:preserve| command to make sure the
1208original file isn't needed for recovery. You might want to do this only when
1209you expect the file to be modified.
1210
1211The |v:cmdarg| variable holds the "++enc=" and "++ff=" argument that are
1212effective. These should be used for the command that reads/writes the file.
1213The |v:cmdbang| variable is one when "!" was used, zero otherwise.
1214
1215See the $VIMRUNTIME/plugin/netrw.vim for examples.
1216
Bram Moolenaarb3480382005-12-11 21:33:32 +00001217==============================================================================
121811. Disabling autocommands *autocmd-disable*
1219
1220To disable autocommands for some time use the 'eventignore' option. Note that
1221this may cause unexpected behavior, make sure you restore 'eventignore'
1222afterwards, using a |:try| block with |:finally|.
1223
1224 *:noautocmd* *:noa*
1225To disable autocommands for just one command use the ":noautocmd" command
1226modifier. This will set 'eventignore' to "all" for the duration of the
1227following command. Example: >
1228
1229 :noautocmd w fname.gz
1230
1231This will write the file without triggering the autocommands defined by the
1232gzip plugin.
1233
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 vim:tw=78:ts=8:ft=help:norl: