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

How do I make keyword completion preserve the capitalization style I started typing?

Answer

:set infercase

Explanation

The infercase option makes Vim's keyword completion smart about capitalization: it adapts each match to reflect the casing of the characters you've already typed. Without it, completions are always inserted in the exact case they appear in the source file, which often doesn't match the style you're writing. infercase requires ignorecase to be set — the two options work together.

How it works

  • :set ignorecase — makes completion case-insensitive (matches function, Function, FUNCTION when you type func)
  • :set infercase — adjusts the inserted completion to match the case pattern you typed
  • If you type all lowercase → match is lowercased
  • If you type with an uppercase first letter → match is Title-cased
  • If you type all uppercase → match is uppercased

Example

Given a file that contains the word calculateTotal:

Without infercase:
  Type "CalcT" → inserts "calculateTotal"  (source case)

With :set infercase:
  Type "CalcT" → inserts "CalculateTotal"  (your case)
  Type "calct" → inserts "calculatetotal"  (your case)
  Type "CALCT" → inserts "CALCULATETOTAL"  (your case)

Tips

  • Add both to your vimrc: :set ignorecase infercase
  • infercase only affects insert-mode keyword completion (<C-n>, <C-p>); it does not change how search patterns match
  • Pairs well with :set smartcase for searches: case-insensitive by default, but case-sensitive when you include an uppercase letter in the pattern
  • If you often work in codebases with mixed naming conventions (camelCase, PascalCase, UPPER_CASE), infercase reduces the need to manually fix capitalization after completing a word

Next

How do I configure Vim's :grep command to use ripgrep for faster project-wide searching?