Configure Custom Header Template in Vim editor:

saurav omar
3 min readJun 24, 2019

--

In this article we will see how can we configure custom headers while opening any file. If you are hardcore user of command line or devops engineer or you write lot of scripts using VIM editor then this become handy.

For Example: When you are write any scripts you always want some custom headers by default

#!/bin/bash -#===================================================================#     FILE: {file}.sh#     USAGE: ./commands.sh
#
# DESCRIPTION:
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: YOUR NAME (),
# ORGANIZATION:
# CREATED: date
# REVISION:
# ---#===================================================================

Let’s see how can we configure

Vim Basic Syntax

:au[tocmd] [group] {event} {pat} [nested] {cmd}

Its look bit complex as per our use case let’s make it simpler

:autocmd {event} {pat} {cmd}

Events: There are around 50+ events are present reading, writing buffers etc.

Reading
|BufNewFile| starting to edit a file that doesn't exist
|BufReadPre| starting to edit a new buffer, before reading the file
|BufRead| starting to edit a new buffer, after reading the file
|BufReadPost| starting to edit a new buffer, after reading the file
|BufReadCmd| before starting to edit a new buffer |Cmd-event|

|FileReadPre| before reading a file with a ":read" command
|FileReadPost| after reading a file with a ":read" command
|FileReadCmd| before reading a file with a ":read" command |Cmd-event|

|FilterReadPre| before reading a file from a filter command
|FilterReadPost| after reading a file from a filter command

|StdinReadPre| before reading from stdin into the buffer
|StdinReadPost| After reading from the stdin into the buffer

Pattern: so only for selected pattern this command will work. for example we only wanted to write in .sh file so we gonna use *.sh

Cmd: basic command which we wanted to execute.

Don’t worry for now if you cannot understand, will learn by configuring below custom template.

#!/bin/bash####################################################################File Name:#Usage:#Description:#Args:#Author:Saurav Omar#Email:sauravomar01@gmail.com#Creation Date: ###################################################################

Below are the steps through which we can configure custom template.

Configure Custom Template:

  • Create template file (preferably under .vim directory) and add below content.

vim ~/.vim/header_template.

:insert#!/bin/bash####################################################################File Name:#Usage:#Description:#Args:#Author: Your name #Email: your email id#Creation Date:#Last Modified:###################################################################
  • Add this template in .vimrc(by default its present inside your home directory) file so that template start getting populated whenever you open a new file.
autocmd bufnewfile *.sh so ~/.vim/header_template.

autocmd: list of commands which gets executed by vim

event -> bufnewfile: starting to edit a non-existent file

pattern->*.sh : execute only this on files which has .sh extension

That’s it we are done. if you open file with extension *.sh then you see all the templates getting populated in new files.

We have seen basics configuration of custom templates. Let’s see how can we auto populate custom variables like date, file name etc.

Auto Populate Custom Variable:

If we want to Auto populate variables whenever new file is opened.

Configure file name:

Add below line again in

autocmd bufnewfile *.sh exe “1,” . 10 . “g/File Name:/s//File Name: “ .expand(“%”)
  • It will search for the pattern “File Name:” from the 1st line to 10th line. If found, it will write the current filename in that line expand commands return the file.

Configure Created Date :

similar as above replacing file name it will search for a pattern Creation Date and replace with the current time .strftime(“%c”)

autocmd bufnewfile *.sh exe "1," . 11 . "g/Creation Date:/s//Creation Date: " .strftime("%c")

Like wise we can populate a lot variable depends upon the requirements

Now this will just update the variable value in buffer because this is just buffered (or only present in buffer ) below command will write in file or flush in the file

autocmd Bufwritepre,filewritepre *.sh execute "normal ma"

filewritepre: starting to write part of a buffer to file

That’s It

Keep the learning fire on.

--

--