You can accomplish what you're trying to do by using an expression (introduced using <C-r>=
, or using <expr>
when defining the abbreviation) and then matching a single "a" word at the end of the text that precedes the word being abbreviated.
When you find that "a" word, then you can expand it to a <C-w>
, that will delete that word, followed by the abbreviation you desire.
You can use this function to implement all the "à" (named crase) abbreviations in Portuguese:
function Crase()
" Get the list of words until this point,
" including the one being abbreviated.
let words = split(getline('.')[0:col('.')-2])
" By default, return the word itself.
let replacement = words[-1]
" Check if a word 'a' precedes it.
if len(words) >=# 2 && words[-2] ==? 'a'
" Handle capitalization.
if words[-2] ==# 'A' || words[-1] =~# '^A'
let replacement = substitute(replacement, '^\ca', 'À', '')
else
let replacement = substitute(replacement, '^\ca', 'à', '')
endif
" Remove the 'a' word and
" contract the 'crase'.
return "\<C-w>".replacement
endif
" Otherwise, no change.
return replacement
endfunction
It looks at the sentence so far, then takes the two last words in it (when using iab <expr>
, you'll get the word being abbreviated as well.) Then it checks whether the second last word was an "a" or an "A". If that's the case, it will then remove that word (using the "\<C-w>"
at the beginning of the expansion), then return the word being abbreviated, just replacing the first character (which must be an "a", lower or uppercase!) with an "à" or an "À".
The function does proper capitalization, so "A aquele" will be expanded to "Àquele", as expected.
Use the function as follows:
inoreabbrev <expr> aquele Crase()
inoreabbrev <expr> Aquele Crase()
inoreabbrev <expr> AQUELE Crase()
inoreabbrev <expr> aquela Crase()
inoreabbrev <expr> Aquela Crase()
inoreabbrev <expr> AQUELA Crase()
inoreabbrev <expr> aquilo Crase()
inoreabbrev <expr> Aquilo Crase()
inoreabbrev <expr> AQUILO Crase()
You can even use it for "a a", turn it into "à", with:
inoreabbrev <expr> a Crase()
inoreabbrev <expr> A Crase()