sponsor Vim development Vim logo Vim Book Ad

Templates for Files and Function Groups : Yet another file template script.

 script karma  Rating 24/24, Downloaded by 1634  Comments, bugs, improvements  Vim wiki

created by
Al Budden
script type
Dr. Beco has a newer and better maintained version of this plugin available here: https://github.com/drbeco/vimtemplates

This script is a modified version of the template plugin documented here: http://lucumr.pocoo.org/cogitations/2007/08/03/vim-file-templates/.

Key features:

Multiple templates (optionally with a default) for any file type; for example, you can have a template for a given file type and for each project you're working on or company you're working for.

Support for automatic population of certain parameters, such as the file name, the year, the date or an upper case version of the basename etc etc (more can be added very easily).

Other parameters can be included and a dialog box will pop up to ask for the value.  The last-used value (from this Vim session) will be remembered as the default.
Support for fixed-width entries.

A template is just a source file stored in ~/.vim/templates or vimfiles/templates (depending on OS) with some identifiers inserted in appropriate places.  An identifier starts with "<+" and ends with "+>".  In the identifier, there can be:

Keywords surrounded by '$' signs: these are automatically expanded.  Supported keywords are (examples shown for file Flash.c):

$BASENAME$ (e.g. Flash)
$LBASENAME$ (e.g. flash)
$FILENAME$ (e.g. Flash.c)
$YEAR$ (e.g. 2008)
$DATE$ (e.g. 3/9/2008)

Keywords surrounded by '#' signs: the user will be asked what these mean (the same replacement will be used for all entries in the file).  For example, you may want to have:


A semicolon followed by an 'R' and then zero or more '+' signs.  This provides a fixed-width entry.  See the included examples for details.

There are example C source and header templates included in the distribution.

After creating a new file and setting the file name, the template is loaded with the command

:LoadFileTemplate <TemplateName>

Where <TemplateName> should be replaced by the name of your template (e.g. ":LoadFileTemplate Example" for the example template).  If you're editing a '.c' file, example.c will be loaded and if you're editing a '.h' file, example.h will be loaded.  If you add an entry in .vimrc to set the global variable g:file_template_default, it is then possible to simply run ":LoadFileTemplate" and the default will be used.

In addition, you can use the command:

:AddTemplate <TemplateName>

to add a template at the current location in the current file.  I use this (for example) to add a circular buffer to an existing module.  Again, it will differentiate by extension, so you can have a template for the code implementation and the header definitions.

More details available at:

install details

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
file_templates.vba r630 2012-01-23 7.0 Al Budden Removed dependency on VIMFILESDIR being defined: find the templates directory using globdir.
file_templates.vba r233 2009-05-21 7.0 Al Budden Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.