Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.