home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim53os2.zip / vim-5.3 / doc / autocmd.txt next >
Text File  |  1998-08-30  |  27KB  |  641 lines

  1. *autocmd.txt*   For Vim version 5.3.  Last modification: 1998 Aug 19
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Automatic commands                    *autocommand*
  8.  
  9. 1.  Introduction        |autocmd-intro|
  10. 2.  Defining autocommands    |autocmd-define|
  11. 3.  Removing autocommands    |autocmd-remove|
  12. 4.  Listing autocommands    |autocmd-list|
  13. 5.  Events            |autocmd-events|
  14. 6.  Patterns            |autocmd-patterns|
  15. 7.  Filetypes            |autocmd-filetypes|
  16. 8.  Groups            |autocmd-groups|
  17. 9.  Executing autocommands    |autocmd-execute|
  18. 10. Using autocommands        |autocmd-use|
  19.  
  20. {Vi does not have any of these commands}
  21.  
  22. ==============================================================================
  23. 1. Introduction                        *autocmd-intro*
  24.  
  25. You can specify commands to be executed automatically for when reading or
  26. writing a file, when entering or leaving a buffer or window, and when exiting
  27. Vim.  For example, you can create an autocommand to set the 'cindent' option
  28. for files matching *.c.  You can also use autocommands to implement advanced
  29. features, such as editing compressed files (see |gzip-example|).  The usual
  30. place to put autocommands is in your .vimrc or .exrc file.
  31.  
  32. WARNING: Using autocommands is very powerful, and may lead to unexpected side
  33. effects.  Be careful not to destroy your text.
  34. - It's a good idea to do some testing on an expendable copy of a file first.
  35.   For example: If you use autocommands to decompress a file when starting to
  36.   edit it, make sure that the autocommands for compressing when writing work
  37.   correctly.
  38. - Be prepared for an error halfway through (e.g., disk full).  Vim will mostly
  39.   be able to undo the changes to the buffer, but you may have to clean up the
  40.   changes to other files by hand (e.g., compress a file that has been
  41.   decompressed).
  42. - If the BufRead* events allow you to edit a compressed file, the FileRead*
  43.   events should do the same (this makes recovery possible in some rare cases).
  44.   It's a good idea to use the same autocommands for the File* and Buf* events
  45.   when possible.
  46.  
  47. The |+autocmd| feature is only included if it has not been disabled at compile
  48. time.
  49.  
  50. ==============================================================================
  51. 2. Defining autocommands                *autocmd-define*
  52.  
  53. Note: The ":autocmd" command cannot be followed by another command, since any
  54. '|' is considered part of the command.
  55.  
  56.                             *:au* *:autocmd*
  57. :au[tocmd] [group] {event} {pat} [nested] {cmd}
  58.             Add {cmd} to the list of commands that Vim will
  59.             execute automatically on {event} for a file matching
  60.             {pat}.  Vim always adds the {cmd} after existing
  61.             autocommands, so that the autocommands execute in the
  62.             order in which they were given.  See |autocmd-nest|
  63.             for [nested].
  64.  
  65. Note that special characters (e.g., "%", "<cword>") in the ":autocmd"
  66. arguments are not expanded when the autocommand is defined.  These will be
  67. expanded when the Event is recognized, and the {cmd} is executed.  The only
  68. exception is that "<sfile>" is expanded when the autocmd is defined.  Example:
  69.  
  70. >    :au BufEnter *.html so <sfile>:h/html.vim
  71.  
  72. Here Vim expands <sfile> to the name of the file containing this line.
  73.  
  74. When your .vimrc file is sourced twice, the autocommands will appear twice.
  75. To avoid this, put this command in your .vimrc file, before defining
  76. autocommands:
  77.  
  78. >    :autocmd!    " Remove ALL autocommands.
  79.  
  80. If you don't want to remove all autocommands, you can instead use a variable
  81. to ensure that Vim includes the autocommands only once:
  82.  
  83. >    :if !exists("autocommands_loaded")
  84. >    :  let autocommands_loaded = 1
  85. >    :  au ...
  86. >    :endif
  87.  
  88. When the [group] argument is not given, Vim uses the current group (as defined
  89. with ":augroup"); otherwise, Vim uses the group defined with [group].  Note
  90. that [group] must have been defined before.  You cannot define a new group
  91. with ":au group ..."; use ":augroup" for that.
  92.  
  93. While testing autocommands, you might find the 'verbose' option to be useful:
  94. >    :set verbose=9
  95. This setting makes Vim echo the autocommands as it executes them.
  96.  
  97. ==============================================================================
  98. 3. Removing autocommands                *autocmd-remove*
  99.  
  100. :au[tocmd]! [group] {event} {pat} [nested] {cmd}
  101.             Remove all autocommands associated with {event} and
  102.             {pat}, and add the command {cmd}.  See |autocmd-nest|
  103.             for [nested].
  104.  
  105. :au[tocmd]! [group] {event} {pat}
  106.             Remove all autocommands associated with {event} and
  107.             {pat}.
  108.  
  109. :au[tocmd]! [group] * {pat}
  110.             Remove all autocommands associated with {pat} for all
  111.             events.
  112.  
  113. :au[tocmd]! [group] {event}
  114.             Remove ALL autocommands for {event}.
  115.  
  116. :au[tocmd]! [group]    Remove ALL autocommands.
  117.  
  118. When the [group] argument is not given, Vim uses the current group (as defined
  119. with ":augroup"); otherwise, Vim uses the group defined with [group].
  120.  
  121. ==============================================================================
  122. 4. Listing autocommands                    *autocmd-list*
  123.  
  124. :au[tocmd] [group] {event} {pat}
  125.             Show the autocommands associated with {event} and
  126.             {pat}.
  127.  
  128. :au[tocmd] [group] * {pat}
  129.             Show the autocommands associated with {pat} for all
  130.             events.
  131.  
  132. :au[tocmd] [group] {event}
  133.             Show all autocommands for {event}.
  134.  
  135. :au[tocmd] [group]    Show all autocommands.
  136.  
  137. If you provide the [group] argument, Vim lists only the autocommands for
  138. [group]; otherwise, Vim lists the autocommands for ALL groups.  Note that this
  139. argument behavior differs from that for defining and removing autocommands.
  140.  
  141. ==============================================================================
  142. 5. Events                        *autocmd-events*
  143.  
  144.                     *autocommand-events* *{event}*
  145. Vim recognizes the following events.  Vim ignores the case of event names
  146. (e.g., you can use "BUFread" or "bufread" instead of "BufRead").
  147.  
  148.                             *BufNewFile*
  149. BufNewFile            When starting to edit a file that doesn't
  150.                 exist.  Can be used to read in a skeleton
  151.                 file.
  152.                             *BufReadPre*
  153. BufReadPre            When starting to edit a new buffer, before
  154.                 reading the file into the buffer.  Not used
  155.                 if the file doesn't exist.
  156.                         *BufRead* *BufReadPost*
  157. BufRead or BufReadPost        When starting to edit a new buffer, after
  158.                 reading the file into the buffer, before
  159.                 executing the modelines.  This does NOT work
  160.                 for ":r file".  Not used when the file doesn't
  161.                 exist.  Also used after succesfully recovering
  162.                 a file.
  163.                             *BufFilePre*
  164. BufFilePre            Before changing the name of the current buffer
  165.                 with the ":file" command.
  166.                             *BufFilePost*
  167. BufFilePost            After changing the name of the current buffer
  168.                 with the ":file" command.
  169.                             *FileReadPre*
  170. FileReadPre            Before reading a file with a ":read" command.
  171.                             *FileReadPost*
  172. FileReadPost            After reading a file with a ":read" command.
  173.                 Note that Vim sets the '[ and '] marks to the
  174.                 first and last line of the read.  This can be
  175.                 used to operate on the lines just read.
  176.                             *FilterReadPre*
  177. FilterReadPre            Before reading a file from a filter command.
  178.                 Vim checks the pattern against the the name of
  179.                 the current buffer, not the name of the
  180.                 temporary file that is the output of the
  181.                 filter command.
  182.                             *FilterReadPost*
  183. FilterReadPost            After reading a file from a filter command.
  184.                 Vim checks the pattern against the the name of
  185.                 the current buffer as with FilterReadPre.
  186.                             *StdinReadPre*
  187. StdinReadPre            Before reading from stdin into the buffer.
  188.                 Only used when the "-" argument was used when
  189.                 Vim was started |--|.
  190.                             *StdinReadPost*
  191. StdinReadPost            After reading from the stdin into the buffer,
  192.                 before executing the modelines.  Only used
  193.                 when the "-" argument was used when Vim was
  194.                 started |--|.
  195.                         *BufWrite* *BufWritePre*
  196. BufWrite or BufWritePre        Before writing the whole buffer to a file.
  197.                             *BufWritePost*
  198. BufWritePost            After writing the whole buffer to a file
  199.                 (should undo the commands for BufWritePre).
  200.                             *FileWritePre*
  201. FileWritePre            Before writing to a file, when not writing the
  202.                 whole buffer.
  203.                             *FileWritePost*
  204. FileWritePost            After writing to a file, when not writing the
  205.                 whole buffer.
  206.                             *FileAppendPre*
  207. FileAppendPre            Before appending to a file.
  208.                             *FileAppendPost*
  209. FileAppendPost            After appending to a file.
  210.                             *FilterWritePre*
  211. FilterWritePre            Before writing a file for a filter command.
  212.                 Vim checks the pattern against the the name of
  213.                 the current buffer, not the name of the
  214.                 temporary file that is the output of the
  215.                 filter command.
  216.                             *FilterWritePost*
  217. FilterWritePost            After writing a file for a filter command.
  218.                 Vim checks the pattern against the the name of
  219.                 the current buffer as with FilterWritePre.
  220.                             *FileChangedShell*
  221. FileChangedShell        After Vim runs a shell command and notices
  222.                 that the modification time of a file has
  223.                 changed since editing started.  This
  224.                 autocommand is triggered for each changed
  225.                 file.  Run in place of the 'has been changed'
  226.                 message.  See |timestamp|.  Useful for
  227.                 reloading related buffers which are affected
  228.                 by a single command.  NOTE: when this
  229.                 autocommand is executed, the current buffer
  230.                 "%" may be different from the buffer that was
  231.                 changed "<afile>".
  232.                             *BufEnter*
  233. BufEnter            After entering a buffer.  Useful for setting
  234.                 options for a file type.  Also executed when
  235.                 starting to edit a buffer, after the
  236.                 BufReadPost autocommands.
  237.                             *BufLeave*
  238. BufLeave            Before leaving to another buffer.  Also when
  239.                 leaving or closing the current window and the
  240.                 new current window is not for the same buffer.
  241.                 Not used for ":qa" or ":q" when exiting Vim.
  242.                             *BufUnload*
  243. BufUnload            Before unloading a buffer.  This is when the
  244.                 text in the buffer is going to be freed.  This
  245.                 may be after a BufWritePost and before a
  246.                 BufDelete.  NOTE: when this autocommand is
  247.                 executed, the current buffer "%" may be
  248.                 different from the buffer being unloaded
  249.                 "<afile>".
  250.  
  251.                             *BufDelete*
  252. BufDelete            Before deleting a buffer from the buffer list.
  253.                 The BufUnload may be called first (if the
  254.                 buffer was loaded).  NOTE: when this
  255.                 autocommand is executed, the current buffer
  256.                 "%" may be different from the buffer being
  257.                 deleted "<afile>".
  258.                             *WinEnter*
  259. WinEnter            After entering another window.  Not done for
  260.                 the first window, when Vim has just started.
  261.                 Useful for setting the window height.
  262.                 If the window is for another buffer, Vim
  263.                 executes the BufEnter autocommands after the
  264.                 WinEnter autocommands.
  265.                             *TermChanged*
  266. TermChanged            After the value of 'term' has changed.  Useful
  267.                 for re-loading the syntax file to update the
  268.                 colors, fonts and other terminal-dependent
  269.                 settings.  Executed for all loaded buffers.
  270.                             *WinLeave*
  271. WinLeave            Before leaving a window.  If the window to be
  272.                 entered next is for a different buffer, Vim
  273.                 executes the BufLeave autocommands before the
  274.                 WinLeave autocommands (but not for ":new").
  275.                 Not used for ":qa" or ":q" when exiting Vim.
  276.                             *VimEnter*
  277. VimEnter            After doing all the startup stuff, including
  278.                 loading .vimrc files, executing the "-c cmd"
  279.                 arguments, creating all windows and loading
  280.                 the buffers in them.
  281.                             *VimLeavePre*
  282. VimLeavePre            Before exiting Vim, just before writing the
  283.                 .viminfo file.  This is executed only once,
  284.                 if there is a match with the name of what
  285.                 happens to be the current buffer when exiting.
  286.                 Mostly useful with a "*" pattern.
  287. >    :autocmd VimLeavePre * call CleanupStuff()
  288.                             *VimLeave*
  289. VimLeave            Before exiting Vim, just after writing the
  290.                 .viminfo file.  Executed only once, like
  291.                 VimLeavePre.
  292.                             *User*
  293. User                Never executed automatically.  To be used for
  294.                 autocommands that are only executed with
  295.                 ":doautocmd".
  296.  
  297. FileEncoding            Fires off when you change the file encoding
  298.                 with ':set fileencoding'.  Allows you to set
  299.                 up fonts or other language sensitive settings.
  300.  
  301. For READING FILES there are three possible pairs of events.  Vim uses only one
  302. pair at a time:
  303. BufNewFile            starting to edit a non-existent file
  304. BufReadPre    BufReadPost    starting to edit an existing file
  305. FilterReadPre    FilterReadPost    read the temp file with filter output
  306. FileReadPre    FileReadPost    any other file read
  307.  
  308. Note that the autocommands for the *ReadPre events and all the Filter events
  309. are not allowed to change the current buffer (you will get an error message if
  310. this happens).  This is to prevent the file to be read into the wrong buffer.
  311.  
  312. Note that the 'modified' flag is reset AFTER executing the BufReadPost
  313. and BufNewFile autocommands.  But when the 'modified' option was set by the
  314. autocommands, this doesn't happen.
  315.  
  316. You can use the 'eventignore' option to ignore a number of events or all
  317. events.
  318.  
  319. ==============================================================================
  320. 6. Patterns                        *autocmd-patterns*
  321.  
  322. The file pattern {pat} is tested for a match against the file name in one of
  323. two ways:
  324. 1. When there is no '/' in the pattern, Vim checks for a match against only
  325.    the tail part of the file name (without its leading directory path).
  326. 2. When there is a '/' in the pattern,  Vim checks for a match against the
  327.    both short file name (as you typed it) and the full file name (after
  328.    expanding it to a full path and resolving symbolic links).
  329.  
  330. Examples:
  331. >    :autocmd BufRead *.txt        set et
  332. Set the 'et' option for all text files.
  333.  
  334. >    :autocmd BufRead /vim/src/*.c    set cindent
  335. Set the 'cindent' option for C files in the /vim/src directory.
  336.  
  337. >    :autocmd BufRead /tmp/*.c    set ts=5
  338. If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", and
  339. you start editing "/tmp/test.c", this autocommand will match.
  340.  
  341. Note:  To match part of a path, but not from the root directory, use a '*' as
  342. the first character.  Example:
  343. >    :autocmd BufRead */doc/*.txt    set tw=78
  344. This autocommand will for example be executed for "/tmp/doc/xx.txt" and
  345. "/usr/home/piet/doc/yy.txt".  The number of directories does not matter here.
  346.  
  347.  
  348. Environment variables can be used in a pattern:
  349. >    :autocmd BufRead $VIM/doc/*.txt  set expandtab
  350. And ~ can be used for the home directory (if $HOME is defined):
  351. >    :autocmd BufWritePost ~/.vimrc   so ~/.vimrc
  352. >    :autocmd BufRead ~archive/*      set readonly
  353. The environment variable is expanded when the autocommand is defined, not when
  354. the autocommand is executed.  This is different from the command!
  355.  
  356.  
  357. Note that for all systems the '/' character is used for path separator (even
  358. MS-DOS and OS/2).  This was done because the backslash is difficult to use
  359. in a pattern and to make the autocommands portable accross different systems.
  360.  
  361. ==============================================================================
  362. 7. Filetypes                        *autocmd-filetypes*
  363.  
  364. On systems which support filetypes you can specify that a command should only
  365. be executed if the file is of a certain type.
  366.  
  367. The actual type checking depends on which platform you are running Vim
  368. on; see your system's documentation for details.
  369.  
  370. To use filetype checking in an autocommand you should put a list of types to
  371. match in angle brackets in place of a pattern, like this:
  372.  
  373. > :au BufRead *.html,<&faf;HTML>  so $VIM/syntax/html.vim
  374.  
  375. This will match:
  376.  
  377. - Any file whose name ends in `.html'
  378. - Any file whose type is `&faf' or 'HTML', where the meaning of these types
  379.   depends on which version of Vim you are using.
  380.   Unknown types are considered NOT to match.
  381.  
  382. You can also specify a type and a pattern at the same time (in which case they
  383. must both match):
  384.  
  385. > :au BufRead <&fff>*diff*
  386.  
  387. This will match files of type `&fff' whose names contain `diff'.
  388.  
  389. Note that filetype checking is skipped if Vim is compiled without the
  390. |+filetype| feature.
  391.  
  392. ==============================================================================
  393. 8. Groups                        *autocmd-groups*
  394.  
  395. Autocommands can be put together in a group.  This is useful for removing or
  396. executing a group of autocommands.  For example, all the autocommands for
  397. syntax highlighting are put in the "highlight" group, to be able to execute
  398. ":doautoall highlight BufRead" when the GUI starts.
  399.  
  400. When no specific group is selected, Vim uses the default group.  The default
  401. group does not have a name.  You cannot execute the autocommands from the
  402. default group separately; you can execute them only by executing autocommands
  403. for all groups.
  404.  
  405. Normally, when executing autocommands automatically, Vim uses the autocommands
  406. for all groups.  The group only matters when executing autocommands with
  407. ":doautocmd" or ":doautoall", or when defining or deleting autocommands.
  408.  
  409. The group name can contain any characters except white space.  The group name
  410. "end" is reserved (also in uppercase).
  411.  
  412.                             *:aug* *:augroup*
  413. :aug[roup] {name}        Define the autocmd group name for the
  414.                 following ":autocmd" commands.  The name "end"
  415.                 or "END" selects the default group.
  416.  
  417. To enter autocommands for a specific group, use this method:
  418. 1. Select the group with ":augroup {name}".
  419. 2. Delete any old autocommands with ":au!".
  420. 3. Define the autocommands.
  421. 4. Go back to the default group with "augroup END".
  422.  
  423. Example:
  424. >    :augroup uncompress
  425. >    :  au!
  426. >    :  au BufEnter *.gz    %!gunzip
  427. >    :augroup END
  428.  
  429. This prevents having the autocommands defined twice (e.g., after sourcing the
  430. .vimrc file again).
  431.  
  432. ==============================================================================
  433. 9. Executing autocommands                *autocmd-execute*
  434.  
  435. Vim can also execute Autocommands non-automatically.  This is useful if you
  436. have changed autocommands, or when Vim has executed the wrong autocommands
  437. (e.g., the file pattern match was wrong).
  438.  
  439. Note that the 'eventignore' option applies here too.  Events listed in this
  440. option will not cause any commands to be executed.
  441.  
  442.                             *:do* *:doautocmd*
  443. :do[autocmd] [group] {event} [fname]
  444.             Apply the autocommands matching [fname] (default:
  445.             current file name) for {event} to the current buffer.
  446.             You can use this when the current file name does not
  447.             match the right pattern, after changing settings, or
  448.             to execute autocommands for a certain event.
  449.             It's possible to use this inside an autocommand too,
  450.             so you can base the autocommands for one extension on
  451.             another extension.  Example:
  452. >                :au Bufenter *.cpp so ~/.vimrc_cpp
  453. >                :au Bufenter *.cpp doau BufEnter x.c
  454.             Be careful to avoid endless loops.  See |autocmd-nest|.
  455.  
  456.             When the [group] argument is not given, Vim executes
  457.             the autocommands for all groups.  When the [group]
  458.             argument is included, Vim executes only the matching
  459.             autocommands for that group.  Note: if you use an
  460.             undefined group name, Vim gives you an error message.
  461.  
  462.                         *:doautoa* *:doautoall*
  463. :doautoa[ll] [group] {event} [fname]
  464.             Like ":doautocmd", but apply the autocommands to each
  465.             loaded buffer.  Careful: Don't use this for
  466.             autocommands that delete a buffer, change to another
  467.             buffer or change the contents of a buffer; the result
  468.             is unpredictable.  this command is intended for
  469.             autocommands that set options, change highlighting,
  470.             and things like that.
  471.  
  472. ==============================================================================
  473. 10. Using autocommands                    *autocmd-use*
  474.  
  475. For WRITING FILES there are four possible pairs of events.  Vim uses only one
  476. pair at a time:
  477. BufWritePre    BufWritePost    writing the whole buffer
  478. FilterWritePre    FilterWritePost    writing to the temp file with filter input
  479. FileAppendPre    FileAppendPost    appending to a file
  480. FileWritePre    FileWritePost    any other file write
  481.  
  482. Note that the *WritePost commands should undo any changes to the buffer that
  483. were caused by the *WritePre commands; otherwise, writing the file will have
  484. the side effect of changing the buffer.
  485.  
  486. Before executing the autocommands, the buffer from which the lines are to be
  487. written temporarily becomes the current buffer.  Unless the autocommands
  488. change the current buffer or delete the previously current buffer, the
  489. previously current buffer is made the current buffer again.
  490.  
  491. The *WritePre and *AppendPre autocommands must not delete the buffer from
  492. which the lines are to be written.
  493.  
  494. The '[ and '] marks have a special position:
  495. - Before the *ReadPre event the '[ mark is set to the line just above where
  496.   the new lines will be inserted.
  497. - Before the *ReadPost event the '[ mark is set to the first line that was
  498.   just read, the '] mark to the last line.
  499. - Before executing the *WritePre and *AppendPre autocommands the '[ mark is
  500.   set to the first line that will be written, the '] mark to the last line.
  501. Careful: '[ and '] change when using commands that change the buffer.
  502.  
  503. In commands which expect a file name, you can use "<afile>" for the file name
  504. that is being read |:<afile>| (you can also use "%" for the current file
  505. name).  "<abuf>" can be used for the buffer number of the currently effective
  506. buffer.  This also works for buffers that doesn't have a name.  But it doesn't
  507. work for files without a buffer (e.g., with ":r file").
  508.  
  509.                             *gzip-example*
  510. Examples for reading and writing compressed files:
  511. > :augroup gzip
  512. > :  autocmd!
  513. > :  autocmd BufReadPre,FileReadPre    *.gz set bin
  514. > :  autocmd BufReadPost,FileReadPost    *.gz '[,']!gunzip
  515. > :  autocmd BufReadPost,FileReadPost    *.gz set nobin
  516. > :  autocmd BufReadPost,FileReadPost    *.gz execute ":doautocmd BufReadPost " . expand("%:r")
  517. > :  autocmd BufWritePost,FileWritePost    *.gz !mv <afile> <afile>:r
  518. > :  autocmd BufWritePost,FileWritePost    *.gz !gzip <afile>:r
  519. >
  520. > :  autocmd FileAppendPre        *.gz !gunzip <afile>
  521. > :  autocmd FileAppendPre        *.gz !mv <afile>:r <afile>
  522. > :  autocmd FileAppendPost        *.gz !mv <afile> <afile>:r
  523. > :  autocmd FileAppendPost        *.gz !gzip <afile>:r
  524. > :augroup END
  525.  
  526.  
  527. The "gzip" group is used to be able to delete any existing autocommands with
  528. ":autocmd!", for when the file is sourced twice.
  529.  
  530. ("<afile>:r" is the file name without the extension, see |:_%:|)
  531.  
  532. The commands executed for the BufNewFile, BufRead/BufReadPost, BufWritePost,
  533. FileAppendPost and VimLeave events do not set or reset the changed flag of the
  534. buffer.  When you decompress the buffer with the BufReadPost autocommands, you
  535. can still exit with ":q".  When you use ":undo" in BufWritePost to undo the
  536. changes made by BufWritePre commands, you can still do ":q" (this also makes
  537. "ZZ" work).  If you do want the buffer to be marked as modified, set the
  538. 'modified' option.
  539.  
  540. To execute Normal mode commands from an autocommand, use the ":normal"
  541. command.  Use with care!  If the Normal mode command is not finished, the user
  542. needs to type characters (e.g., after ":normal m" you need to type a mark
  543. name).
  544.  
  545. If you want the buffer to be unmodified after changing it, reset the
  546. 'modified' option.  This makes it possible to exit the buffer with ":q"
  547. instead of ":q!".
  548.  
  549.                             *autocmd-nest*
  550. By default, autocommands do not nest.  If you use ":e" or ":w" in an
  551. autocommand, Vim does not execute the BufRead and BufWrite autocommands for
  552. those commands.  If you do want this, use the "nested" flag for those commands
  553. in which you want nesting.  For example:
  554. >    :autocmd FileChangedShell *.c nested e!
  555. The nesting is limited to 10 levels to get out of recursive loops.
  556.  
  557. It's possible to use the ":au" command in an autocommand.  This can be a
  558. self-modifying command!  This can be useful for an autocommand that should
  559. execute only once.
  560.  
  561. There is currently no way to disable the autocommands.  If you want to write a
  562. file without executing the autocommands for that type of file, write it under
  563. another name and rename it with a shell command.
  564.  
  565. Note: When reading a file (with ":read file" or with a filter command) and the
  566. last line in the file does not have an <EOL>, Vim remembers this.  At the next
  567. write (with ":write file" or with a filter command), if the same line is
  568. written again as the last line in a file AND 'binary' is set, Vim does not
  569. supply an <EOL>.  This makes a filter command on the just read lines write the
  570. same file as was read, and makes a write command on just filtered lines write
  571. the same file as was read from the filter.  For example, another way to write
  572. a compressed file:
  573.  
  574. > :autocmd FileWritePre *.gz   set bin|'[,']!gzip
  575. > :autocmd FileWritePost *.gz  undo|set nobin
  576.  
  577.                             *autocommand-pattern*
  578. You can specify multiple patterns, separated by commas.  Here are some
  579. examples:
  580.  
  581. > :autocmd BufRead   *        set tw=79 nocin ic infercase fo=2croq
  582. > :autocmd BufRead   .letter    set tw=72 fo=2tcrq
  583. > :autocmd BufEnter  .letter    set dict=/usr/lib/dict/words
  584. > :autocmd BufLeave  .letter    set dict=
  585. > :autocmd BufRead,BufNewFile   *.c,*.h    set tw=0 cin noic
  586. > :autocmd BufEnter  *.c,*.h    abbr FOR for(i = 0; i < 3; i++)^M{^M}^[O
  587. > :autocmd BufLeave  *.c,*.h    unabbr FOR
  588.  
  589. For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.):
  590.  
  591. > :autocmd BufEnter  ?akefile*    set include=^s\=include
  592. > :autocmd BufLeave  ?akefile*    set include&
  593.  
  594. To always start editing C files at the first function:
  595.  
  596. > :autocmd BufRead   *.c,*.h    1;/^{
  597.  
  598. Without the "1;" above, the search would start from wherever the file was
  599. entered, rather than from the start of the file.
  600.  
  601. To read a skeleton file for new C files:
  602.  
  603. > :autocmd BufNewFile  *.c    0r ~/.skeleton.c
  604. > :autocmd BufNewFile  *.h    0r ~/.skeleton.h
  605.  
  606. To insert the current date and time in a *.html file when writing it:
  607.  
  608. > :autocmd BufWritePre,FileWritePre *.html ks|1,20g/Last modification: /normal f:lD:read !date^MkJ's
  609.  
  610. (to insert the ^M type CTRL-V CTRL-M)
  611. You need to have a line "Last modification: <date time>" in the first 20 lines
  612. of the file for this to work.  Vim replaces <date time> (and anything in the
  613. same line after it) with the current date and time.  Explanation:
  614.     ks        mark current position with mark 's'
  615.     1,20g/pattern/    find lines that contain the pattern
  616.     normal f:    find the ':'
  617.     lD        delete the old date and time
  618.     !date^M        read the current date and time into the next line
  619.     kJ        Join the date and time with the previous line
  620.     's        return the cursor to the old position
  621.  
  622. When entering :autocmd on the command-line, completion of events and command
  623. names may be done (with <Tab>, CTRL-D, etc.) where appropriate.
  624.  
  625. Vim executes all matching autocommands in the order that you specify them.
  626. It is recommended that your first autocommand be used for all files by using
  627. "*" as the file pattern.  This means that you can define defaults you like
  628. here for any settings, and if there is another matching autocommand it will
  629. override these.  But if there is no other matching autocommand, then at least
  630. your default settings are recovered (if entering this file from another for
  631. which autocommands did match).  Note that "*" will also match files starting
  632. with ".", unlike Unix shells.
  633.  
  634. Autocommands do not change the current search patterns.  Vim saves the current
  635. search patterns before executing autocommands then restores them after the
  636. autocommands finish.  This means that autocommands do not affect the strings
  637. highlighted with the 'hlsearch' option.  Within autocommands, you can still
  638. use search patterns normally, e.g., with the "n" command.
  639.  
  640.  vim:tw=78:ts=8:sw=8:
  641.