New Fun with VIM

Hey everyone! I apologize for my lack of writing lately. Our baby boy was born on August 8th, and I'm still trying to catch up to everything from April :)

However, today I figured out some neat magic with VIM that I just have to share. Part of it is also so I don't forget it in the future! That's how awesome it is.

As you well know, I'm a big fan of Django. And I'm a relatively recent convert to VIM. One thing that I find to be kind of a common thing for me when developing a Django site is adding new blocks in the templates. Typing the "boilerplate" code for these blocks is easy, but it takes time.

Today, while tinkering with VIM, I figured out a way to automate the insertion of the boilerplate code:

nnoremap <Leader>b i{% block  %}{% endblock %}<Esc>16hi

Inserting that in your ~/.vimrc will allow you to type \b in normal mode while in VIM, which will insert the text:

{% block  %}{% endblock %}

...and move your cursor to where you would type in the block's name. Finally, it puts you straight into insert mode so you can immediately type the block name and get to work. Alternatively, if you prefer to have the opening and closing block tags on separate lines, you could use:

nnoremap <Leader>b i{% block  %}<CR>{% endblock %}<Esc><Up>$2hi

This would insert:

{% block  %}
{% endblock %}

...and move your cursor to the same place as the other one, so you could start typing the block name. If you'd like to have access to both, you could change the letter that comes after <Leader> to whatever key you'd like. Pretty fancy stuff, eh?

I love it! I'm sure there are other, perhaps better, ways to accomplish the same thing. I am just excited that I figured it out on my own :)

Enjoy!

Comments

Comments powered by Disqus