Luc Hermitte (talk | contribs) 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= |
+ | |created=2003 |
|complexity=basic |
|complexity=basic |
||
|author=Bart van Deenen |
|author=Bart van Deenen |
||
− | |version= |
+ | |version=6.0 |
|rating=151/46 |
|rating=151/46 |
||
+ | |category1=Automated Text Insertion |
||
− | |text= |
||
+ | |category2=XML |
||
− | Hi all. |
||
⚫ | |||
⚫ | |||
+ | imap ,,, <Esc>bdwa<<Esc>pa><CR></<Esc>pa><Esc>kA |
||
⚫ | |||
⚫ | |||
− | 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 |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | The same happens with any other word |
+ | 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) |
||
⚫ | |||
− | |||
⚫ | |||
− | 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). |
||
− | |||
⚫ | |||
− | 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,,, |
||
⚫ | |||
− | <test> |
||
− | <test2> |
||
− | </test2> |
||
− | <test> |
||
+ | <pre> |
||
− | 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 |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | endif |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
− | 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 :) |
||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | 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 |
||
− | |||
⚫ | |||
− | |||
− | Bart |
||
− | |||
− | bart van deenen |
||
− | , May 1, 2003 10:41 |
||
− | ---- |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
*{{help|id=:set+=}} |
*{{help|id=:set+=}} |
||
*{{help|id=:set-=}} |
*{{help|id=:set-=}} |
||
− | 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 |
||
⚫ | |||
− | |||
− | Happy vimming! |
||
− | |||
− | Bart |
||
− | |||
− | bart |
||
− | , May 1, 2003 11:11 |
||
---- |
---- |
||
⚫ | |||
⚫ | |||
− | <code>a,,,</code> the result is: |
||
− | < |
||
− | a></> |
||
− | |||
− | 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 |
||
− | |||
⚫ | |||
− | |||
− | Have fun, |
||
− | Tobi |
||
− | |||
− | |||
− | tobiasreif pinkjuice com |
||
− | , October 13, 2003 3:30 |
||
− | ---- |
||
⚫ | |||
⚫ | |||
− | |||
− | 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 {{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 |
||
⚫ | |||
⚫ | |||
− | 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
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