Generic xml imap to make an element of any word you type
From Vim Tips Wiki
created 2003 · complexity basic · author Bart van Deenen · version 6.0
We're doing a lot of xml work, in docbook and custom xml files. Here is one mapping that I use all the time.
imap ,,, <Esc>bdwa<<Esc>pa><CR></<Esc>pa><Esc>kA
If in isert mode I type
programlisting,,, the text immediately gets modified to
with the cursor in between, still in insert mode.
The same happens with any other word I type followed by three commas.
If you change the b to B and the w to W, then the imap will go over the colon in a namespace.
If you use
:set ai then indenting will be retained.
I improved my imap with a function. Put the following in xml.vim in your ftplugin directory, or 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*'.@".'\s*$') == -1 "the deleted word was not alone on the line let @w = "i<^[ea></^[pa>^[F<i" else "the deleted word was on it's own on the line let @w = "i<^[ea>^M</^[pa>^[kA" endif endfunction "include colon(58) for namespaces in xsl for instance set iskeyword=@,48-57,_,192-255,58 imap <buffer> ,,, <Esc>bye:call Make_element()<CR>@w
It's probably better to use :setlocal iskeyword than :set
Just a quick note, instead of setting the "iskeyword" like that, you can use
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.
This tip is great, but it does not work if the word is only one character long, like "a" in html.
I uploaded this as script#632 with a correction that handles one-character words.
Try VimTip583 (Vim as XML Editor).
This fixes the case where you have only one (or more) character(s):
inoremap ,,, <Esc>diwi<<Esc>pa><CR></<Esc>pa><Esc>kA