sponsor Vim development Vim logo Vim Book Ad

basic Tip #650: abbreviation that prompts whether to expand it or not

 tip karma   Rating 5/5, Viewed by 406 

created:   February 4, 2004 7:13      complexity:   basic
author:   Yakov Lerner      as of Vim:   6.0

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

 rate this tip  Life Changing Helpful Unfulfilling 

<<expand existing abbreviation | Edit gnupg-encrypted files. >>

Additional Notes

[email protected], February 4, 2004 9:26
In your example shouldn't 'AskExpand' read 'MyExpand' ?
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo