How to remap CoC VIM autocomplete key? - vim

I am trying to remap the autocomplete key from the "Enter" key to "TAB" because I keep autocompleting when I intend to go to the next line. The code below is the default option for coc, and I think this is where I should be able to remap the key.
" make <CR> auto-select the first completion item and notify coc.nvim to
" format on enter, <cr> could be remapped by other vim plugin
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<c-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
I thought that changing the <cr> in the beginning to <TAB> would work. However, although it does allow me to autocomplete using TAB, it creates weird auto indentations in some cases. For example:
//normal behavior
someFunction(){
//cursor here appropriately indented
}
//behavior after I made the changes mentioned above
someFunction(){
//cursor here}
I assume I just fundamentally don't understand something about coc or remapping keys in VIM.
Why can't I simply change that <cr> to <TAB>? How can I go about remapping the autocomplete key from "Enter" to "TAB"?

I don't understand vimscript too well, but I managed to get something working by trial and error.
Default Setting:
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
Autocompleting on Tab:
"This expression seems to be responsible for coc formatting on enter
inoremap <silent><expr> <cr> "\C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
"I this just says autocomplete with the first option if pop up menu is open.
"If it is not open, just do a regular tab.
inoremap <silent><expr> <TAB> pumvisible() ? coc#select_confirm() : "\<C-g>u\<TAB>"

Replace the following line from the example coc config
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
with this:
inoremap <silent><expr> <TAB> coc#pum#visible() ? coc#pum#confirm() : "\<C-g>u\<TAB>"
This was based on #christofuy's answer but updated after this line changed.

Related

VIM) Overriding arrow keys on popup menu of 'You complete me' only works on 'down'

Env: VIM 8.2.2815, PluginManager-Vundle
I installed YouCompleteMe Plugin for VIM. I use arrow keys a lot for moving between lines and characters. But YouCompleteMe popup hijack my arrow command so I decided to change key mapping.
First, I found that there is a default mapping for completion select like below. I added these lines to ~/.vimrc
let g:ycm_key_list_select_completion = ['<TAB>', '<Up>']
let g:ycm_key_list_select_previous_completion = ['<S-TAB>', '<Down>']
So I removed <Up>, <Down>. But it doesn't work.
According to this link : https://github.com/ycm-core/YouCompleteMe/issues/2336, it might be a problem of VIM. So I added lines below to ~/.vimrc
inoremap <expr> <up> pumvisible() ? '<c-y><up>' : '<up>'
inoremap <expr> <down> pumvisible() ? '<c-y><down>' : '<down>'
It worked. But only works on 'down' arrow, not on 'up'.
I have searched github YCM issues and google, but couldn't find any solution.
Is there any way to get it fixed?
edit)
I found out that arrow key has weird behavior. When set showcmd is on, hitting both <Up> and <Down> shows ~#k on the right below of my vim. It can be a problem of mobaXterm or ssh, but not sure...

How to pass autocomplete dialog with <Enter> key while cword equals autocomplete value in vim?

When I press <Enter> after typing do I can't pass to a new line. I can press <Enter> key 100 times and will see this autocomplete suggestion until I don't cancel it. The simplest way to omit this is to press <Space> and then <Enter> for a new line.
My .vimrc - https://github.com/vadimshvetsov/dotfiles/blob/master/vim/.vimrc
It looks like my configuration for prabirshrestha/asyncomplete.vim was wrong.
Finally end up with adding this entry and now <Enter> works:
inoremap <expr> <CR> pumvisible() ? asyncomplete#close_popup() . "\<CR>" : "\<CR>"

Vim's pop-up menu autocompletion not working correctly

I'm using deoplete plugin for autocompletion and it works as expected but with a small trivial problem.
Using tab key to select completion goes to the last item instead of first and vice versa for shift-tab which got fixed by following 2 lines but whenever I open a file in new vim instance the following lines don't work unless I source my .vimrc.
inoremap <expr> <Tab> pumvisible() ? "\<Down>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<Up>" : "\<S-Tab>"
If I already have vim open and have sourced my .vimrc and then tried opening any file in the same vim instance then it works without needing to source my .vimrc again.

Vim: Remove mapping created by vim-plugin

I use the Vimwiki-Plugin a lot, but remapping <Backspace> and <CR> is just anoying. If I use :nmap, the mapping is shown:
n <CR> #<Plug>VimwikiFollowLink
n <Backspace> #<Plug>VimwikiGoBackLink
If I try to remove tha mapping with :nunmap <CR> I get an "E31: No such mapping" error. Is there a way to give <CR> and <Backspace> its's normal behaviour back?
if you want to just disable it, you could give
:nunmap <buffer> <CR>
because it is a buffer-local mapping.
or
:h vimwiki_<cr>
you found:
<CR> Follow/create wiki link (create target wiki page if
needed).
Maps to |:VimwikiFollowLink|.
To remap: >
:nmap <Leader>wf <Plug>VimwikiFollowLink
if you remap that to another key, e.g. the keys in example <leader>wf, the <cr> would be reset to normal.
because in its code, vimwiki has:
if !hasmapto('<Plug>VimwikiFollowLink')
nmap <silent><buffer> <CR> <Plug>VimwikiFollowLink
endif
same for the <BS>

Ctrl+Space for omni and keyword completion in vim

I Want to use Ctrl+Space for omni-completion (and keyword completion if there is no omni-completion) in vim. I've tried this which I found somewhere on the web:
inoremap <expr> <c-space> pumvisible() ? "\<C-n>" : "\<C-x>\<C-o>\<C-n>\<C-p>\<C-r>=pumvisible() ? \"\\<Down>\" : \"\\<CR>\""
however it's not working. Anyone who is using Ctrl+Space for this too who can show me the correct way (which works) to do it?
Worth noting is that it needs to work in the terminal version of vim NOT gvim.
Try this:
inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
\ "\<lt>C-n>" :
\ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
\ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
\ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
imap <C-#> <C-Space>
The above way is "kind of" working, but it's so unreadable that almost nobody could say what it actually does. The solution above is not good.
Short Answer - Use this:
function! Auto_complete_string()
if pumvisible()
return "\<C-n>"
else
return "\<C-x>\<C-o>\<C-r>=Auto_complete_opened()\<CR>"
end
endfunction
function! Auto_complete_opened()
if pumvisible()
return "\<Down>"
end
return ""
endfunction
inoremap <expr> <Nul> Auto_complete_string()
inoremap <expr> <C-Space> Auto_complete_string()
This answer also respects that there are two possible values (depending on terminal/gvim usage) for Ctrl+Space: <C-Space> and <Nul>.
I use a similar approach as the first one in jedi-vim, but more customizable.
Long Answer - What the above does:
The whole escaping of the above answer is so confusing, that I've split the above answer into a readable format:
function! Auto_complete_string()
if pumvisible()
return "\<C-n>"
else
return "\<C-x>\<C-o>\<C-r>=Auto_complete_opened()\<CR>"
end
endfunction
function! Auto_complete_opened()
if pumvisible()
return "\<c-n>\<c-p>\<c-n>"
else
return "\<bs>\<C-n>"
end
endfunction
inoremap <expr> <Nul> Auto_complete_string()
This clearly shows what it is doing. There's some weird stuff happening in Auto_complete_opened. It's not just doing completion, it's doing two additional things after trying to complete:
When trying to use the omnicompletion, it somehow does a <C-n><C-p><C-n>, which could IMHO just be abbreviated to <C-n>.
In case completion is unsuccessful, it uses a backspace and does a completion again, not <C-o><C-x> but <C-n>, which just doesn't make a lot of sense.
I'm not saying that this is not what some user might want, but it's probably not what most users want! My short answer takes that in credit and gives you a simple way to edit it. You can now just easily change things if you want to (for example <Down> to <C-n>, if you want the first entry to be written from the beginning on).
For iterm2 and vim these lines works for me, I got from jedi-vim
" Next three lines are to enable C-Space to autocomplete, omnicomplete
inoremap <C-Space> <C-x><C-o>
imap <buffer> <Nul> <C-Space>
smap <buffer> <Nul> <C-Space>

Resources