Vim Tips Wiki
m (Added to Automated_Text_Insertion Category + code reformatted + help links fixed)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=465
 
|id=465
  +
|previous=464
|title=generic xml imap to make an element of any word you type
 
  +
|next=466
|created=April 29, 2003 12:05
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=Bart van Deenen
 
|author=Bart van Deenen
|version=5.7
+
|version=6.0
 
|rating=151/46
 
|rating=151/46
  +
|category1=Automated Text Insertion
|text=
 
  +
|category2=XML
Hi all.
 
 
}}
 
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
We're doing a lot of xml work, in docbook and custom xml files.
 
   
 
If in isert mode I type <code>programlisting,,, </code> the text immediately gets modified to
I'd just like to share one macro I've developed, that I really can't live without.
 
 
<programlisting>
imap ,,, &lt;esc&gt;bdwa&lt;&lt;esc&gt;pa&gt;&lt;cr&gt;&lt;/&lt;esc&gt;pa&gt;&lt;esc&gt;kA
 
 
</programlisting>
   
 
with the cursor in between, still in insert mode.
If in isert mode I type <code>programlisting,,, </code> the text immediately get's modified to
 
&lt;programlisting&gt;
 
&lt;/programlisting&gt;
 
with the cursor in between, still in Insert mode.
 
   
The same happens with any other word i type followed by three commas.
+
The same happens with any other word I type followed by three commas.
&lt;tryit&gt;
+
<tryit>
&lt;/tryit&gt;
+
</tryit>
   
 
==Comments==
It saves me a lot of work, and I'd like to share it. Hope you can use it
 
 
If you change the b to B and the w to W, then the imap will go over the colon in a namespace.
   
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
 
 
----
 
----
  +
If you use <code>:set ai</code> then indenting will be retained.
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
 
 
----
 
----
 
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>
Is it possible to modify this map, so the indentation will be kept. So
 
test,,,&lt;cr&gt;&lt;tab&gt;test2,,,
 
results in
 
&lt;test&gt;
 
&lt;test2&gt;
 
&lt;/test2&gt;
 
&lt;test&gt;
 
   
  +
<pre>
It would also be nice, if it was possible to detect if the line already contains non-whitespace so
 
 
function! Make_element()
this is a bold,,,bold&lt;esc&gt;f&gt; test
 
 
"function to make an xml element at the current cursor position out of the
resulted in
 
 
"contents of the just deleted unnamed register "
this is a &lt;bold&gt;bold&lt;/bold&gt; test
 
 
"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
  +
</pre>
   
fuzz01--AT--spamfilter.dk
 
, April 30, 2003 7:26
 
 
----
 
----
 
It's probably better to use :setlocal iskeyword than :set
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
 
 
----
 
----
 
Just a quick note, instead of setting the "iskeyword" like that, you can use
Hi
 
 
: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.
I improved my imap with a function (my first in vim) (can probably be done in a one-liner :)
 
   
 
References
Put the following in xml.vim in your ftplugin directory, our source it in your .vimrc. The ^] and ^M are entered via &lt;CTRL-V&gt;&lt;esc&gt; and &lt;CTRL-V&gt;&lt;CR&gt;
 
 
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&lt;^[ea&gt;&lt;/^[pa&gt;^[F&lt;i"
 
else "the deleted word was on it's own on the line
 
let --AT--w = "i&lt;^[ea&gt;^M&lt;/^[pa&gt;^[kA" endif
 
endfunction
 
 
"include colon(58) for namespaces in xsl for instance
 
set iskeyword=--AT--,48-57,_,192-255,58
 
 
imap &lt;buffer&gt; ,,, &lt;Esc&gt;bye:call Make_element()&lt;enter&gt;--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|id=:set+=}}
 
*{{help|id=:set+=}}
 
*{{help|id=:set-=}}
 
*{{help|id=:set-=}}
   
arnarb at oddi dot is
 
, May 1, 2003 10:45
 
 
----
 
----
 
This tip is great, but it does not work if the word is only one character long, like "a" in html.
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
 
 
----
 
----
 
I uploaded this as {{script|id=632}} with a correction that handles one-character words.
Hi all
 
   
I uploaded this as {{script|id=632}}.
 
 
Happy vimming!
 
 
Bart
 
 
bart
 
, May 1, 2003 11:11
 
 
----
 
----
 
Try [[VimTip583]] (Vim as XML Editor).
This tip is great, but it does not work if the word is only one character long, like "a" in html. If I type
 
<code>a,,,</code> the result is:
 
&lt;
 
a&gt;&lt;/&gt;
 
 
What is going wrong?
 
 
André, fs111--AT--linuxmail.org, October 6, 2003 6:00
 
: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), November 29, 2003 4:48
 
:P.S. sorry for the late answer, I was on honeymoon :-)
 
----
 
Hi
 
 
If you install [[VimTip583]] (Vim as XML Editor).
 
 
Have fun,
 
Tobi
 
 
 
tobiasreif pinkjuice com
 
, October 13, 2003 3:30
 
----
 
This fixes the case where you have only one (or more) character(s):
 
p,,,
 
 
Results in:
 
&lt;p&gt;
 
&lt;/p&gt;
 
 
inoremap ,,, &lt;esc&gt;diwi&lt;&lt;esc&gt;pa&gt;&lt;cr&gt;&lt;/&lt;esc&gt;pa&gt;&lt;esc&gt;kA
 
   
David Fishburn
 
, December 9, 2004 12:05
 
 
----
 
----
 
This fixes the case where you have only one (or more) character(s):
I've added version 1.1 of {{script|id=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.
 
  +
inoremap ,,, <Esc>diwi<<Esc>pa><CR></<Esc>pa><Esc>kA
   
  +
Then, typing
happy vimming
 
 
p,,,
   
 
results in
Bart
 
  +
<p>
  +
</p>
   
'''Anonymous'''
 
, February 6, 2005 4:48
 
 
----
 
----
<!-- parsed by vimtips.py in 0.633922 seconds-->
 
[[Category:XML]]
 
[[Category:Automated_Text_Insertion]]
 

Latest revision as of 05:32, 13 July 2012

Tip 465 Printable Monobook Previous Next

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

<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>

Comments[]

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

: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


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

Then, typing

p,,,

results in