How do I insert a literal control character or special key in insert mode?
Answer
<C-v>{char}
Explanation
When you need to insert a literal tab character despite expandtab being set, or embed a control character like ^M (carriage return) into your text, <C-v> in insert mode is the tool. It tells Vim to insert the very next keystroke literally, bypassing any mappings or special behavior.
How it works
<C-v><Tab>— inserts a real tab character, even whenexpandtabconverts tabs to spaces<C-v><Esc>— inserts a literal escape character (^[) into the buffer<C-v><CR>— inserts a literal carriage return (^M)<C-v>065— inserts the character with decimal ASCII code 65 (the letterA)
The key point is that <C-v> suppresses Vim's interpretation of the next key. This is different from <C-v>u{hex} which inserts Unicode by code point.
Example
Suppose you have expandtab set but need a real tab in a Makefile:
# Before (cursor in insert mode in a Makefile)
build:
echo "spaces here"
# Press <C-v><Tab> to insert a literal tab
build:
echo "real tab here"
Tips
- On Windows or in terminals that intercept
<C-v>, use<C-q>instead — it works identically - Use
:set listto visually distinguish literal tabs (^I) from spaces <C-v>followed by three digits inserts the character by its decimal byte value- In the command line,
<C-v>works the same way for inserting literal characters into search patterns or Ex commands