How to Configure Custom Headers to the shell script files automatically?

Venu Madhav
3 min readJun 11, 2020

--

In this article we will learn how to create and configure the Customized Header templates to the newly created files automatically. This will help many Linux users who write a number of shell scripts regularly and add Header sections manually each and every time.

Below is sample customized Header Template, which adds automatically when a new shell file is created.

#!/bin/bash##################################################################
#Script Name :
#Description :
#Creation Date :
#Author : Venu Madhav Palugula
#Email : xxx@gmail.com
##################################################################

To configure the Header Template, we need to follow the below steps:

  1. First make sure the file .vimrc and the directory .vim are present in your logged in user home directory i.e. where the .bashrc and .bash_profile files are present. If not, please create the above mentioned directory and file in users home directory.
[venram@centos7 ~]$ pwd
/home/venram
[venram@centos7 ~]$ ls -ld .vim
drwxrwxr-x. 2 venram venram 46 Jun 7 22:03 .vim
[venram@centos7 ~]$ ls -lrt .vimrc
-rw-rw-r--. 1 venram venram 245 Jun 7 21:41 .vimrc

2. Create the Template file named sh_header.txt under the .vim directory.

vi ~/.vim/sh_header.txt

Next add the below Lines in the above created sh_header.txt file and save it.

#!/bin/bash##################################################################
#Script Name :
#Description :
#Creation Date :
#Author : Venu Madhav Palugula
#Email : xxx@gmail.com
##################################################################

The template will auto populate the above contents when a new file is created.

3. Configure the autocmd in Vimrc file.

4. autocmd is a feature where we can specify specific commands to be executed automatically, when reading or writing a file, when entering or leaving a buffer or window, and when exiting Vim editor.

5. Add the below lines in the .vimrc file which was created earlier.

au bufnewfile *.sh 0r /home/venram/.vim/sh_header.txtau bufnewfile *.sh exe "1," . 10 . "g/Script Name :.*/s//Script Name : " .expand("%")autocmd bufnewfile *.sh exe "1," . 10 . "g/Creation Date :.*/s//Creation Date : " .strftime("%d-%m-%Y")
  • The First Line indicates that, when a new file with *.sh extension is created, it will use the defined header template file.
  • The Second Line indicates, it will search for the pattern “Script Name: ” from Line 1 to Line 10 and if found, it will write the Entered File name in that field.
  • The Third Line indicates that, it will update the creation time stamp.

au means autocmd

bufnewfile — starting to edit a file that doesn’t exist.

*.sh — Applicable to only the files which has *.sh extension

Note: /home/venram/ is my user home directory. Please change this with your home directory and sh_header.txt is the file name which we created earlier.

6. Now, test the Custom Header script by creating a new shell file.

7. Lets create a new shell file named test.sh. Now, we can see the custom header template will be added automatically.

#!/bin/bash##################################################################
#Script Name : test.sh
#Description :
#Creation Date : 07-06-2020
#Author : Venu Madhav Palugula
#Email : xxx@gmail.com
##################################################################

8. There are many autocmd features available in the documentation to try out. Above are only basic and most regularly used one.

9. The above one not only restrict to shell files, we can use for any files. Just replace the *.sh with your file extension.

10. Lastly, if you want to view the autocmd documentation in linux, just enter the vi , which opens the vi editor and then type :help autocmd

That’s all, Hope its Helpful!…

Continue Learning and Sharing.

--

--