2

我正在尝试将自动完成键从“Enter”键重新映射到“TAB”,因为当我打算转到下一行时,我一直在自动完成。下面的代码是 coc 的默认选项,我认为这是我应该能够重新映射密钥的地方。

" 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>"

我认为将开头的 <cr> 更改为 <TAB> 会起作用。然而,虽然它允许我使用 TAB 自动完成,但在某些情况下它会创建奇怪的自动缩进。例如:

//normal behavior
someFunction(){
    //cursor here appropriately indented
}


//behavior after I made the changes mentioned above
someFunction(){
//cursor here}

我想我只是根本不了解有关 coc 或在 VIM 中重新映射键的内容。

为什么我不能简单地将 <cr> 更改为 <TAB>?如何将自动完成键从“Enter”重新映射到“TAB”?

4

1 回答 1

1

我不太了解vimscript,但我设法通过反复试验得到了一些工作。

默认设置:

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

在选项卡上自动完成:

"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>"
于 2020-12-22T21:15:53.193 回答