How to Create Custom Header Template for Shell Scripts in Vim

In this article, we will show you a simple way to configure a custom header for all newly created bash scripts in Vim editor. This means that every time you open a new .sh file using vi/vim editor, the custom header will be automatically added to the file.

How to Create Custom Bash Script Header Template File

First start by creating the template file called sh_header.temp, which contains your custom bash script header, possibly under ~/.vim/ directory under your home.

$ vi ~/.vim/sh_header.temp

Next add the following lines in it (feel free to set your own template file location and custom header) and save the file.

Custom Header Template for Scripts
#!/bin/bash 

###################################################################
#Script Name	:                                                                                              
#Description	:                                                                                 
#Args           	:                                                                                           
#Author       	:Aaron Kili Kisinga                                                
#Email         	:[email protected]                                           
###################################################################
Create Custom Header Template for Scripts

Create Custom Header Template for Scripts

The template above will automatically add the required “shebang” line: “#!/bin/bash” and your other custom headers. Note that in this example, you will manually add the script name, description and arguments when editing your script content.

Configure autocmd in Vimrc File

Now open your vim initialization file ~/.vimrc for editing and add the following line to it.

au bufnewfile *.sh 0r /home/aaronkilik/.vim/sh_header.temp

Where:

  • au – means autocmd
  • bufnewfile – event for opening a file that doesn’t exist for editing.
  • *.sh – consider all files with .sh extension.

So the above line instructs vi/vim editor to read the contents of the template file (/home/aaronkilik/.vim/sh_header.temp) and insert it into every new .sh file opened by a user.

Configure Vimrc File

Configure Vimrc File

Test Custom Bash Script Header in New Script File

Now you can test if all is working by opening a new .sh file using vi/vim editor, and your custom header should be auto-added there.

$ vi test.sh
Verify Custom Header in New Scripts

Verify Custom Header in New Scripts

For more information, see the Vim autocmd documentation.

Lastly, here are some useful guides concerning bash scripting and vim editor:

  1. 10 Useful Tips for Writing Effective Bash Scripts in Linux
  2. 10 Reasons Why You Should Use Vi/Vim Text Editor in Linux
  3. How to Password Protect a Vim File in Linux
  4. How to Enable Syntax Highlighting in Vi/Vim Editor

That’s all! If you have any questions or useful bash scripting tips and tricks to share, use the comment form below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

5 Responses

  1. Manav says:

    How it will work if I use same script on another machine?

  2. Hossein says:

    Hi, I do this changes in my vimrc file of VIM and save this, but when i create new bash file with command “vim test1.sh“, file created but when i want to run this sh file, system show me Error “permission denied“.

    • Aaron Kili says:

      @Hossien

      Check the file permissions, ensure that you have made the script executable like this:

      $ chmod +x test1.sh
      

      and that the directory in which you have stored it has permissions to allow you run it.

  3. Jeremy Van Veelen says:

    On RHEL (and it’s derivatives) simple running /usr/bin/vi invokes the small version that does not include the autocmd feature.

    To get the autocmd feature one needs to run the huge version – /usr/bin/vim.

Leave a Reply to Jeremy Van Veelen Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.