Usiel
Usiel

Reputation: 719

Neovim - Shortcut for native autocomplete Ctrl + n

Hello, I want to add a shortcut in neovim to take advantage of its native autocomplete which is activated with Ctrl + n, when typing in insert mode I want to press tab and autocomplete. The following codes work for me, especially the lua code. But there's a problem:

When I use this shortcut I lose the tab function, I'm looking for a way to make this shortcut only work when there is a word before the cursor; otherwise the tab should work normally. I appreciate the help in advance.

Lua

map('i', '<Tab>', '<c-n>', {noremap = true})

Vimscript

imap <Tab> <c-o><c-n>

Note: This question addresses native nvim 'Ctrl + n' autocompletion and not autocomplete plugins. Thank you.

Upvotes: 1

Views: 3949

Answers (1)

Archar Gelod
Archar Gelod

Reputation: 21

inoremap <expr><Tab> CheckBackspace() ? "\<Tab>" : "\<C-n>"

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

I took the CheckBackspace() function from coc.nvim plugin's example config.

Upvotes: 2

Related Questions