Vim Completion

Insert mode completion and intelligent text expansion.

Generic Completion

Trigger completion
Ctrl-n                                   " Next match (generic)
Ctrl-p                                   " Previous match (generic)
Completion menu
Ctrl-n                                   " Select next in menu
Ctrl-p                                   " Select previous in menu
Ctrl-y                                   " Accept selection
Ctrl-e                                   " Cancel completion

Specific Completion Types

Line completion
Ctrl-x Ctrl-l                            " Complete whole line
Keyword completion
Ctrl-x Ctrl-n                            " Keywords in current file (forward)
Ctrl-x Ctrl-p                            " Keywords in current file (backward)
Ctrl-x Ctrl-i                            " Keywords in included files
Dictionary completion
Ctrl-x Ctrl-k                            " Dictionary words
:set dictionary=/usr/share/dict/words   " Set dictionary
Thesaurus
Ctrl-x Ctrl-t                            " Thesaurus
:set thesaurus=/path/to/thesaurus        " Set thesaurus
File path completion
Ctrl-x Ctrl-f                            " File paths
Tag completion
Ctrl-x Ctrl-]                            " Tag completion
Omni completion
Ctrl-x Ctrl-o                            " Omni completion (language aware)
User completion
Ctrl-x Ctrl-u                            " User defined completion
Vim command completion
Ctrl-x Ctrl-v                            " Vim command line
Spelling
Ctrl-x s                                 " Spelling suggestions
Ctrl-x Ctrl-s                            " Same

Completion Settings

Menu behavior
:set completeopt=menu,menuone,noselect   " Common settings
:set completeopt=menu,preview            " With preview window
:set completeopt+=longest                " Insert longest common text
Complete sources
:set complete=.,w,b,u,t                  " Default sources
" .  = current buffer
" w  = buffers in other windows
" b  = loaded buffers
" u  = unloaded buffers
" t  = tags
" i  = included files
" kspell = spelling (when spell on)

Popup Menu Keys

While menu is visible
Ctrl-n                                   " Next item
Ctrl-p                                   " Previous item
Ctrl-y                                   " Accept selected
Ctrl-e                                   " Close menu, restore text
<CR>                                     " Accept (if completeopt has noinsert)
<Tab>                                    " Often mapped to next
<S-Tab>                                  " Often mapped to previous
Scroll info
Ctrl-f                                   " Scroll popup forward
Ctrl-b                                   " Scroll popup backward

Common Mappings

Tab completion
inoremap <Tab> <C-n>
inoremap <S-Tab> <C-p>
Smart tab
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
Enter to accept
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>"

Path Completion Tips

Complete from current file directory
Ctrl-x Ctrl-f                            " After typing ./
With autochdir
:set autochdir                           " Change to file's directory

Omni Completion Setup

For specific filetypes
" Python
:set omnifunc=python3complete#Complete

" JavaScript
:set omnifunc=javascriptcomplete#CompleteJS

" HTML
:set omnifunc=htmlcomplete#CompleteTags

" CSS
:set omnifunc=csscomplete#CompleteCSS
Check current
:echo &omnifunc

Snippet-like Completion

Abbreviations
:iab teh the                             " Fix typo
:iab @@ email@example.com                " Email shortcut
:iab sig -- <CR>Best regards<CR>Name     " Signature

LSP Completion

With nvim-lspconfig
" Completion handled by nvim-cmp or similar
" Ctrl-space often mapped to trigger