2

I'm trying to add an abbreviation to some strange language syntax I don't use often and always forget, for example, on all python buffers, i'd want

iab "else if" elif

Those are strange use cases for an abbreviation (most of them convert a phrase to a word), alas I can't think of a better feature to implement this in. But i cannot figure out how to start the aliasing with spaces.

2 Answers 2

3

As Vivian said, this is not possible. Instead, you'll have to analyse what is before the cursor on a if abbreviation. At that moment, you could replace it with the relevant suppression, if need be.

function! s:merge_elif() abort
  let context = getline('.')[:col('.')-1]
  let match   = matchstr(context, 'else\s*if$')

  " minus 2 because "if" is not considered already inserted
  return !empty(match) ? repeat("\<bs>", strlen(match)-2) .'elif' : ''
endfunction

" I expect the abbreviation to not be global, but specialised for a specific
" filetype. Hence the `<buffer>` annotation: the abbreviation is expected to
" be defined in a filetype plugin.
inoreab <buffer> <expr> if <sid>merge_elif()
1
  • thanks for working a solution for this!
    – gcb
    Commented yesterday
1

It is not possible to have abbreviation containing space :-/

See :help abbreviations

And in particular:

Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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