0

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
Usiel
  • 671
  • 3
  • 14
  • I think this is not autocompletion, this is still manual completion. For autocompletion, no trigger key is needed. – jdhao Oct 21 '22 at 07:59

1 Answers1

1
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.