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

How do I insert a literal control character or special key into the command line?

Answer

<C-v> (command-line mode)

Explanation

In command-line mode (after : or /), pressing <C-v> followed by any key inserts that key literally — bypassing all key notation, mappings, and special interpretations. This is essential for embedding actual tab characters, carriage returns, or other non-printable bytes directly in search patterns and substitute commands.

How it works

  • <C-v>{key} — inserts the next key as its raw character
  • <C-v><Tab> — inserts a real tab character (visible as ^I)
  • <C-v><C-m> — inserts a carriage return ^M (the Windows CR in CRLF)
  • <C-v>{1-3 decimal digits} — inserts the character with that ASCII code (e.g., <C-v>065A)
  • <C-v>u{hex} — inserts a Unicode code point (e.g., <C-v>u03B1α)

Example

To strip Windows-style carriage returns (^M) from a Unix system:

:%s/<C-v><C-m>//g

The <C-v><C-m> places a literal ^M in the search pattern, matching the carriage return that \r doesn't always match in older Vims.

To replace every real tab with four spaces (when \t in the replacement side isn't honoured):

:%s/\t/<C-v><Tab><C-v><Tab><C-v><Tab><C-v><Tab>/g

Tips

  • \t reliably matches tabs in the search side of a substitute — <C-v><Tab> is most useful on the replacement side
  • In insert mode, <C-v> works identically: embed a literal key mid-text without triggering abbreviations or mappings
  • Do not confuse with normal mode <C-v>, which starts a visual block selection
  • See :help c_CTRL-V for the full specification

Next

How do I read from and write to files directly in Vimscript without shelling out to external commands?