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