如何重新Mapcoc.nvim自动完成键?

okxuctiv  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(164)

我试图改变我的coc.nvim自动完成键,并发现堆栈溢出中的this问题,但回答这个问题的家伙并没有解释好如何自定义它,因为你想要的,所以我会解释它,以帮助NeoVim用户正在为此绞尽脑汁,因为我是.

mtb9vblg

mtb9vblg1#

简短回答

如果您想绑定Tab键以进行自动补全,请将其粘贴到您的.vimrc或init.vim

inoremap <silent><expr> <tab> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"
inoremap <silent><expr> <cr> "\<c-g>u\<CR>"

详细答案

所以,你必须做2插入模式重新Map,在这种情况下,我将我的完成重新Map到Tab键。

inoremap <silent><expr> <tab> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"

观察结果:如果要将其他键绑定到自动完成:

inoremap <silent><expr> [the key that you want to autocomplete] pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"

现在,CoC也会用Tab键自动完成,但Enter也是自动完成的,我想绑定Enter,而不是自动完成键。
在VimScript中,Enter表示为<cr>

inoremap <silent><expr> <cr> "\<c-g>u\<cr>"

观察结果:

inoremap <silent><expr> [this is the current autocompletion key] "\<c-g>u\[this is the bind that I am giving to the key]"
nlejzf6q

nlejzf6q2#

示例vim配置为COC提供了一些非常有用的提示。
但具体到你来说,这是相关的:

" Use tab for trigger completion with characters ahead and navigate.
" NOTE: There's always complete item selected by default, you may want to enable
" no select by `"suggest.noselect": true` in your configuration file.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

相关问题