Skip to content

Instantly share code, notes, and snippets.

@jsatk
Created September 12, 2018 19:04
Show Gist options
  • Save jsatk/a8994388ea9ecd9d1c273137072fa77a to your computer and use it in GitHub Desktop.
Save jsatk/a8994388ea9ecd9d1c273137072fa77a to your computer and use it in GitHub Desktop.
Tab or complete when in insert mode in vim
" Auto-Completion --------------------------------------------------------- {{{
" Use TAB to complete when typing words, else inserts TABs as usual.
function! Tab_Or_Complete(move_to_next_word) abort
" If auto-complete menu is not open and we are in the middle of typing a
" word OR if auto-complete menu is already open the `tab` cycles through
" suggested completions.
if pumvisible() || (col('.')>1 && strpart( getline('.'), col('.')-2, 3 ) =~ '^\w')
return a:move_to_next_word
else
" If we aren't typing a word and we press `tab` simply do the normal `tab`
" action.
return "\<Tab>"
endif
endfunction
inoremap <expr> <Tab> Tab_Or_Complete("\<C-N>")
inoremap <expr> <S-Tab> Tab_Or_Complete("\<C-P>")
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment