Vim Tips Wiki
Register
(Standard category names + minor manual clean.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 16: Line 16:
 
imap ,,, <Esc>bdwa<<Esc>pa><CR></<Esc>pa><Esc>kA
 
imap ,,, <Esc>bdwa<<Esc>pa><CR></<Esc>pa><Esc>kA
   
If in isert mode I type <tt>programlisting,,, </tt> the text immediately gets modified to
+
If in isert mode I type <code>programlisting,,, </code> the text immediately gets modified to
 
<programlisting>
 
<programlisting>
 
</programlisting>
 
</programlisting>
Line 30: Line 30:
   
 
----
 
----
If you use <tt>:set ai</tt> then indenting will be retained.
+
If you use <code>:set ai</code> then indenting will be retained.
   
 
----
 
----

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