I use C-Y
in insert mode relatively often to copy characters on the line above. This however does not work directly when the suggestion popup is open. At the same time, given that C-Y
can also be used to close the popup, pressing it two times accomplishes the same task. However, I would like not to have to do that.
Ideally, I would simply check whether pumvisible()
is true, and if so inoremap <C-Y>
to <C-Y><C-Y>
. However, this solution does not work when using the YouCompleteMe plugin.
As per comments in the plugin's code, "When selecting a candidate and closing the completion menu with the key, the menu will automatically be reopened because of the TextChangedI event". Instead, YCM remaps (by default <C-Y>
) a key to a local function which disables this behavior:
" When selecting a candidate and closing the completion menu with the <C-y>
" key, the menu will automatically be reopened because of the TextChangedI
" event. We define a command to prevent that.
exe 'inoremap <expr>' . key . ' <SID>StopCompletion( "\' . key . '" )'
function! s:StopCompletion( key )
call s:StopPoller( s:pollers.completion )
call s:ClearSignatureHelp()
if pumvisible()
let s:completion_stopped = 1
return "\<C-y>"
endif
return a:key
endfunction
So this function would be inaccessible to me unless through these remapping, since StopCompilation
is local here (IIUC).
Then I thought I could remap this function to another combination, and remap <C-Y>
like this:
let g:ycm_key_list_stop_completion = ['<C-b>']
inoremap <expr><C-Y> pumvisible() ? "<C-b><C-Y>" : "<C-Y>"
This seems to work, but also inserts the (escape) character <C-b>
into the text. I have tried many things at this point, including making a separate function, but I can't seem to figure out a way to do this that has no side-effects. Could you please help me?