vim-templates — using template files for vim
What and why?
vim-templates is great plugin which adds capability of generating files from a predefined templates. It comes with few predefined templates, which are present in the plugin directory and are used as default templates.You can customize them and create your own templates to use in you project. This helps you to maintain consistency across your project or projects, even among the entire organization where vim is used extensively. Many modern editors have this feature where you can define your templates per file basis. vim-templates brings this feature in vim.
One great use case would be copyright and licensing notice, which needs to be included in all of your files. With this plugin you can put them in the template file and it will automatically put them in each of your file you create one or if you use sample copyright and licensing in more than one of your project you can create a LICENSE file and include it in the template.
How does it work?
To generate files from it needs two things extension of the file and template associated with the extension, which is named as <ext>.template. When you create a new file through vim it finds out the extension of the file reads the template file for the extension and puts it in the file. If the template contains any variables, which are supported by the plugin, it replaces them with their values. The plugin searches for templates as follows
- In folders named
templatesrecursively up the directory tree, i.e. first in a directorytemplatesunder the current working directory, then in../templates, then '../../templates', ... - In search paths defined by
g:tmpl_search_pathsin your.vimrcfile - The
templatesfolder in this plugin's directory
If you don’t provide any extension in the filename, it doesn’t do anything in that case you can run the vim command with the file extension as argument to load the template.
Example
h.template
/**
* @author : {{NAME}} ({{EMAIL}})
* @created : {{TODAY}}
* @filename : {{FILE}}
*/
#ifndef {{MACRO_GUARD}}
#define {{MACRO_GUARD}}
/* declarations */
#endif /* {{MACRO_GUARD}} */generates this for a file named header.h
/**
* @author : Vikas Kesarwani (vikash@abc.com)
* @created : 07/09/2018
* @filename : header
*/
#ifndef HEADER_H
#define HEADER_H
/* declarations */
#endif /* HEADER_H */To learn more about how to configure and customize your templates visit project page on github
