Taskstasks
Most of the examples and instructions will only work as shown
when carried out in an environment
environment
set up as described in the previous
chapters.
But many of them might work with your settings when you adjust the
various details.
They are meant as illustrations of the respective general idea
and not as perfect recipes.
Creating TagsTurn on XML mode via mapleaderx,
then enter insert mode via i.To create a pair of tags enter the opening tag, you should get the
closing one for free. Now you can enter content, for example text.
bar]]>
If you want the content to be indented start again but this time hit
> again after having closed the opening tag
:
bar
]]>
This way you can quickly create nicely indented XML
documents:
delicious
]]>Creating Documentscreating documentsFlowersLilies...Orchids...]]>
Here is one way to create the above document
:
mapleaderxi<chapter>>up<title>Flowersdown<section>>up<tictrlxctrlp>Liliesdown<para>...escdowno<secctrlxctrlp>>up<tictrlxctrlp>Orchidsdown<parctrlxctrllesc
There are many different ways to create the same document.
For example it would have been faster to create just one section
skeleton and duplicate it.
Now you can foldfolding
away the section you don't work on, with
2Gzc:
Flowers
+--- 3 lines: Lilies----------
Orchids...
]]>
To learn more about folding see
:help
folding
and
:help
fold-commands.
Marking up Textmarking up textWith xmledit
Turn on XML editing mode via
mapleaderx.
To mark up
the literal command name string in
Use cd to go to a different directory.
]]>
select it (eg via
2Gwve),
then type
mapleaderx
in quick succession (eg
\x
or
,x).
At the prompt, enter the name of the element, eg
command, then press enter twice.
You should get something like the following:
Use cd to go to a different directory.
]]>With RecordingAlthough the above method is convenient it is not the best
choice when there are more than one text portions to be marked up
because it can't be repeated or automated easily. But Vim lets you record
actions, the documentation describes this under complex
repeats, see :help
recording.
Let's say you want to mark up the acronyms in
These XSLTs can be used for transforming
DBX to XHTML.]]>
Search for groups of uppercase characters via
/\u\{2,}enter
Enter n until you reach the first acronym you want to
mark up. Then record one of the following macros:
In XML mode (with xmledit)qai<acronym>escrightd%/\UenterP/\u\{2,}enterqWithout xmleditqai<acronym>esc/\Uenteri</acronym>esc/\u\{2,}enterq
Now the cursor should be at the beginning of the next group of
uppercase characters. If you want to mark it up (if it's an acronym
that's not yet marked up) do
@a, otherwise
do n to jump to the next group of uppercase letters.
To repeat the last executed recorded action do
@@.
You should get this:
These XSLTs can be used for transforming
DBX to XHTML.]]>Marking up Tablesmarking up tabular dataRecordingLet's say you have some space-separated strings which you want
to mark up as XHTML table:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
One way is to record some sub-tasks:
get setmapleaderxgg>G:%s/$/space./enterggOctrld<table>>escdd.Gp2Gcellsqci<td>escrightd%tspaceprightd/\Senterq4@crowsqrxI<tr>escrightd%$p+qcells and rowsqa5@c@rqand now comes the fun part3@aYou should get this:
1
2
3
4
5
2
4
6
8
10
3
6
9
12
15
4
8
12
16
20
5
10
15
20
25
]]>SubstitutionUsing substitution commands should be faster than using
recorded macros in XML mode. Hundreds of lines should be marked up
instantly, thousands of lines still take less than a second on my box.
With thousands of lines, undo can take seconds though; test and adjust
your command line with a small number of representative
lines before you run it on larger data sets.The following examples show how semicolon-separated data
which has been exported from a spreadsheet application can be marked
up as XHTML table.If the data is simple you can use simple commands.
Here's some CSV data which has
no empty fieldsno quoted fields (no record separators inside fields, no
quotes inside quotes)and no fields that span mutiple lines1;2;3
2;4;6
3;6;9
4;8;12
5;10;15
Mark up the table cells via
\1<\/td>/g]]>
or
\1')"]]>
so that you get
1
2
3
2
4
6
3
6
9
4
8
12
5
10
15
]]>
Then mark up the table rows via
&<\/tr>/]]>If you want to mark up just a range of lines (eg lines
29 through 33) replace :% with
:29,33.
Alternatively you could chain the two steps:
\1<\/td>/g | s/.\+/
&<\/tr>/]]>
1
2
3
2
4
6
3
6
9
4
8
12
5
10
15
]]>
Transforming CVS data to
XHTML would typically not be done by manipulating a
text editor buffer with commands but by running a little script on a
file, but let's try another example just for the fun of
it.Spreadsheet[spreadsheet screenshot]
Here's some more complex CSV data, as exported from
a spreadsheet app:
1;2;3;4
"this field contains a line
break and a record ; separator";4;6;"line
break"
;8;12;
"quoted ""word""";16;;
;;"record ; separator";
Inserting a record separator at the end of each record which is at the
end of the line simplifies things a bit:
:v/"[^";]\+$/ s/$/;/
To mark up all records in the buffer as table cells do
\1<\/td>/g]]>
Mark up table rows:
\_.\{-}<\/td>\)\{-}\)\(\n\|$\)/
\1<\/tr>\3/g]]>
All this is still based on some assumptions; before you process real
data run tests with some lines representing your data and adjust the
commands.
Cleaning up is not simple either, here's a cheap way to remove the
double quotes inside quotes
:%s/""/"/g
... and the quotes around the fields:
\)"/\1/g]]>
A pretty cryptic way to do both at once: (one line)
)\"('+$nlt+'+)\"(<\/td>)')){($1+$2+$3).gsub(/\"\"/,'\"')}"]]>
This is what you should get:
1
2
3
4
this field contains a line
break and a record ; separator
4
6
line
break
8
12
quoted "word"
16
record ; separator
]]>
Now you could insert dashes into the empty cells
\)\(<\/td>\)/\1-\2/g]]>
mark up the line breaks
$/ s/$/ /]]>
and then wrap the whole thing in an XHTML table.
Table in Browser[browser screenshot]Before you publish web content please check it
against the WCAG. For example
there's a guideline explaining how to make
tables accessible.Generating XMLgenerating XML
From Vim's command line, you can call any tool
which is available on your system's pathsystem path.
So when there's a repetitive task like writing lots of similar tags,
you could ask your favorite programming language to do it for you.
Grab an SVG
SVG
skeleton
skeleton:r /path/to/basictemplate_1_1.svg
insert a circle, and set the view box, so that you get something
like this:
]]>snake skin pattern
then place your cursor where the content should go, eg via
G2k.
Enter the following in Vim's command line,
all in one line.
You can paste it (after having pressed :),
eg via the middle mouse button
(don't forget to delete the duplicate :).
'}"]]>Ruby
Now save the SVG and open it in
some
SVG viewerSVG viewers.
You should see something like this:
Snake PatternA pattern consisting of black dots on white ground.
Here is the SVG itself:
cos_pattern.svg.
Search and Replacesearch and replace
Via search and replace, you can make hundreds of similar changes at
once.
To go from
foo]]>
to
foo]]>
select the block(s) via
then enter the following in Vim's command line:
s/\(\_.\{-}\)<\/span>/\1<\/em>/g]]>
To rename
renaming
all sect1 tags
to
section tags,
you can ask Vim to execute something like
To index some titles in a DBX document I use the
following command (one line):
\)\(.\+\)\(<\/title>\)\n\?\(\s*\)/\1\2\r\4
\2<\/primary>\r\4<\/indexterm>\3\r/gc]]>
This only automates part of the task and is not really general
but unlike XSLT
it leaves CDATA sections intact (simply skip them)
which make editing of code listings easier.
If you get it to work for titles spanning multiple lines send me an
email :).
XPath-based EditingXPath-based editing
Sometimes a task is expressed best using XPathXPath.
Simpler stuff can be done conveniently from Vim's command line,
without having to create an XSLT file.
Rowsrows
Here's an example showing a simple two-step approach to the common
task of marking table rows
table rows
with alternating attributes.
This way the command lines become shorter and easier to write.
Between each screen the command line is listed which describes the
change between the previous and the following screen.
foo
foo
foo
foo
]]>:%!xmlstar ed -i /table/tr -t attr -n class -v odd
Here's what the option names in the above command line stand for:
editinserttypenamevalue
XMLStarlet also supports long options
(eg --insert
as alternative for -i), but the short versions save
typing and space, which is especially useful when calling XMLStarlet
from the shell and not from a script.
foo
foo
foo
foo
]]>:%!xmlstar ed -u "/table/tr[(position() mod 2)=0]/@class" -v even