Create templates for Vim

Developers should follow not only the rules and processes of quality assurance. They must also have advantages for motivation from QA! As QA, you can already achieve more with less. Templates would be a possibility. This guide shows you how to deploy templates for Vim.

Preparation

In order to provide templates, check if the personal vimrc file exists. Do not use the global vimrc! Whatever you configure in the personal vimrc file will overrule any setting in the global vimrc file.

# start vim
$ vim

# the global vimrc file can found by
:echo $VIM

# the home directory can found by
:echo $HOME

# the personal vimrc can found by
:echo $MYVIMRC

# exit vim
:q!

# if no personal vimrc exists
$ touch /home/<user>/.vimrc

# create folder for templates
$ mkdir /home/<user>/templates

Create some templates

Now place some files with in the templates directory.

<!DOCTYPE html>
<html lang="">
<head>
	<title></title>
	<meta charset="UTF-8">
	<meta name="description" content="">
	<meta name="author" content="">
	<meta name="keywords" content="">
	<meta name="robots" content="noindex,follow" />
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link href="" type="text/css" rel="stylesheet" />
</head>
<body>
	<!-- Content goes here... -->
</body>
</html>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Missing docstring"""


class Lorem(object):
    """Missing docstring"""
    
    def __init__(self):
        """Missing docstring"""


if __name__ == '__main__':
    

and so on… . Open the personal vimrc file and add the following auto-command.

# open personal vimrc
$ vim /home/<user>/.vimrc

# change into insert mode
i

# add command
autocmd BufNewFile * silent! 0r $HOME/templates/%:e.tpl

# exit insert mode
<ESC>

# store change and close
:x

Whenever a Developer create now a new file, Vim looks for a template that matches the extension of the file. If there is no template for the file type, then Vim simply creates an empty file as usual.

My vimrc example

" Set color scheme
colorscheme peachpuff

"Show current position
set ruler

" Enable syntax highlighting
syntax enable

" Set standard encoding
set encoding=utf8

" Set standard file type
set ffs=unix,mac,dos

" Use spaces instead of tabs
set expandtab

" Use smart tabs
set smarttab

" 1 tab == 4 spaces
set shiftwidth=4
set tabstop=4

autocmd BufNewFile * silent! 0r $HOME/templates/%:e.tpl