DEV Community

Cover image for Neovim auto select suggestions using coc-vim
Cason Adams
Cason Adams

Posted on

Neovim auto select suggestions using coc-vim

I do a lot of pair programming using vi and tmux so I try to accommodate those I pair with by adding features into vi that are similar to those in other IDEs.

I have been pairing with a colleague over the past few days and I noticed that every time a suggestion dialog would appear we ended up on the next line down and partially typed word above. Come to find out that Intellij has this nice option to autocomplete suggestions by auto selecting them as you type and auto filling if return is pressed.

Alt Text

Here is what I did to get this to work.

Requirements

I am going to just copy a segment from the coc-vim README and plug it into the ~/.config/nvim/init.vim file.

" --------------------------------------------------------
" SETTINGS START

set completeopt=longest,menuone

" SETTINGS END
" --------------------------------------------------------

" --------------------------------------------------------
" COC-VIM TAB SETTINGS START

" Use tab for trigger completion with characters ahead and navigate.
" 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>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

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

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

" Use <cr> to confirm completion, `<C-g>u` means break undo chain at current
" position. Coc only does snippet and additional edit on confirm.
" <cr> could be remapped by other vim plugin, try `:verbose imap <CR>`.
if exists('*complete_info')
  inoremap <expr> <cr> complete_info()["selected"] != "-1" ? "\<C-y>" : "\<C-g>u\<CR>"
else
  inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
endif

" COC-VIM TAB SETTINGS END
" --------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

Then add the following setting to ~/.config/nvim/coc-settings.json file. In vi execute :CocConfig

"suggest.noselect": false
Enter fullscreen mode Exit fullscreen mode

Latest comments (3)

Collapse
 
zyfyy profile image
zyfyy
let g:UltiSnipsExpandTrigger="<Nop>"
Enter fullscreen mode Exit fullscreen mode

and every thing works fine .~*

Collapse
 
inegm profile image
Islam NEGM

Thanks for sharing (Y)

Collapse
 
ayoubelmhamdi profile image
ayoubelmhamdi

it's work : thanks so much ❤️