sponsor Vim development Vim logo Vim Book Ad

templates.vim : Load filetype templates for new files

 script karma  Rating 187/80, Downloaded by 3784    Comments, bugs, improvements  Vim wiki

created by
Aristotle Pagaltzis
script type
templates.vim lets you use filetype-dependent templates for new files, using a simple but effective mechanism.

It is, in various ways, a more sophisticated approach to the same problem that vimscript #1160 addresses, but doesn't try to do quite as much.

Usage is simple: when you start with a fresh buffer, setting a filetype on it will load the corresponding template. To test it, open Vim and issue

    :set ft=html

One new command has been added for convenience: it is called :New and takes exactly one argument. It will open a new empty window and set the filetype for it to the argument given.

Templates are kept in “.vim/templates”. The template filename must be equal to the filetype. So when you set the filetype of an empty buffer to “html”, “.vim/templates/html” will be loaded. It's that simple.

In the templates, you can use a cursorline to specify a position for the cursor after loading the template. Such a cursorline works much like a modeline: the word “cursor:” must appear, followed by one or two numbers and opionally the word “del”, all separated by whitespace. The first number specifies the line the cursor will be placed in. The second, if present, specifies a column. The optional word “del” directs the script to remove the cursorline at load time. Take a look at the templates supplied in the package, it should be fairly self-explanatory.
install details
This script uses a new distribution format: it's a self-extracting vim archive. Simply save it somewhere and source it from Vim: it will automatically extract the contained files. It may not yet work well on Windows (creating directories is not implemented, in particular), but I will fix that soon.

Some sample templates are included.

(I plan to write and release another script that creates such archives in the near future. Look for it here on vim.org. In the meantime, you can probably figure out how it works by looking at the source — it's really quite unremarkable.)

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
templates.ar.vim 0.3 2005-01-06 6.0 Aristotle Pagaltzis 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.
SourceForge.net Logo