I am very used to the shell autocompletion, which uses the <tab>
character. Therefore I tried to build something similar into my .vimrc
. I'm aware of the super popular YCM plugin but let's face this as an exercise in learning.
I have built the following piece of vimscript to: first, allow me to use different types of <c-x>
completions by changing a variable; second, do not fire a completion if the cursor is not after a cword character.
let g:tab_completion_keys = "\<c-x>\<c-p>"
function! TabCompletion1()
let l:char = matchstr(getline('.'), '\%' . col('.') . 'c.')
if l:char =~# '\k'
execute "normal! a" . g:tab_completion_keys . "\<esc>"
else
execute "normal! a\<tab>\<esc>"
endif
endfunction
inoremap <tab> <esc>:call TabCompletion1()<cr>a
The problem is that I cannot leave normal!
in insert-mode. i.e. I cannot enter insert-mode in normal!
and finish the command without exiting the insert-mode. Thanks to that, the code above opens the completion list and always selects the first completion, because I leave the insert mode.
How can I make an insert-mode mapping that leaves me in insert-mode for some time (to select one word from the completion list) and then continues the mapping?
Or, maybe, there is a better way to deal with exe normal!
?
:h :map-<expr>
. Mostimap
maps are easier to write with<expr>
.:h map-<expr>
clarified a lot of things for me. Thanks