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