Vim Tips Wiki
Register
No edit summary
 
m (should be 2 choices)
Tag: Visual edit
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=650
 
|id=650
  +
|previous=649
|title=abbreviation that prompts whether to expand it or not
 
  +
|next=651
|created=February 4, 2004 7:13
+
|created=February 4, 2004
 
|complexity=basic
 
|complexity=basic
 
|author=Yakov Lerner
 
|author=Yakov Lerner
 
|version=6.0
 
|version=6.0
 
|rating=5/5
 
|rating=5/5
  +
|category1=Abbreviations
|text=
 
  +
|category2=Map
You can define abbreviation in such a way that it will ask whether to expand it or not. The trick is to define it as insert-mode mapping with special body, not as abbreviation.
 
 
Here is how to define it:
 
 
 
 
function! MyExpand(abbr,expansion)
 
 
let answer=confirm("Expand '".a:abbr."' [y] ", "&Yes\n&No")
 
 
if answer==2
 
 
exec "normal! a".a:abbr
 
 
else
 
 
exec "normal! a".a:expansion
 
 
endif
 
 
endfunction
 
 
 
 
imap ABC <esc>:call AskExpand("ABC","...expansion for ABC ...")<cr>a
 
 
imap XYZ <esc>:call AskExpand("XYZ","...expansion for XYZ ...")<cr>a
 
 
}}
 
}}
 
You can define an abbreviation in such a way that it will ask whether to expand it or not. The trick is to define it as an expression, then have that expression ask for confirmation:
   
  +
<pre>
== Comments ==
 
 
function! s:Ask(abbr,expansion,defprompt)
In your example shouldn't 'AskExpand' read 'MyExpand' ?
 
 
let answer = confirm("Expand '" . a:abbr . "'?", "&Yes\n&No", a:defprompt)
 
  +
" testing against 1 and not 2, I correctly take care of <abort>
mark.thomas--AT--tampagov.net
 
  +
return answer == 1 ? a:expansion : a:abbr
, February 4, 2004 9:26
 
 
endfunction
----
 
It would be great if you could have the default prompt Yes or No for each abbreviation.
 
It could be added as a parameter to the AskExpand
 
eg.
 
imap if0 &lt;esc&gt;:call AskExpand("if0",'&#35;if 0 /* VITTAL_IF_0 */', y)&lt;cr&gt;a
 
 
 
vittal at cisco dot com
 
, May 10, 2006 4:50
 
----
 
Here is the code where u can choose what the default action is ... for each abbreviation ie. expand or not..
 
 
 
 
"defprompt: 2 =&gt; Yes, 1=&gt; NO
 
function! MyAskExpand(abbr,expansion,defprompt)
 
 
if a:defprompt==2
 
let answer=confirm("Expand '".a:abbr."' [y] ", "&amp;Yes\n&amp;No")
 
if answer==2
 
exec "normal! a".a:abbr
 
else
 
exec "normal! a".a:expansion
 
endif
 
else
 
let answer=confirm("Expand '".a:abbr."' [n] ", "&amp;No\n&amp;Yes")
 
if answer==1
 
exec "normal! a".a:abbr
 
else
 
exec "normal! a".a:expansion
 
endif
 
endif
 
endfunction
 
   
  +
iabbrev <expr> for <SID>Ask('for', "for () {\n}", 2)
"imap XYZ &lt;esc&gt;:call myAskExpand("XYZ","...expansion for XYZ ...")&lt;cr&gt;a
 
  +
</pre>
imap abc &lt;esc&gt;:call MyAskExpand("abc",'buginf("\n:KV:%s ",__FUNCTION__ );',2)&lt;cr&gt;a "default is YES
 
imap xyz &lt;esc&gt;:call MyAskExpand("xyz",'/* VITTAL_DEBUG */',1)&lt;cr&gt;a "default is NO.
 
   
  +
==References==
  +
*{{help|:iabbrev}}
  +
*{{help|:map-expression}}
   
 
==Comments==
vittal at cisco dot com
 
, May 10, 2006 5:20
 
----
 
<!-- parsed by vimtips.py in 0.537500 seconds-->
 

Latest revision as of 07:04, 26 December 2019

Tip 650 Printable Monobook Previous Next

created February 4, 2004 · complexity basic · author Yakov Lerner · version 6.0


You can define an abbreviation in such a way that it will ask whether to expand it or not. The trick is to define it as an expression, then have that expression ask for confirmation:

function! s:Ask(abbr,expansion,defprompt)
  let answer = confirm("Expand '" . a:abbr . "'?", "&Yes\n&No", a:defprompt)
  " testing against 1 and not 2, I correctly take care of <abort>
  return answer == 1 ? a:expansion : a:abbr
endfunction

iabbrev <expr> for <SID>Ask('for', "for () {\n}", 2)

References[]

Comments[]