Wednesday, March 23, 2011

Editing XML and HTML in Vim

I just discovered the Vim xmledit plugin.

With features like tag-completion, auto-wrapping and unwrapping, quick navigation, etc., it has, in a matter of minutes, measurably decreased my level of frustration while editing markup in Vim.

Installation

The quickest way to install the plugin is by downloading the latest .vba from the plugin site, and run the following commands:

$ vim xmledit.vba
:so %

You also need to edit your .vimrc and add the following:

filetype plugin on

This installs the plugin into your .vim/ftplugin directory, and enables it for .xml files. To enable it for other file types, create a link to the file with the new extension name in the same directory. (Copying the file also works.)

$ cd ~/.vim/ftplugin
$ ln -s xml.vim html.vim
$ ln -s xml.vim xhtml.vim

Usage


The plugin supports the various Vim modes in interesting ways.

In insert mode, when you finish a tag (with the > character), it will be autocompleted and the cursor placed in-between the tags.

If you immediately type another >, it will close the tag on its own line, and place the cursor on a new line right in between.

The % key jumps between the start and end of a tag.

The \% combination jumps between opening and closing tags. (Note that backslash is the default key-prefix for scripts and plugins to use. You can change this prefix with the mapleader setting.)

If you select text (for example with v), and type \x, it will prompt you to wrap the text with a custom tag.

Typing in \d unwraps surrounding tags from the cursor.

Learning More


Type :help xml-plugin for help and more information.

Yay for another awesome Vim plugin. You can see my entire Vim profile in my Evil Tomato GitHub Repository.

3 comments:

  1. Also take a look at sparkup: https://github.com/rstacruz/sparkup

    ReplyDelete
  2. This sounds really interesting! thanks. However ... I've installed the plugin and created a .vimrc, but when I try
    $ cd ~/.vim/ftplugin
    my system just says
    No such file or directory :-{
    Where should I be looking?

    ReplyDelete
  3. @Niels You need to create the directory if it doesn't exist.

    $ mkdir ~/.vim/ftplugin

    ReplyDelete