Today when my 30daychallenge almost finishes (tomorrow is the last day) I feel that I could not make it without reusing previous posts for new posts. Check out how to automate it!


The problem

As you may or may not find - most of my posts are currently made with a ViM editor.

It can be a bit painful sometimes, especially when it goes with day-to-day blog-posting. So to be able to deliver content I was cheating a little - to be more precise - I used a Copy-and-Paste method. That means I was making a copy of the previous post and replaced it with current content.

A fun fact is that this post is also written like that. That's a bit silly, so let's change it, shall we?

Vim Templates

There is a plugin called vim-templates by Adrian Perez that gives us the opportunity to make a life of a blogger and a programmer a bit easier.

You can find the project on GitHub repo - Vim-Templates.

How to install

The most comfortable way will be by using Vundle Vim Plugin Manager. That's the one I'm using, and that's why I'll cover how to do it this way.

Add Plugin to your .vimrc file where you usually place your plugins:

Plugin 'aperezdc/vim-template'

And now install it with :

vim +PluginInstall +qall

That's it. You should have your plugin installed.

You should now get to new shell env to get your plugin running.

How to use it?

Start a new file in vim, and if your extension is in templates directory, then you will get automatically generated content from that template.

Of course, you may not want to use automatically generated content by this plugin, then remove auto-cmd within .vimrc:

let g:templates_no_autocmd = 1

Then to use this plugin all you need to do is use the vim command:

:Template *.<here-goes-template-extension>

i.e. for blog markdown file, I would use:

:Template *.md

Or for Python file:

:Template *.py

BE AWARE of using :Template

Making a vim-command :Template *.py in an already filled file, will append content from template to your vim-buffer at the top.

Where are the templates?

Template files are located usually within directory:

ls ~/.vim/bundle/vim-template/templates/

Out of the box you have :

ls -l ~/.vim/bundle/vim-template/templates/ | sort | uniq -c | wc -l

so about 50 file-templates.

Can I extend them?

Yes, you can extend them! Tweak them to make what you want them to do.

There are some unique characters in templates that make the whole process of automation even a bit further.

I.e. the python template uses something like this:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
# Copyright © %YEAR% %USER% <%MAIL%>
# Distributed under terms of the %LICENSE% license.


Where you find special characters like:

  • %YEAR% - a current year.

  • %USER% - a name from your computer user. Change it within .vimrc with : let g:username = 'yourusername'

  • %MAIL% - email is a combination of your name on the computer and a host-name. This can be change within .vimrc with : let g:email = ''

  • %LICENSE% - which by default creates a "MIT" sign. You can change this within .vimrc with : let g:license = 'yourslicensename'

  • %HERE% - will give nothing, but ensure that vim-cursor will get placed in the place where this sign is.

For more knowledge

You can check documentation with vim using :

:help templates.txt

Also, check out in browser documentation of this plugin.

I will surely check it whenever I will need to change something with the template.



That's it :) Comment, share or don't :)

If you have any suggestions what I should blog about in the next articles - please give me a hint :)

See you tomorrow! Cheers!


comments powered by Disqus