Tip: #465 - Generic xml imap to make an element of any word you type
Created: April 29, 2003 12:05 Complexity: basic Author: Bart van Deenen Version: 5.7 Karma: 151/46 Imported from: Tip#465
Hi all.
We're doing a lot of xml work, in docbook and custom xml files.
I'd just like to share one macro I've developed, that I really can't live without.
imap ,,, <esc>bdwa<<esc>pa><cr></<esc>pa><esc>kA
If in isert mode I type programlisting,,,
the text immediately get's modified to
<programlisting>
</programlisting>
with the cursor in between, still in Insert mode.
The same happens with any other word i type followed by three commas.
<tryit>
</tryit>
It saves me a lot of work, and I'd like to share it. Hope you can use it
Bart van Deenen. (bart@vandeenensupport.com)
Comments
please share any other mappings you like! That one is quite nice!
gmann , April 29, 2003 18:46
Chris Lear had a small modification, which allows the same imap to be used with namespaced elements (such as used all the time in xsl).
just change the b in B and the w in W, and the imap will go over the colon in the namespace.
bart--AT--vandeenensupport.com , April 30, 2003 3:05
Is it possible to modify this map, so the indentation will be kept. So
test,,,<cr><tab>test2,,,
results in
<test>
<test2> </test2>
<test>
It would also be nice, if it was possible to detect if the line already contains non-whitespace so
this is a bold,,,bold<esc>f> test
resulted in
this is a <bold>bold</bold> test
fuzz01--AT--spamfilter.dk , April 30, 2003 7:26
Your question about indenting is easily fulfilled by :set ai which sets auto indenting.
The other one is really intriguing, and I'll have a go at it, because that would indeed be helpfull
Bart van Deenen
bart--AT--vandeenensupport.com , April 30, 2003 12:33
Hi
I improved my imap with a function (my first in vim) (can probably be done in a one-liner :)
Put the following in xml.vim in your ftplugin directory, our source it in your .vimrc. The ^] and ^M are entered via <CTRL-V><esc> and <CTRL-V><CR>
function! Make_element()
"function to make an xml element at the current cursor position out of the "contents of the just deleted unnamed register " "Bart van Deenen
if match(getline('.'),'^\s*'.--AT--".'\s*$') == -1 "the deleted word was not alone on the line let --AT--w = "i<^[ea></^[pa>^[F<i" else "the deleted word was on it's own on the line let --AT--w = "i<^[ea>^M</^[pa>^[kA" endif
endfunction
"include colon(58) for namespaces in xsl for instance set iskeyword=--AT--,48-57,_,192-255,58
imap <buffer> ,,, <Esc>bye:call Make_element()<enter>--AT--w
It seems to do the job with me.
Thanks for the question, I learned something from finding the answer
Bart van Deenen
bart ot vandeenensupport point com , May 1, 2003 8:31
hi
it's probably better to use :setlocal iskeyword than :set
Bart
bart van deenen , May 1, 2003 10:41
Just a quick note, instead of setting the "iskeyword" like that, you can use
- set iskeyword+=:
to add the colon to the list of characters/ranges, this way you won't break anything else if it depends on specific settings of iskeyword.
References
- help set+=
- help set<CTRL-D>
arnarb at oddi dot is
, May 1, 2003 10:45
Nice.. that four minute difference between my comment and the one before that one was about the time it took me to write it :o) Hence.. I didn't see it until after I posted.
arnarb at oddi dot is , May 1, 2003 10:47
Hi all
I uploaded this as [/scripts/script.php?script_id=632 vimscript #632].
Happy vimming!
Bart
bart , May 1, 2003 11:11
This tip is great, but it does not work if the word is only one character long, like "a" in html. If I type a,,, the result is: < a></>
What is going wrong?
Andr�
fs111--AT--linuxmail.org , October 6, 2003 6:00
Hi
If you install VimTip583 (Vim as XML Editor).
Have fun, Tobi
tobiasreif pinkjuice com
, October 13, 2003 3:30
Andr� wrote:
This tip is great, but it does not work if the word is only one character long, like "a" in html. If I type a,,, the result is: < a></>
It has to do with a part of the script where I use bye to go to the beginning of the word (a in your case), and yank the word into a buffer. Unfortunately the 'b' command skips over the start of the 'a' word, so you end up with garbage.
I don't have a fix yet, but if I find one, I'll post it. Somebody else?
Bart (the author of the tip)
P.S. sorry for the late answer, I was on honeymoon :-)
Anonymous , November 29, 2003 4:48
This fixes the case where you have only one (or more) character(s):
p,,,
Results in:
<p> </p>
inoremap ,,, <esc>diwi<<esc>pa><cr></<esc>pa><esc>kA
David Fishburn , December 9, 2004 12:05
I've added version 1.1 of [/scripts/script.php?script_id=632 vimscript#632] which correctly handles single character xml elements. I'm ill in bed today, that helps in finding the time for these kinds of fun things.
happy vimming
Bart
Anonymous , February 6, 2005 4:48