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

How do I insert a literal tab character in insert mode even when expandtab is enabled?

Answer

<C-v><Tab>

Explanation

When expandtab is set, pressing the Tab key inserts spaces instead of a real tab character. But sometimes you need to embed an actual tab — in a Makefile that requires hard tabs, a TSV data file, or a heredoc with specific whitespace — without changing your global settings. <C-v><Tab> inserts the next character literally, bypassing any option-driven conversions.

How it works

  • <C-v> in insert mode enters literal insert mode: the next keystroke is inserted as-is, without any mapping or option processing
  • <Tab> following it inserts the literal tab character (ASCII 9), regardless of expandtab, softtabstop, or indentexpr

Example

With expandtab set and shiftwidth=4, typing Tab normally produces four spaces:

rule:    (Tab key → four spaces)

Using <C-v><Tab> instead:

rule:	  (actual tab character — required for Makefiles)

Tips

  • The same <C-v> technique works for any key: <C-v><Esc> inserts a literal escape character, <C-v><CR> a carriage return, and <C-v>065 the character with decimal code 65 (A)
  • In command-line mode, <C-v><Tab> also inserts a literal tab into the command rather than triggering completion
  • To check whether a character is really a tab, use ga on it — a tab shows as ^I (9) in the output

Next

How do I run normal mode commands in a script without triggering user-defined key mappings?