Simple Vim templates plugin
|Author:||Adrian Perez <firstname.lastname@example.org>|
This is a simple plugin for Vim that will allow you to have a set of templates for certain file types. It is useful to add boilerplate code like guards in C/C++ headers, or license disclaimers.
The easiest way to install the plugin is to install it as a bundle. For example, using Pathogen:
- Get and install pathogen.vim. You can skip this step if you already have it installed.
git clone git://github.com/aperezdc/vim-template.git
Bundle installs are known to work fine also when using Vundle. Other bundle managers are expected to work as well.
In order to update the plugin, go to its bundle directory and use Git to update it:
:BundleUpdate command provided by Vundle, for example invoking
Vim like this:
% vim +BundleUpdate
In your vimrc you can put:
let g:templates_plugin_loaded = 1to skip loading of this plugin.
let g:templates_no_autocmd = 1to disable automatic insertion of template in new files.
let g:templates_directory = '/path/to/directory'to specify a directory from where to search for additional global templates. See template search order below for more details. This can also be a list of paths.
let g:templates_name_prefix = '.vimtemplate.'to change the name of the template files that are searched.
let g:templates_global_name_prefix = 'template:'to change the prefix of the templates in the global template directory.
let g:templates_debug = 1to have vim-template output debug information
let g:templates_fuzzy_start = 1to be able to name templates with implicit fuzzy matching at the start of a template name. For example a template file named
let g:templates_tr_in = [ '.', '_', '?' ]and
let g:templates_tr_out = [ '\.', '.*', '\?' ]would allow you to change how template names are interpretted as regular expressions for matching file names. This might be helpful if hacking on a windows box where
*is not allowed in file names. The above configuration, for example, treates underscores
_as the typical regex wildcard
let g:templates_no_builtin_templates = 1to disable usage of the built-in templates. See template search order below for more details.
let g:templates_user_variables = [[USERVAR, UserFunc]]to enable user-defined variable expanding. See User-defined variable expanding below for details.
There are a number of options to use a template:
Create a new file giving it a name. The suffix will be used to determine which template to use. E.g:
$ vim foo.c
In a buffer, use
:Template *.footo load the template that would be loaded for file matching the pattern
*.foo. E.g. from within Vim:
Template search order
The algorithm to search for templates works like this:
- A file named
.vim-template:<pattern>in the current directory. If not found, goto (2). If there are multiple template files that match a given suffix in the same directory, the one that is most specific is used.
- Go up a directory and goto (1), if not possible, goto (3).
- Try to use the
=template=<pattern>file from the directory specified using the
g:templates_directoryoption (only if the option is defined and the directory exists).
- Try to use the
=template=<pattern>file supplied with the plugin (only if
g:templates_no_builtin_templateswas not defined).
Variables in templates
The following variables will be expanded in templates:
- Numerical day of the month, year and month.
- Date in
- Time in
- Full date (date + time), in
- File name, without extension.
- File name, with extension.
- File extension.
- Current user's e-mail address. May be overriden by defining
- Current logged-in user name. May be overriden by defining
- Host name.
- A string with alphanumeric characters and underscores, suitable for use in proprocessor guards for C/C++/Objective-C header files.
- File name, without extension, and the first character of every word is capital
- File name, without extension, and all characters are capitals.
- File name, without extension, the first character of every word is capital, and all underscores are removed.
- Expands to nothing, but ensures that the cursor will be placed in its position after expanding the template.
User-defined variable expanding
You can set
g:templates_user_variables to expand custom variables. It should
be something like
[['USERVAR1', 'UserFunc1'], ['USERVAR2', 'UserFunc2']],
USERVAR1 is the variable to be expanded and
UserFunc1 is the name of
the function that returns the result. The function should take no arguments and
return the string after expansion.
let g:templates_user_variables = [['FULLPATH', 'GetFullPath']] function GetFullPath() return expand('%:p') endfunction
And each occurrence of
%FULLPATH% in template will be replaced with the full
path of current file.