2

I successfully installed CoC in my Vim to have some code-suggestions/completions while coding in Rust.

It works fine, but I want make the suggestion-box only show up when I press a key-shortcut (E.g Cmd + x)

2
4

According to coc readme you can trigger completion manually with:

" Use <c-space> to trigger completion.
inoremap <silent><expr> <c-space> coc#refresh()

NOTE that not every terminal supports <c-space>...

I don't know for sure how to map cmd in vim... Probably D or something:

" Use <CMD-x> to trigger completion.
inoremap <silent><expr> <D-x> coc#refresh()

Example with <C-k> that should work with most terminals:

" Use <C-k> to trigger completion.
inoremap <silent><expr> <C-k> coc#refresh()

To turn off autocompletion for coc -- as stated in coc help, you have to add configuration to coc json file:

  1. run :CocConfig
  2. in the opened coc-settings.json file add:
{
  "suggest.autoTrigger": "none"
}

For reference, see :h CocConfig and :h coc-configuration (search for autoTrigger).

9
  • The part to disable autoTrigger works perfectly! However manually opening the pMenu doesn't work yet. The default (c + space) just types a space and 'c' but opens nothing. The remap for D-x doesn't work. Maybe Cmd-key is a problem for the terminal Vim. How would I map Shift + e for example? Maybe that's easier.
    – O'Niel
    Mar 30 '20 at 10:46
  • That is because of your terminal doesn't support this mapping. Try to change it to C-k for example. Shift + e is not very good as you it would trigger coc-refresh whenever you enter E letter (and will not be able to write a simple word with E -- Error, for example)
    – Maxim Kim
    Mar 30 '20 at 11:06
  • Tried adding inoremap <silent><expr> <C-k> coc#refresh() to the bottom of vimrc, restarted Vim. Pressed Ctrl + k. Did not work.
    – O'Niel
    Mar 30 '20 at 15:40
  • what is the output of :verbose imap <C-k>?
    – Maxim Kim
    Mar 30 '20 at 16:58
  • and you do press it in insert mode, right? just to dblcheck
    – Maxim Kim
    Mar 30 '20 at 17:00
0

I added the following lines to my .vimrc for this:

" Disable Coc suggestions by default, use manual trigger.
autocmd BufEnter * let b:coc_suggest_disable = 1

" Use <c-space> to trigger completion.
if has('nvim')
  inoremap <silent><expr> <c-space> coc#refresh()
else
  inoremap <silent><expr> <c-@> coc#refresh()
endif

The first block disables automatic coc-suggestions each time you enter the buffer, and the second configures the ctrl + spacebar key combination as a trigger to display suggestions manually.

Of course, you can only disable suggestions for certain file types. For example:

autocmd FileType rust let b:coc_suggest_disable = 1
3
  • Welcome to Vi and Vim! Instead of autocmd FileType, use ftplugins (e.g., put let b:coc_… in ~/.vim/after/ftplugin/rust.vim (or the neovim equivalent).
    – D. Ben Knoble
    Oct 4 at 23:06
  • Thanks for your comment! But I'd like to disable suggestions for multiple file types at the same time and don't want to edit a bunch of files. Also, for portability, I prefer to manage all settings from .vimrc only. What are the advantages of your approach, could you explain please?
    – awerebea
    Oct 6 at 5:47
  • Faster load times, for one, since the filetype mechanism already runs ftplugins using an autocommand. Plus, since your vimrc can live at ~/.vim/vimrc, you can keep everything in one directory (not the same as a single file, but still fairly portable). And actually you’ll probably find that if/as your settings grow more complicated for filetypes, you’ll appreciate the organization of having dedicated places for that customization.
    – D. Ben Knoble
    Oct 6 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.