Automated file templates

4.8
(74)

VimTricks reader Kyle recently wrote in to ask about file templates. There are plenty of snippet and templating plugins out there. But did you know Vim has a built-in concept called skeleton files which allow you to automatically populate a new file with a given template? Learn about it with :help skeleton but let’s dive into it.

The general idea of skeleton files is that you configure your Vim to read a template from disk every time you open a new, empty buffer with a filename that matches a given pattern. One example might be when writing shell scripts. You can create a skeleton file that contains the Bash shebang line like so:

#!/usr/bin/env bash

Then configure your Vim to populate any new and empty buffer that ends in .sh with this file contents. If you’re working in a language that requires a lot of boilerplate in every file, this can be an enormous time saver.

Another example use case is a README file. You could create a template Markdown file that includes all the headings and basic bullet points that every good README should have. Then whenever you open a new file called README.md in Vim, the buffer will automatically pre-populate with the skeleton template file.

Configuring these templates in your .vimrc is simple:

autocmd BufNewFile readme.md 0r ~/skeletons/readme.md
autocmd BufNewFile *.sh 0r ~/skeletons/bash.sh

In this case, I’ve created a new directory inside my home directory called skeletons but you could put these anywhere. Here’s how this works:

  • autocmd – run this automatically on some event
  • BufNewFile – this is Vim’s new file event
  • readme.md – this is the pattern you want the new file to match
  • 0r – read into the buffer starting at line 0, the first line
  • ~/skeletons/readme.md – the file to read in

There’s a quick demo of this in action below. Let us know what you think in the comments. Do you have any use cases where this is handy? And please reach out with other content you’d like to see covered in future issues of VimTricks.

How useful was this tip?

Average rating 4.8 / 5. Vote count: 74

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

92 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

4 thoughts on “Automated file templates”

  1. Thanks Colin, super helpful!

    I use Vim for my daily journaling and have a bash script to open Vim and read a template file for the headings.

    I also liked the “0r” command. Totally forgot about it!

  2. I like the idea of autocmd but it plays with the last buffer used. For instance I have “ff” set to :e# but the last buffer is now the skeleton file itself. I have to use :b … to get back to the buffer I had been in.

    Is there a solution so that autocmd does now wipe out the last buffer information?

Leave a reply

Your email address will not be published. Required fields are marked *