Saturday, December 12, 2015

How to Add Custom Header to Your .C File Automatically in vi Editor.

In this article I am going to tell you how to add default header text to your .c file, or how to create a header section in your C Programming code with File Name, Creation Date/Time, Last Modified Date/Time, Author Name, Organization Name automatically populated when you open a file in Vi. Whenever a new .c file is created. There is a feature vi editor called "autocommand", using this feature you can specify some vi commands to be executed automatically while reading or writing a file, or while entering/leaving a buffer/window, or while exiting vi editor.

Vi autocmd syntax: autocmd {event} {pattern} {cmd}

Events: Following are some autocmd events.

BufNewFile - Starting to edit a file that doesn't exist.
FileReadPre - Before reading a file with a ":read" command.
BufWritePre - Starting to write the whole buffer to a file.
FileWritePre - Starting to write part of a buffer to a file.
BufDelete - Before deleting a buffer from the buffer list.
BufWipeout - Before completely deleting a buffer.
BufNew - Just after creating a new buffer.
BufEnter - After entering a buffer.
BufLeave - Before leaving to another buffer.
SwapExists - Detected an existing swap file.

Suppose that I need the following template to be loaded automatically while opening a new “.c” file in Vi.

/*========================================================================================
**
**  File Name       :
**  Creation Date :
**  Last Modified  :
**  Compoler        : gcc
**  Author             : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization   : SCIS, University of Hyderabad, India.
**
**========================================================================================*/

You can achieve this in three steps as mentioned below.
Step-1: Create a template file
Save the above template in a text file with “:insert” in the first line, followed by the template and a “.”(dot) in the last line as shown below.

:insert
/*==========================================================================================
**
**  File Name       :
**  Creation Date :
**  Last Modified :
**  Compoler       : gcc
**  Author            : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**=========================================================================================*/
.


Step-2: Add autocmd commands to ~/.vimrc
Add the following lines in the ~/.vimrc file.

autocmd bufnewfile *.c so /home/manoj/c_header.txt
autocmd bufnewfile *.c exe "1," . 5 . "g/File Name     :.*/s//File Name     : " .expand("%")
autocmd bufnewfile *.c exe "1," . 5 . "g/Creation Date :.*/s//Creation Date : " .strftime("%c")
autocmd Bufwritepre,filewritepre *.c execute "normal ma"
autocmd Bufwritepre,filewritepre *.c exe "1," . 10 . "g/Last Modified :.*/s/Last Modified :.*/Last Modified : " .strftime("%c")
autocmd bufwritepost,filewritepost *.c execute "normal `a"

Step-3: Create a new *.c file with automatic header

Now, when you create a new *.c file using vim, this will automatically add the header defined in the Step1 and populate the File Name and Creation Date automatically as shown below.

/*==============================================================================================
**
**  File Name       : first.c
**  Creation Date : Sat 12 Dec 2015 10:06:12 PM IST
**  Last Modified :
**  Compoler       : gcc
**  Author            : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**============================================================================================*/ 

When you save the first.c file, it will automatically update the Last Modified field accordingly as shown below.

/*=======================================================================================
**
**  File Name       : first.c
**  Creation Date : Sat 12 Dec 2015 10:06:12 PM IST
**  Last Modified : Sat 12 Dec 2015 10:41:35 PM IST
**  Compoler       : gcc
**  Author            : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**========================================================================================*/ 

Explanation of the autocmd commands inside ~/.vimrc

1 autocmd bufnewfile *.c so /home/manoj/c_header.txt
2 autocmd bufnewfile *.c exe "1," . 5 . "g/File Name :.*/s//File Name : " .expand("%")
3 autocmd bufnewfile *.c exe "1," . 5 . "g/Creation Date :.*/s//Creation Date : " .strftime("%d-%m-%Y")
4 autocmd Bufwritepre,filewritepre *.c execute "normal ma"
5 autocmd Bufwritepre,filewritepre *.c exe "1," . 5 . "g/Last Modified :.*/s/Last Modified :.*/Last M
odified : " .strftime("%c")
6 autocmd bufwritepost,filewritepost *.c execute "normal `a"
 
Line 1 defines the template file. This indicates that for *.c file, /home/manoj/c_header.txt template file 
should be used.
Line 2 will search for the pattern “File Name :” from the 1st line to 5th line. If found, it will write the current 
filename in that line. 
Line 3 will update the Creation Date field. 
Line 5 will update the Last Modified field with the current date and time when you save the file.
Line 4 & 6: While saving the file, the cursor will move to the “Last modified :”(because of last write operation). 
If you want the cursor back to the previous position then, you need to add Line 4 and 6 
to the .vimrc file. 
Line 4 will mark the current cursor position before updating.
Line 6 will restore the cursor position back to its previous position.
 

Note:

1. Verify whether autocmd is enabled in Vi / Vim – Execute :version from vi / vim. If autocommand feature is enabled, it will display +autocmd.

2. Autocommand help: Execute :help au from vi / vim, to get quick help on vim autocmd features.

 

If you like what you show
Please like, share and comment in the comment box bellow.
Big thanks for reading..!

No comments:

Post a Comment