DocBook support for Vim 7

Vim 7 has a good DocBook support... that can be improved!

vi is a great editor

I must confess I'm a long time vi user. I started using vi ages ago with an HP-UX 9.05 system (I think, or was it an older system?). And, since then, vi has always been with me. It's one of those "invisible" pieces of software you can't life without (another one is bash, of course).

Vim 7 and DocBook

As you probably know I'm doing some heavy-duty experiments with DocBook at the moment, so I'd love my current vi implementation to do some autocompletion for me.

And the fact is that you need to do some extra configuration to have it working on a default Vim 7 installation. Here're some tips.

Enable filetype plugins for Vim

Vim 7 has added some nice ways to detect filetypes. You should enable them. Add the following lines to your .gvimrc or .vimrc file:

filetype plugin on
filetype indent on

This will enable your filetype detection stuff.

Now get some DocBook sugar for Vim

Go fetch docbook44.vim and store it under your $HOME/.vim/autoload/docbk/docbook44.vim You must create that directory on your $HOME if it does not exist.

Note that the page at vim.org asks you to store it under $HOME/.vim/autoload/xml/docbook44.vim, that's wrong with Vim 7: you need to store it under the "docbk" subdirectory. That's because Vim 7 detects DocBook XML files as being of type "docbk", and not "xml".

Finally arrange your Vim 7 default installation

As I said previously, Vim 7 detects DocBook xml files as being of type "docbk" (you can try it yourself: open a DocBook xml file and type ":set filetype"). So what we need to do now is to enable some fancy XML stuff for "docbk" filetypes.

Doing this is very easy:

  1. Locate your VIM installation directory (try out a ":version")
  2. Go to the "ftplugin" directory. (ftplugin = File Type PLUGIN, as I learned).
  3. Copy your "xml.vim" to "docbk.vim" (note that there is not a docbk.vim file there. That's the problem!)

Now you're all set. Vim 7 should be DocBook aware now.

Testing it out

Create a DocBook document. Open an XML element (such as a para). Write some text. Now type "</" and press Ctrl+X Ctrl+N to have Vim 7 suggest the element name for you automagically! This is cool!

But there's even more. You can ask for suggestions while typing an element name, so after typing a "<" just type Ctrl+X Ctrl+P and a popup with all tags appears.

I'm just learning how to use this new feature of Vim (I'm doing a :help i _ CTRL-X _ CTRL-O).

If you know of any other tips for Vim 7 and DocBook then please let me know.

Cheers, Antonio

blog comments powered by Disqus