Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #650 - Abbreviation that prompts whether to expand it or not

Created: February 4, 2004 7:13 Complexity: basic Author: Yakov Lerner Version: 6.0 Karma: 5/5 Imported from: Tip#650

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! AskExpand(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

Comments

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.

"defprompt: 2 => Yes, 1=> NO 
function! MyAskExpand(abbr,expansion,defprompt) 
  if a:defprompt==2 
    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 
  else 
    let answer=confirm("Expand '".a:abbr."' [n] ", "&No\n&Yes") 
    if answer==1 
      exec "normal! a".a:abbr 
    else 
      exec "normal! a".a:expansion 
    endif 
  endif 
endfunction 

"imap XYZ <esc>:call myAskExpand("XYZ","...expansion for XYZ ...")<cr>a 
imap abc <esc>:call MyAskExpand("abc",'buginf("\n:KV:%s ",__FUNCTION__ );',2)<cr>a "default is YES 
imap xyz <esc>:call MyAskExpand("xyz",'/* VITTAL_DEBUG */',1)<cr>a "default is NO. 

vittal at cisco dot com , May 10, 2006 5:20


Advertisement