8

To work, abbreviations need to be separated by whitespace. E.g., if I have an iab fo FOOBAR then typing whatfo will not trigger the abbreviation. There are some obvious sensible reasons for this.

However, I'd like an abbreviation that could be used as a suffix without me having to trigger the iab then go back and remove the space. Is this possible? E.g., iab fo <backspace>FOOBAR.

  • Does the following code produce what you want? vpaste.net/IfU1j – saginaw Feb 6 '16 at 18:27
  • @saginaw Yeah, brilliant -- post that as an answer and it's yours. – goldilocks Feb 6 '16 at 18:43
8

You could write inside your vimrc: iab fo <BS>FOOBAR

<BS> is the notation to use for the backspace key.

For more information about special key names, see:

:help key-notation
  • 1
    The "simpler way" didn't work out in this case because I think of robustness; the actual suffix is [7:0] and you would have to play around with the number of bs, etc. to customize for that, I think. – goldilocks Feb 6 '16 at 19:11
  • @delicateLatticeworkFever You are right, I didn't think about that. In this particular case, maybe replacing b and e with B and E would be enough. But there may be other cases where it would fail, I don't know. In any case the first version should, hopefully, work most of the time. – saginaw Feb 6 '16 at 19:21
  • @delicateLatticeworkFever Rereading my answer I realize that it's overly complex. Probably something like iab fo <BS>foobar was enough. Sorry for the inconvenience, you should probably tick the answer of @djjcast as the correct answer. At the moment I can't remove my answer, but once I can I will. Again really sorry, I'm not used to abbreviations. – saginaw Feb 7 '16 at 20:51
  • Hey <BS> was just the thing. I swear tried something like that, but I don't know all the control characters, so I might have used <backspace> or something. – goldilocks Feb 7 '16 at 20:53
  • 1
    @delicateLatticeworkFever I understand, no problem. If you want to see all the special key names, you can read them here: :help key-notation – saginaw Feb 7 '16 at 20:56
4

Simply press in insert mode Ctrl]. This will trigger abbreviations to be expanded, without entering a space

  • You misread the question. I want the end result to be something like: X(FOOBAR) not X( FOOBAR. Using Ctrl-] just eliminates the space at the end. I need to be able to use the abbreviation -- which also depends on whitespace at the beginning to fire -- but have it concatenated with the previous word (that's what a "suffix" means). – goldilocks Feb 7 '16 at 11:17
  • Oh, you want a snippet plugin... – Christian Brabandt Feb 7 '16 at 19:22
  • I guess this could be done with something defined ${1}FOOBAR -- that's a good answer. If you want to change this :/ or add another answer I'll tick that. – goldilocks Feb 7 '16 at 19:32
  • since i personally don't know snippet plugins, I can't seriously make this an answer. – Christian Brabandt Feb 7 '16 at 20:27
4

As an alternative, here's a way to define and expand abbreviations that expand anywhere:

let s:anywhere_abbreviations = {
    \ 'fo': 'FOOBAR',
    \ 'ab': 'ABBREVIATION',
\ }

function! s:MaybeExpandAbbreviation(trigger)
    for key in keys(s:anywhere_abbreviations)
        if matchstr(getline('.'), repeat('.', strchars(key)) . '\%' . col('.') . 'c') ==# key
            return repeat("\<BS>", strchars(key)) . s:anywhere_abbreviations[key] . a:trigger
        endif
    endfor
    return "\<C-]>" . a:trigger
endfunction

inoremap <expr> <Space> <SID>MaybeExpandAbbreviation("\<Space>")

Just add the abbreviations that should expand anywhere to the s:anywhere_abbreviations dictionary. The s:MaybeExpandAbbreviation() function searches for and expands abbreviation matches when the trigger key is pressed - <Space> is mapped to trigger the expansion of the "anywhere abbreviations".

With the above s:anywhere_abbreviations dictionary, typing whatfo<Space> will expand to whatFOOBAR.

2

To summarize the two simplest answers that came out of this:

  1. You can use a backspace directly in an abbreviation -- various special characters are documented under :help keycodes. So, e.g.:

    :iab cdt <BS>cedental
    

    Will allow you to type trans cdt and get transcendental.

  2. One I'm particularly ashamed of since I've been using it for years: The common snipMate plugin allows for parameters to go in front, so

     snippet cdt
         ${1}cendental ${2}
    

    Allows for typing cdt, which Tab will expand to cendental and put the cursor in front so you can type trans, then hit Tab again to move to the beginning of the next word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.