Vim: Automatic Last Updated tag

Ever wanted to know when a script was last updated and don’t necessarily want to implement a version control tool like subversion? Adding a comment in your script with the date of the last change is a good start but relies on manually updating the comment every time a change is made. Adding the code below to your .vimrc file (and/or in the .vimrc file of the root user) will automate this for you, it will also add the username of the editor. Of course this is no proper way of auditing but it has proven quite useful when working in a team of 3 to 5 system administrators. This also works well for apache vhost files files or DNS zone files for instance.

autocmd BufWritePre /usr/local/bin/*,/usr/local/sbin/*,~/bin/*,*.sh,*.html,*.pl,*/check_* ks|call LastMod()|'s
fun LastMod()
 if line("$") > 20
 let l = 20
 let l = line("$")
 let editor_name=system('logname')
 exe "1," . l . "g/^# Modified:/s/# Modified:.*/# Modified: " .
 \ strftime("%c") . " by: " . editor_name

In the first line you can add file extensions or patterns of files you want to use this function for. Then when you create a new file, say for instance, add the following line:

 # Modified: x

The x is arbitrary, it will be replaced with the current date & time once the file is saved.

The expression to find the ‘# Modified:’ tag could be changed to cater for files that use a different symbol for in-line comments

Leave a Reply