Vim Macros for Editing DocBook Documents

Macros for key mappings, tags and entities that can be used with DocBook/XML and other similar markup languages.
Macro Key Bindings

The macros are bound to function keys (F-Keys) followed by zero, one or two modifier characters. Function keys are specified as <FN>, where N is the function key number. Shifted function keys are specified as <S-FN>. The key mappings are detailed belwo in Tables 1-4.

Table 1. Key Mappings

Key SequenceMapping
<F2>tag-keysInsert tag template. Inserts a template for the tag corresponding to the specified tag-keys in the document. See Table 2 for a list of defined tag-keys.
<F3>symbol-keysInsert symbol character. Inserts the symbol character corresponding to the specified symbol-keys in the document. Most often, this inserts the DocBook/XML entity reference for the symbol. See Table 3 for a list of defined symbol-keys.
<F4>foreign-char-keysInsert foreign character. Inserts the foreign character corresponding to the specified foreign-char-keys into the document. See Table 4 for a list of defined foreign-char-keys.
<F5>tag-keysInsert opening tag. Inserts the opening tag corresponding to the specified tag-keys.
<F6>tag-keysInsert closing tag. Inserts the closing tag corresponding to the specified tag-keys.
<F7>tag-keysTag word. If the cursor is on a word, this places tags corresponding to the specified tag-keys around the word.
<F7>tag-keysTag range (command mode). When a range of lines is specified, this places tags corresponding to the specified tag-keys around the lines. Note: this is done in command mode; type a colon followed by the key sequence.
<F8>tag-keysChange tag. If the cursor is on a tag, this changes the tag to the tag that corresponds to the specified tag-keys.
<F9>Move cursor to the left by tags. If the cursor is on a tag, this moves to the beginning of the tag (to the <). If the cursor is not on a tag, this moves to the beginning of the next tag to the left of the cursor.
<F10>Move tag left of preceding word. If the cursor is on a tag, this moves the tag to the left of the preceding word. In other words, if the cursor is on a start tag, it expands the amount of tagged content to include the word to the left of the tag. If the cursor is on an end tag, it removes the word to the left of the tag from the tagged contents.
<F11>Move tag right of following word. If the cursor is on a tag, this moves the tag to the right of the following word. In other words, if the cursor is on a start tag, it removes the word to the right of the tag from the tagged contents. If the cursor is on an end tag it, expands the amount of tagged content to include the word to the right of the tag.
<F12>Move cursor to the right by tags. If the cursor is on a tag, this moves to the end of the tag (to the >). If the cursor is not on a tag, this moves to end of the next tag to the right of the cursor.
<S-F8>Delete tag. If the cursor is on a tag, this deletes the tag.
<S-F9>Delete whitespace to the left of the tag. If the cursor is on a tag, this deletes any whitespace to the left of the tag.
<S-F10>Insert a space to the left of the tag. If the cursor is on a tag, this inserts a single space to the left of the tag.
<S-F11>Insert a space to the right of the tag. If the cursor is on a tag, this inserts a single space to the right of the tag.
<S-F12>Delete whitespace to the right of the tag. If the cursor is on a tag, this deletes any whitespace to the right of the tag.

The following keys are used in combination with the function keys <F2>, <F5>, <F6>, <F7> and <F8> . The function key determines the action; the keys that follow determine the tag.

Table 2. Tag Keys

KeysCorresponding Tag
a<article>
b<emphasis role="bold">
c<command>
d<entry>
e<email>
f<function>
h<title>
i<emphasis>
jd<remark role="web-pub-date">
ji<remark role="author-image">
jl<remark role="layout-info">
jn<remark role="article-number">
jo<remark role="output-file">
jp<remark role="pull-quote">
js<remark role="article-section">
jt<remark role="teaser">
jw<remark role="article-series">
k<command role="what-to-type">
l<listitem>
m<mediaobject>
n<itemizedlist>
o<orderedlist>
p<para>
q<quote>
r<row>
s<sidebar>
t<table>
u<ulink>
xa<author>
xb<blockquote>
xc<![CDATA[
xf<firstname>
xi<articleinfo>
xl<surname>
xm<othername role="middle">
xn<!--
xo<screen>
xp<programlisting>
xq<question>
xr<answer>
xs<simplesect>

For example, <F2> inserts a tag template, so <F2>p inserts <para></para>, places the cursor between the two tags and enters insert mode. Many of the templates consist of more than the start and end tag. For example, the template for a table inserts empty heading and body rows.

The following keys are used in combination with function key <F3>.

Table 3. Symbol Keys

KeysValue InsertedDescription
3¾Three-fourths fraction
5Left single quote
6Right single quote
7Left double quote
8Right double quote
,<Less than sign
.>Greater than sign
<<Less than sign
>>Greater than sign
aæae ligature
c©Copyright symbol
d°Degrees sign
f¼One-fourth fraction
h½One-half fraction
nEn-dash
mEm-dash.
r®Registered symbol
t×Times sign
_ (underscore)-date-Current date

For example, <F3>3 inserts &frac34; into the text.

The following keys are used in combination with function key <F4>.

Table 4. Foreign Character Keys

KeysValue InsertedDescription
bβGreek beta
mμGreek mu
nñn with tilde
'a (singlequote-a)áa with acute accent
'cçc with cedilla
'eée with acute accent
'i íi with acute accent
'oóo with acute accent
'uúu with acute accent
`a (backtick-a)àa with grave accen
`e èe with grave accent
`iìi with grave accent
`oòo with grave accent
`uùu with grave accent
"a (doublequote-a)äa with diaeresis
"eëe with diaeresis
"iïi with diaeresis
"oöo with diaeresis
"uüu with diaeresis
^aâa with circumflex
^eêe with circumflex
^iîi with circumflex
^oôo with circumflex
^uûu with circumflex

For example, <F4>'a inserts á (an a with an acute accent) into the text.

______________________

Mitch Frazier is an Associate Editor for Linux Journal.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Vim Macros for Editing DocBook Documents

Anonymous's picture

i copied the 4 macro files in the article, but i get the following errors when doing, ":so maps.vim"...

i xb

i xc [CDATA[-:-]]?-:-3xi
Error detected while processing function MapTagKey:
line 57:
E121: Undefined variable: etag
E15: Invalid expression: "imap " . s:InsertEndTagKey . a:key . eicmd . etag
line 58:
E121: Undefined variable: etag
E15: Invalid expression: "nmap " . s:InsertEndTagKey . a:key . encmd . etag
line 61:
E121: Undefined variable: etag
E116: Invalid arguments for function escape(etag, """)
E15: Invalid expression: escape(etag, """)

it sort of just gets worse from there...

Re: Vim Macros for Editing DocBook Documents

Anonymous's picture

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

Anonymous's picture

Where is the linuxjournal server that you downloaded the files from?

ftp://www.linuxjournal.com/pu

Pablo's picture

Re: Vim Macros for Editing DocBook Documents

Anonymous's picture

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

Anonymous's picture

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

Anonymous's picture

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 ?

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix