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.


Posted

in

by

Comments

2 responses to “HTML tag matching in vim”

  1. Mesagoma Avatar
    Mesagoma

    Under Windows 7 I did the following:

    Based on http://vim.wikia.com/wiki/Open_vimrc_file, I determined my “$HOME” folder as well as the name of my vimrc file (which yielded C:UsersMyWindowsUsername_vimrc); I did the same for the “~” folder which turned out to be identical to “$HOME”.

    Based on another source, I learned that for Windows the “plugin” and “doc” folders mentioned in your instructions should be located under “~vimfiles” (i.e., C:UsersMyWindowsUsernamevimfiles). After creating these folders I adapted your “saveas” commands as follows:

    :saveas ~vimfilespluginmatchit.vim
    :saveas ~vimfilesdocmatchit.txt

    I then created my _vimrc file via e: $HOME_vimrc and placed the following two lines into it:

    source $VIM/_vimrc
    :helptags ~/vimfiles/doc

    …however, based on http://www.vim.org/scripts/script.php?script_id=39 and trying :help matchit-install in Vim (after reading about it in matchit.txt), it appears that it is not necessary to put the :helptags line into your vimrc file; executing it once appears to suffice such that :help matchit or :help matchit-activate produce the matchit help file at the proper position.

    1. David Vielmetter Avatar

      Thank you for clarifying.

Skip to content