Vim Macros for Editing DocBook Documents
Recently, while helping Linux Journal convert its editorial process to use DocBook/XML for articles, I had occasion to convert some old Vim macros for use with the new process. The original macros were key maps or abbreviations for inserting Quark tags and special characters. The new editorial process involves marking or tagging a document in DocBook/XML. From there, a stylesheet is applied to convert the document either to Quark for publication in the print magazine or to HTML for publication on the Web site.
DocBook exists in two basic forms, an SGML version and a newer XML version. DocBook is a markup language that looks similar to HTML. It uses tags with attributes and ampersand sequences for specifying special characters and symbols. Listing 1 contains a short DocBook/XML article.
Listing 1. Sample DocBook/XML Article
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE article SYSTEM "docbookx.dtd">
<article>
<!-- Article information. -->
<articleinfo>
<!-- Article title and abstract. -->
<title>This is an Uninteresting Sample article</title>
<abstract>
<para>This article isn't about anything interesting.</para>
</abstract>
<!-- Author name and bio. -->
<author>
<firstname>John</firstname> <surname>Doe</surname>
<authorblurb>
<para>The author is not a very interesting person.</para>
</authorblurb>
</author>
</articleinfo>
<!-- Body of article. -->
<simplesect>
<title>This is the first of thankfully only
one uninteresting section.</title>
<para>True to form this is not very interesting either.</para>
</simplesect>
</article>
As you can see, the article structure is similar to HTML, except the tag names are different. The DOCTYPE line refers to the DTD (Document Type Definition) used to validate the file. To write useful DocBook, you need the DTD and a program to validate your document so you can determine if it contains any DocBook or XML errors. See the Resources at the end of the article for sites where you can get the DTD and programs for validating your documents.
Vim primarily is a work-a-like replacement for the original vi editor that came with many UNIX systems. Vim also contains many enhancements, including a quite capable macro/scripting language and a GUI version. Most Linux distributions should include Vim. Vim is a moded text editor; that is, keystrokes have different meanings depending on whether you're entering text or manipulating it.
The set of Vim macros used for our project are contained in the following files:
tagtmps.vim: contains tag templates. Tag templates are starting and ending tags and some predefined content that can be inserted into the file you're editing.
tfuncs.vim: contains functions for manipluating tags. Functions are available for inserting, deleting, moving and changing tags.
mfuncs.vim: contains functions that assist in setting up Vim key mappings.
maps.vim: uses the mapping functions defined above to define key mappings for accessing the tag functions defined above.
To use these files start vi and type:
:so tagtmps.vim :so tfuncs.vim :so mfuncs.vim :so maps.vim
Normally, however, you would place these commands into a single file and source only it when you enter vim. For example, place the above files in a sub-directory named vim in your home directory. Then, put the following lines in a file named editdb.vim:
so ~/vim/tagtmps.vim " Tag templates. so ~/vim/tfuncs.vim " Tag functions. so ~/vim/mfuncs.vim " Map functions. so ~/vim/maps.vim " Key mappings.
Now, start vi and type the following to load all the files with one command :so ~/editdb.vim. Another option is to source these files in your .vimrc file so they are loaded whenever you start vim. Your .vimrc file is located in your home directory.
Once these files have been read and processed by vim, the macros are bound to the keyboard. The macros provide the following capabilities:
Inserting a tag template.
Inserting a start tag.
Inserting an end tag.
Tagging a word by placing it between a start tag and an end tag.
Tagging a range of lines with a start tag and an end tag.
Changing a tag.
Inserting special symbols such as the copyright symbol.
Inserting special characters such as accented characters.
Deleting a tag under the cursor.
Moving the cursor to the left (right) of the previous (next) tag.
Moving a tag to the left (right) of the previous (next) word.
Deleting whitespace to the left or right of a tag.
Inserting whitespace to the left or right of a tag.
Mitch Frazier is an Associate Editor for Linux Journal.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Nice article, thanks for the
6 hours 11 min ago - I once had a better way I
11 hours 57 min ago - Not only you I too assumed
12 hours 14 min ago - another very interesting
14 hours 7 min ago - Reply to comment | Linux Journal
16 hours 1 min ago - Reply to comment | Linux Journal
22 hours 55 min ago - Reply to comment | Linux Journal
23 hours 11 min ago - Favorite (and easily brute-forced) pw's
1 day 1 hour ago - Have you tried Boxen? It's a
1 day 6 hours ago - seo services in india
1 day 11 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Re: Vim Macros for Editing DocBook Documents
i copied the 4 macro files in the article, but i get the following errors when doing, ":so maps.vim"...
i xb
it sort of just gets worse from there...
Re: Vim Macros for Editing DocBook Documents
It works now when downoading the files from the LJ server.
But there is a bug that inserts closing tags...
For example:
<F2><n> produces:
<itemizedlist>
<listitem><para></para></listitem>
</itemizedlist></para></listitem></itemizedlist>
<F2><p> produces:
<para>
</para></para>
If this could be fixed it would really make editing a lot easier so plz look over the code and post an update :-)
Morten Damsgaard-Madsen
Denmark
Where is the linuxjournal ser
Where is the linuxjournal server that you downloaded the files from?
ftp://www.linuxjournal.com/pu
ftp://ftp.ssc.com/pub/lj/listings/Web/7737.tar.gz
Re: Vim Macros for Editing DocBook Documents
As usual the socurce of errors is right in front of the computer ;-).
The script works as expected - I just forgot that I had the xml.vim script installed which interacts with these macros in funny ways...
Nice work!
/Morten
Re: Vim Macros for Editing DocBook Documents
I use Vim 6.2 and I get the same error.
If you observe the error shown, it says that a variable is undefined.
If you look into mfuncs.vim, you will see that "etags" is defined
in a conditional statement and used anyway subsequently.
Therefore, in the cases where the conditional statement is missed,
"etags" is undefined subsequently.
The author should have a look at the code one again and post here an update.
Re: Vim Macros for Editing DocBook Documents
Same goes for me: the version of vim I use is 6.3. Maybe the macros of this article are meant for an older version ?