vim coding templates

3

0

I recently started learning vim and, I'm curious, what's the best way to setup a sort of bare-bones template for specific types of files?

For example: say I wanted to automatically load some C #includes and the main() call with a keystroke (or whenever I create a new .c file) What's the most efficient way to do this?

I was thinking I could write a vimscript function and then map that to a key, or turn it into a macro, but I don't think the macro would be permanent, correct?

Any ideas? All help is appreciated. Thanks!

user193524

Posted 2013-01-28T21:19:10.670

Reputation: 33

Answers

2

I use something like this:

au BufNewFile * silent! 0r ~/.vim/skeleton/template.%:e

This creates an autocommand that loads a template file for each new file it opens. %:e expands to the file extension, so opening a new file test.h would load boiler plate ~/.vim/skeleton/template.h

You can then add whatever you want inside template.h

Steven

Posted 2013-01-28T21:19:10.670

Reputation: 135

3

I use SnipMate for that but there are many other ways.

romainl

Posted 2013-01-28T21:19:10.670

Reputation: 19 227