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

How do I enable syntax-based completion without plugins using completefunc?

Answer

:set completefunc=syntaxcomplete#Complete

Explanation

If omnifunc is unavailable for a filetype, Vim can still offer meaningful completion by using syntax groups. Setting completefunc to syntaxcomplete#Complete gives you a built-in fallback that understands tokens from the current buffer and syntax context. It is a practical middle ground between keyword-only completion and full LSP stacks.

How it works

  • completefunc defines the function called by user-defined completion (<C-x><C-u> in Insert mode)
  • syntaxcomplete#Complete is shipped with Vim/Neovim and uses syntax highlighting groups to propose candidates
  • This setting is often best applied per filetype (setlocal) when you want predictable behavior in specific languages

Example

In a shell script where LSP is not configured, you can still complete syntax-aware tokens:

:set completefunc=syntaxcomplete#Complete

Then in Insert mode, press:

<C-x><C-u>

Vim opens completion suggestions based on the syntax context rather than only raw keyword scanning.

Tips

  • Prefer :setlocal completefunc=... inside ftplugin files to avoid globally overriding other completion strategies
  • Combine with a tuned completeopt for cleaner popup behavior

Next

How do I open another buffer in a split without changing the alternate-file register?