Home Explore Blog CI



neovim

22th chunk of `runtime/doc/autocmd.txt`
c930db48e011da81e533b344dcc992253bcbab56666441bc0000000100000cfa
 or the 'eventignore' option.

Note: When reading a file (with ":read file" or with a filter command) and the
last line in the file does not have an <EOL>, Vim remembers this.  At the next
write (with ":write file" or with a filter command), if the same line is
written again as the last line in a file AND 'binary' is set, Vim does not
supply an <EOL>.  This makes a filter command on the just read lines write the
same file as was read, and makes a write command on just filtered lines write
the same file as was read from the filter.  For example, another way to write
a compressed file: >

  :autocmd FileWritePre *.gz   set bin|'[,']!gzip
  :autocmd FileWritePost *.gz  undo|set nobin
<
							*autocommand-pattern*
You can specify multiple patterns, separated by commas.  Here are some
examples: >

  :autocmd BufRead   *		set tw=79 nocin ic infercase fo=2croq
  :autocmd BufRead   .letter	set tw=72 fo=2tcrq
  :autocmd BufEnter  .letter	set dict=/usr/lib/dict/words
  :autocmd BufLeave  .letter	set dict=
  :autocmd BufRead,BufNewFile   *.c,*.h	set tw=0 cin noic
  :autocmd BufEnter  *.c,*.h	abbr FOR for (i = 0; i < 3; ++i)<CR>{<CR>}<Esc>O
  :autocmd BufLeave  *.c,*.h	unabbr FOR

For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.): >

  :autocmd BufEnter  ?akefile*	set include=^s\=include
  :autocmd BufLeave  ?akefile*	set include&

To always start editing C files at the first function: >

  :autocmd BufRead   *.c,*.h	1;/^{

Without the "1;" above, the search would start from wherever the file was
entered, rather than from the start of the file.

						*skeleton* *template*
To read a skeleton (template) file when opening a new file: >

  :autocmd BufNewFile  *.c	0r ~/vim/skeleton.c
  :autocmd BufNewFile  *.h	0r ~/vim/skeleton.h
  :autocmd BufNewFile  *.java	0r ~/vim/skeleton.java

To insert the current date and time in a "*.html" file when writing it: >

  :autocmd BufWritePre,FileWritePre *.html   ks|call LastMod()|'s
  :fun LastMod()
  :  if line("$") > 20
  :    let l = 20
  :  else
  :    let l = line("$")
  :  endif
  :  exe "1," .. l .. "g/Last modified: /s/Last modified: .*/Last modified: " ..
  :  \ strftime("%Y %b %d")
  :endfun

You need to have a line "Last modified: <date time>" in the first 20 lines
of the file for this to work.  Vim replaces <date time> (and anything in the
same line after it) with the current date and time.  Explanation:
	ks		mark current position with mark 's'
	call LastMod()  call the LastMod() function to do the work
	's		return the cursor to the old position
The LastMod() function checks if the file is shorter than 20 lines, and then
uses the ":g" command to find lines that contain "Last modified: ".  For those
lines the ":s" command is executed to replace the existing date with the
current one.  The ":execute" command is used to be able to use an expression
for the ":g" and ":s" commands.  The date is obtained with the strftime()
function.  You can change its argument to get another date string.

When entering :autocmd on the command-line, completion of events and command
names may be done (with <Tab>, CTRL-D, etc.) where appropriate.

Vim executes all matching autocommands in the order that you specify them.
It is recommended that your first autocommand be used for all files by using
"*" as the file pattern.  This means

Title: Autocommand Patterns, Templates, and Execution Order in Nvim
Summary
This section describes how to specify multiple patterns in autocommands, separated by commas, with examples for C files, Makefiles, and skeleton/template files. It includes an example of inserting the current date and time in HTML files when writing them. It also explains that Vim executes matching autocommands in the order they are specified, recommending that the first autocommand should be for all files using "*" as the file pattern.