我正在尝试将自动完成键从“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>"
Run Code Online (Sandbox Code Playgroud)
我认为将开头的 <cr> 更改为 <TAB> 就可以了。然而,虽然它确实允许我使用 TAB 自动完成,但在某些情况下它会产生奇怪的自动缩进。例如:
//normal behavior
someFunction(){
//cursor here appropriately indented
}
//behavior after I made the changes mentioned above
someFunction(){
//cursor here}
Run Code Online (Sandbox Code Playgroud)
我想我只是根本不理解 coc 或 VIM 中重新映射键的一些东西。
为什么我不能简单地将 <cr> 更改为 <TAB>?如何将自动完成键从“Enter”重新映射到“TAB”?
我不太了解 vimscript,但我通过反复试验成功地得到了一些东西。
默认设置:
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
Run Code Online (Sandbox Code Playgroud)
选项卡上的自动补全:
"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>"
Run Code Online (Sandbox Code Playgroud)
注意:使用coc#pum#confirm()而不是作品,并且是git 中的 coc 官方文档coc#select_confirm()使用的内容(于 2023 年 5 月审核)
替换示例 coc 配置中的以下行
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
Run Code Online (Sandbox Code Playgroud)
有了这个:
inoremap <silent><expr> <TAB> coc#pum#visible() ? coc#pum#confirm() : "\<C-g>u\<TAB>"
Run Code Online (Sandbox Code Playgroud)
这是基于 @christofuy 的答案,但在该行更改后进行了更新。