tmpl.vim
provides autoloading new file templates for vim and Neovim. Templates
are kept in any templates
directory on the runtimepath
, allowing you to
easily add your own templates separately from this plugin. By setting
g:tmplvim_default_environment
, different templates for the same file type may
be loaded in different environments (e.g. work versus personal).
Template names are constructed as environment.type
. The environment may be set
using g:tmplvim_default_environment
, which defaults to default
. For file
names with an extension the file extension is taken as the template type, or for
files with no extension the lower-case name of the file is used as the template
type.
It is recommended to use a plugin manager to install tmpl.vim
.
With vim-plug
Plug 'jgoguen/tmpl.vim'
With dein.vim
call dein#add('jgoguen/tmpl.vim')
With minpac
call minpac#add('jgoguen/tmpl.vim')
Clone the repository to pack/plugins/start
:
For vim 8:
mkdir -p ~/.vim/pack/plugins/start
git clone https:/github.com/jgoguen/tmpl.vim.git ~/.vim/pack/plugins/start/tmpl.vim
For Neovim:
mkdir -p "${XDG_DATA_HOME:-${HOME}/.local/share}/nvim/site/pack/plugins/start"
git clone https://github.com/jgoguen/tmpl.vim.git "${XDG_DATA_HOME:-${HOME}/.local/share}/nvim/site/pack/plugins/start"
To set the template environment, set g:tmplvim_default_environment
. To
override the template environment on a per-buffer basis, set
b:tmplvim_default_environment
for the specific buffers. If this is not given,
the default value is default
.
To set the replacement value for the <# AUTHOR #>
tag, set g:tmplvim_author
.
If this is not given, the default value is the ${USER}
environment variable.
To set the replacement value for arbitrary template tags, use the dictionary
g:tmplvim_template_vars
. Set the tag name as the dictionary key and the value
as the tag replacement value.
Templates may contain tags which will be automatically expanded when the template is loaded into the buffer. There are two types of tags:
- Template variables
- Template includes
Template includes are processed first, then variables once includes have been inserted into the buffer. Nested includes are not currently supported.
Template variable tags are upper-case letters surrounded by <#
and #>
. The
value of a template variable comes from g:tmplvim_template_vars
, except for
some specific reserved tag names:
DATE
: Always replaced with the value ofstrftime('%Y-%m-%d')
(e.g.2020-03-04
)YEAR
: Always replaced with the value ofstrftime('%Y')
(e.g.2020
)TIME
: Always replaced with the value ofstrftime('%H:%M:%S')
(e.g.13:03:34
)AUTHOR
: Always replaced with the value ofg:tmplvim_author
, or the value of$USER
if that is not set.DIRNAME
: Always replaced with the name of the directory the file is in.BASENAME
: Always replaced with the name of the file without extensions.UPPERBASENAME
: Always replaced with the name of the file without extensions in upper-case.
Template include tags are letters, numbers, or periods surrounded by <$
and
$>
. The tag name is expected to be the name of another template, which may
include template variables but may not include other template includes. For
example, the include tag <$ LICENSE.MIT $>
will be replaced with the contents
of the template file named LICENSE.MIT
.
To accommodate including files in source code templates, an include with the
same extension as the current buffer will be searched for and preferred if
found. For example, the include tag <$ LICENSE.MIT $>
in setup.py
would use
the template named LICENSE.MIT.py
if it exists, or LICENSE.MIT
if not.
When a new file is opened, the corresponding template is automatically loaded.