vimtricks.wiki Concise Vim tricks, one at a time.

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

  1. Ensure filetype detection is enabled: :filetype plugin on
  2. In insert mode, press <C-x><C-o>
  3. Vim invokes the omnifunc set for the current filetype
  4. 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

Next

How do I always access my last yanked text regardless of deletes?