The Path of Least Resistance: Using Vim Templates
My Kirby powered website uses a few different templates depending on the type of content. I have one for detailing a project, one for news, one for events and one for this blog you're reading now. Whilst Kirby has a wonderful CMS that you can use, I prefer to do all my updating myself through text files, using Vim. Rather than have to remember the format of a particular template to then populate with content, I can use Vim's template feature to start with a boiler plate template depending on the content type, and even have it auto populate with dynamic information such as the current date. Here's how I go about doing that.
My Blog Template
The template for my blog is very simple and looks like this:
Title: Date: Text: Tags:
To create a template for my blog posts, I create a text file called article.txt and save the above content into that file inside ~/Dropbox/vim/Templates Saving this inside Dropbox means it is available to me across the different machines I use.
Next I need to edit my .vimrc to tell Vim the location of this text file, and what file type it's associated with. My vimrc is stored in my Dropbox/vim folder, again so it carries across to different machines, but yours may not be.
With the .vimrc open I then add these lines to define my template:
augroup filetype_article.txt autocmd! autocmd BufNewFile article.txt 0r ~/Dropbox/vim/Templates/article.txt augroup END
This sets up Vim so when I create a new file called article.txt it will read in the contents of my template and use it to populate this newly created file. Very nice.
Inserting Dynamic Information
Yet there's more I can do. For instance I don't want to have to type the date each time I create a new blog post. Handily we can get vimscript to do this after populating the file with our template by adding this line after the template definition:
autocmd BufNewFile article.txt 5$pu=strftime('%Y-%m-%d %H:%M:%S')
This will put (paste) the current date in the format we specify at the end of line 5.
Finally I can do one more thing to make this even more efficient, by getting Vim to go into insert mode at the end of the Title: line, ready for me to instantly start typing like this:
autocmd BufNewFile article.txt :normal ggA
This will tell Vim to enter Normal mode and execute 'gg' to go to the top of the buffer, and then, by using the command 'A', position the cursor at the end of the line and enter Insert mode, ready for me to begin typing the article title.
The final autogroup now looks like this:
augroup filetype_article.txt autocmd! autocmd BufNewFile article.txt 0r ~/Dropbox/vim/Templates/article.txt autocmd BufNewFile article.txt 5$pu=strftime('%Y-%m-%d %H:%M:%S') autocmd BufNewFile article.txt :normal ggA augroup END
With this in place, every time I create a file called article.txt, it becomes populated with the boiler plate, has the current date inserted, and goes into Insert mode at the end of the first line ready for me to start typing. All of this saves valuable time and lets me get on with the job in hand.
I have a few of these templates set up and they always come in very handy. If you're into processing, be sure to take a look at my Processing boiler plate template.