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