VIM autocmd

Want Vim to automatically do things to new or existing files?  Autocmd to the rescue!  Here I will demo a very simple example that I use for when I create/edit bash/python files.

I created the directory .vim in the root of my home directory and then created the "header" files that I wanted.

$ cat .vim/python_header
:insert
#!/usr/bin/python3
################################################################################
####
#### Filename:
####
#### Purpose:
####
#### Created on:
####
#### Author:
####
#### Last Modified:
####
################################################################################
.

The first line must be :insert and the last line must be a period '.'

I then added the following lines to the .vimrc file in the root of my home directory.

" Python file header
autocmd bufnewfile *.py so /home/cdstealer/.vim/python_header
autocmd bufnewfile *.py exe "4" "g/Filename:.*/s//Filename:\t\t\t" .expand("%")
autocmd bufnewfile *.py exe "6" "g/Purpose:.*/s//Purpose:\t\t\t"
autocmd bufnewfile *.py exe "8" "g/Created on:.*/s//Created on:\t\t" .strftime("%c")
autocmd bufnewfile *.py exe "10" "g/Author:.*/s//Author:\t\t\t" .$USER
autocmd Bufwritepre,filewritepre *.py execute "normal ma"
autocmd BufWritePre,FileWritePre *.py exe "12" "g/Last Modified:.*/s/Last Modified:.*/Last Modified:\t\t" .strftime("%c") . " by " . $USER
autocmd bufwritepost,filewritepost *.py execute "normal `a"

Note: Lines that start with a double quote are treated as comments.

The official docs can be found here.

Explanation:

The first autocmd line specifies the file.  Due to the :insert, this is then inserted into the new file.

bufnewfile: means take the following action on a new file.
Bufwritepre,filewritepre: means update the file only if editing not creating.

Lines 2, 3, 4 & 5 are basic search and replace for the header file being inserted.

The number in quotes after the exe is the line number to work on.  You can set this to a range instead of being specific.  The syntax is:

"<starting line number>," . <end line number> .

Once saved, you can test straight away by starting a new file:

$ vi python_test.py

1 #!/usr/bin/python3
2 ################################################################################
3 ####
4 #### Filename: python_test.py
5 ####
6 #### Purpose:
7 ####
8 #### Created on: Fri 30 Dec 2016 19:18:58 GMT
9 ####
10 #### Author: cdstealer
11 ####
12 #### Last Modified:
13 ####
14 ################################################################################

Save the file and then re-open, make a small change and the "Last Modified" line will now contain a date stamp.

1 #!/usr/bin/python3
2 ################################################################################
3 ####
4 #### Filename: python_test.py
5 ####
6 #### Purpose: To test
7 ####
8 #### Created on: Fri 30 Dec 2016 19:18:58 GMT
9 ####
10 #### Author: cdstealer
11 ####
12 #### Last Modified: Fri 30 Dec 2016 19:20:38 GMT by cdstealer
13 ####
14 ################################################################################

Very very useful ;)