Auto-generated file headers with vi

Dec. 2, 2011, 8:01 p.m.

It's quite needless to say, that vi is a really powerful editor. So lately I've attended a training course at work and the referent was using vi to edit his source files. Everytime he opened a new file, he already had a nice header in his source or header file, like:

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.

* File Name : test.c

* Purpose :

* Creation Date : 02-12-2011

* Last Modified : Fr  2 Dez 19:45:44 2011

* Created By : anonymous

_._._._._._._._._._._._._._._._._._._._._.*/

So I asked him, how this could be done with vi and he told me, he was using autocmd lines within his .vimrc file and a skeleton text file. After googling around a while, I found out, how you can create this headers:

Create a skeleton text file somewhere that contains the following:

cat c_header.txt

:insert  
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.

* File Name :

* Purpose :

* Creation Date :

* Last Modified :

* Created By :

_._._._._._._._._._._._._._._._._._._._._.*/

Edit your .vimrc file (usually within your home folder) to contain the following lines (note that every command is one line, the backslashes within the following block are just there to format the output within this blog entry):

autocmd bufnewfile *.c,*.h,*.cpp so /path/to/your/c_header.txt

autocmd bufnewfile *.c,*.h,*.cpp exe "1," . 10 . \  
    "g/File Name :.*/s//File Name : " .expand("%")

autocmd bufnewfile *.c,*.h,*.cpp exe "1," . 10 . \  
    "g/Creation Date :.*/s//Creation Date : " .strftime("%d-%m-%Y")

autocmd bufnewfile *.c,*.h,*.cpp exe "1," . 12 . \  
    "g/Created By :.*/s//Created By : " .$USER

autocmd Bufwritepre,filewritepre *.c,*.h,*.cpp \  
    execute "normal ma"

autocmd Bufwritepre,filewritepre *.c,*.h,*.cpp exe "1," . 10 . \  
    "g/Last Modified :.*/s/Last Modified :.*/Last Modified : " .strftime("%c")

autocmd bufwritepost,filewritepost *.c,*.h,*.cpp \  
    execute "normal `a"

That's all. As I told you: vi is amazing.

tags: autocmd header vi

read more