Vim Macros for Editing DocBook Documents
DocBook is a vast markup language offering many tags, and most projects use only a subset of the available tags. The bindings above were developed for marking up LJ articles. Your use of DocBook probably will focus on a different set of tags, so you probably need to change the templates and key bindings.
The file tagtmps.vim contains the templates inserted by the <F2> key. Listing 2 shows the template for the article tag.
Listing 2. Article Tag Template
let g:Template_article ="<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?><CR>" \."<!DOCTYPE article SYSTEM \"docbookx.dtd\"><CR><CR>" \."<article><CR><CR>" \."<simplesect><title/><CR>" \."<para><CR>-:-<CR>" \."</para><CR>" \."</simplesect><CR><CR>" \."<simplesect><title></title><CR>" \."<para><CR>" \."</para><CR>" \."</simplesect><CR><CR>" \."</article>"
All templates are stored in global string variables whose names start with Template_ and end with the tag to which the template corresponds. The g: prefix in the variable name makes it a global variable. Long strings can be placed on multiple lines by prefixing the second and subsequent lines with a backslash (\) and using the dot (.) concatenation operator to append the lines together. Each string piece is contained within double quotes. Double-quoted strings understand the usual C escape sequences. You can modify the existing templates or add new ones for other tags by following the described naming sequence. Within a template the sequence -:- is used to specify where you want the cursor to be placed after the template is inserted. The macro automatically removes this string after inserting the macro.
If you want to change the function keys used to execute the macros, modify the following lines in the file mfuncs.vim:
let s:InsertTagTemplateKey = "<F2>" let s:InsertSymbolKey = "<F3>" let s:InsertForeignCharKey = "<F4>" let s:InsertStartTagKey = "<F5>" let s:InsertEndTagKey = "<F6>" let s:TagWordKey = "<F7>" let s:TagRangeKey = "<F7>" let s:ChangeTagKey = "<F8>"
If you want to change the tag-keys, symbol-keys or foreign-char-keys that follow the functions keys or change the tags associated with the keys, change the corresponding lines in maps.vim. For example, to change b so it is associated with the tag <book> rather than the tag <emphasis role="bold">, look for the following line in maps.vim:
call MapTagKey("b", 0, 0, "emphasis", " role=\"bold\"")
and change it to:
call MapTagKey("b", 1, 1, "book", "")
The function MapTagKey is defined in mfuncs.vim. Its prototype is:
function! MapTagKey(key, snewline, enewline, tag, stagx)
Its parameters are explained in Table 5.
Table 5. MapTagKey Parameters
|key||Keystroke(s) to bind the tag key to (for example, <F2>key).|
|snewline||One if the start tag should be placed on a new line, zero otherwise.|
|enewline||One if the end tag should be placed on a new line, zero otherwise.|
|tag||The tag associated with the key.|
|stagx||Extra attributes that should be placed in the start tag.|
The function MapTagKey merely sets up and executes a number of Vim nmap and imap commands to make the appropriate key bindings. Similarily, symbol-keys and foreign-char-keys are mapped by the functions MapSymbolKey and MapForeignCharKey. These functions each take two arguments, the key and the text to insert. For example:
call MapSymbolKey("3", "¾") call MapForeignCharKey("b", "β")
Near the bottom of the file maps.vim are a handful of Vim nmap commands for binding the tag manipluation and movement keys, including delete tag, change tag and move tag. All of these bindings call the functions defined in the file tfuncs.vim.
nmap <S-F8> :call DeleteTag()^M " Delete tag at cursor. nmap <F9> :call CursorLeftByTag()^M " Move left by tags. nmap <F10> :call MoveTagLeft()^M " Move tag left of preceding word. nmap <F11> :call MoveTagRight()^M " Move tag right of following word. nmap <F12> :call CursorRightByTag()^M " Move right by tags. nmap <S-F9> :call TightenTagLeft()^M " Delete whitespace left of tag. nmap <S-F10> :call InsertStringLeftOfTag(" ")^M " Insert space to the left of tag. nmap <S-F11> :call InsertStringRightOfTag(" ")^M " Insert space to the right of tag. nmap <S-F12> :call TightenTagRight()^M " Delete whitespace right of tag.
If you cat this file, these lines are going to look strange, and in some editors, all the lines are going to break right after the closing parentheses in the call. If you look at it with vim, you can see that the closing parentheses are followed by ^M, a carriage return. When you cat the file, this causes part of the line to be erased. Some editors cause a line break here. These mappings work in command mode and the ^M ends the command.
Mitch Frazier is an Associate Editor for Linux Journal.
|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|
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- 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)
- Favorite (and easily brute-forced) pw's
51 min 53 sec ago
- Have you tried Boxen? It's a
6 hours 43 min ago
- seo services in india
11 hours 15 min ago
- For KDE install kio-mtp
11 hours 16 min ago
- Evernote is much more...
13 hours 16 min ago
- Reply to comment | Linux Journal
22 hours 1 min ago
- Dynamic DNS
22 hours 35 min ago
- Reply to comment | Linux Journal
23 hours 34 min ago
- Reply to comment | Linux Journal
1 day 24 min ago
- Not free anymore
1 day 4 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!
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?