Enter Search Query:

  • 2089
  • Close

HTML tag matching in vim

My Vim knowledge sure comes in handy when I need to edit code from the command line and when it comes to hand coding large HTML, CSS, or PHP files, I’m thankful for the matchit vim tag matching macro. It doesn’t come activated by default (I found that out after re-installing OS X from scratch). Here’s how on a Mac:[step 1] Fire up a terminal window.

[step 2] Check to see if you already have a “.vim” folder in your user home directory.

ls -al ~/

[step 3] If you don’t see a .vim directory, no worries – we’ll just create it. If you do, skip to [step 4]

cd ~
mkdir .vim

[step 4] Start vim, then type the following commands to copy the matchit.vim macro/plugin along with its documentation to the .vim folder we created or already have. It’ll extend the existing behavior of bracket matching via % to various XHTML tags.

:e $VIMRUNTIME/macros/matchit.vim
:saveas ~/.vim/plugin/matchit.vim
:e $VIMRUNTIME/macros/matchit.txt
:saveas ~/.vim/doc/matchit.txt

[step 5] Initialize the plugin’s help documentation and start it automatically every time you use vim by adding it to your .vimrc file. If you don’t have one, just create it. Within vim, type the following:

:helptags ~/.vim/doc

Now you’re ready to jump from <html> to </html> by typing % (of course other HTML/XML tags are supported too). For help just type :help matchit.

December 20, 2010

Related Posts