You could put one of the following codes at the end of your vimrc
file to make the - key toggle the vowel under the cursor between the
accented and unaccented form:
The intuitive solution
function! ToggleAccent()
" Vowels
let withAccent = [ "á", "é", "í", "ó", "ú", "Á", "É", "Í", "Ó", "Ú" ]
let withNoAccent = [ "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" ]
" A better way of getting the character under the cursor
" From: https://stackoverflow.com/a/23323958/1121933
let character = matchstr( getline('.'), '\%' . col('.') . 'c.' )
" If it's a vowel without an acute accent over it, 'position' will contain
" the index of the matching element in the 'withNoAccent' list or -1 otherwise.
let position = match( withNoAccent , character )
if position != -1
" Replace it with an accented vowel
execute ":normal! r" . withAccent[position]
else
" Check if it's a vowel with an acute accent over it
let position = match( withAccent , character )
if position != -1
" Replace it with a vowel with no accent
execute ":normal! r" . withNoAccent[position]
endif
endif
" Do nothing if it isn't a vowel
endfunction
" Map the '-' key
nnoremap <silent> - :call ToggleAccent()<CR>
The clever solution
See Luc Hermitte's answer, but here's what's going on:
s
Delete the character under the cursor, save it into register "
and start insert mode. See :help s
<c-r>
Insert the content of the register that follows (=
in that case). See :help i_CTRL-R
=
This is the expression register, i.e., you're prompted to enter a expression. Once the expression is evaluated (after you hit Enter, represented by <cr>
), the result (the string returned by tr()
) is saved in that register. See :help i_CTRL-R_=
tr({src},{fromstr},{tostr})
Return a copy of the {src}
string with all characters which appear in {fromstr}
replaced by the character in that position in the {tostr}
string (see :help tr()
.) That's what ToggleAccent()
above tries to do. In that case {src}
is @"
, that's the way Vim gets the content of register "
. See Registers as Variables at Learn Vimscript the Hard Way
<cr>
Hit Enter and evaluate the expression
<esc>
Go back to normal mode
Unfortunately, the .
command doesn't repeat custom mappings, but
you can read Ingo Karkat's answer to get around it.