Wednesday 20 January 2016

Quick and Dirty: Vim highlighting and tag matching for HTML and Perl's Template Toolkit

If you're a Vim connoisseur you're probably using vim-perl for getting all the Perl goodies you can cram into Vim. On the other hand if, like me, you're building and trashing virtual machines left, right and centre to develop different widgets you don't have the patience to achieve a perfect Vim environment on each box. This is the quick and dirty solution which gets you 80% of the solution in a matter of seconds.
For the most part Vim just works. For example, on Ubuntu if you

apt-get install vim

then it comes with syntax highlighting for Perl scripts (ending in .pl), Perl modules (ending in .pm) and HTML files (ending in .html). It also comes with brace matching for the Perl code, but no <html> tag matching </html>. This is a real pain if you're trying to make sense of a large HTML file in Vim.

HTML tag matching


1. Find plugin/matchit.vim and copy it into your personal vim plugin directory  ~/.vim/plugin (creating it if it's not already there)

mkdir -p ~/.vim/plugin
cp `locate plugin/matchit.vim` ~/.vim/plugin

2. Add this line to your Vim config file ~/.vimrc

echo filetype plugin on >> .vimrc

Then voilĂ ! With Vim you can now jump between matching tags like <html> and </html> using the % key.

Template Toolkit syntax highlighting and tag matching

Of course if you're working on a Perl Template Toolkit we're back to square one because you have HTML'ish files called something like:

foo.html.tt

At this point you just need to add the following line to your ~/.vimrc file

echo au BufRead,BufNewFile *.tt setfiletype html >> ~/.vimrc

and once again you have HTML highlighting and tag matching just like an HTML file (although the TT delimiters [% %] are still ignored by Vim).

Enjoy!

2 comments:

  1. If you use bcvi then your Vim customisations only need to live on your laptop/workstation.

    http://sshmenu.sourceforge.net/articles/bcvi/

    ReplyDelete
  2. better yet for Template Toolkit, use the vim-perl syntax files:

    https://github.com/vim-perl/vim-perl/blob/master/syntax/

    There is even a syntax file for type "tt2html" which is html with template toolkit support. Best of both worlds.

    ReplyDelete