How do I trigger language-aware autocompletion in Vim?
Answer
<C-x><C-o>
Explanation
The <C-x><C-o> keystroke triggers omni completion, Vim's built-in language-aware completion system. It uses filetype-specific logic to suggest completions based on the structure of the programming language you're editing.
How it works
- Ensure filetype detection is enabled:
:filetype plugin on - In insert mode, press
<C-x><C-o> - Vim invokes the
omnifuncset for the current filetype - A popup menu shows language-aware suggestions
What it knows about
Vim ships with omni completion for several languages:
- HTML/CSS: Tag names, attributes, CSS properties and values
- JavaScript: Object properties, DOM methods
- Python: Module members, function signatures (with
pythoncomplete) - SQL: Table names, column names, SQL keywords
- XML: Tag completion based on DTD
- PHP: Functions, class methods
Example
In an HTML file:
<div cl
Pressing <C-x><C-o> after cl suggests class, clear, etc.
Setting up for other languages
" Set omnifunc manually for a filetype
autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS
autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS
Tips
- Check the current omnifunc with
:set omnifunc? - For more advanced completion, pair with LSP clients (vim-lsp, coc.nvim, nvim-lspconfig)
<C-n>/<C-p>cycle through results in the popup- Omni completion is the foundation that many plugins build upon