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

How do I search for non-printable or control characters in Vim?

Answer

/[\x00-\x1f]

Explanation

Files sometimes contain hidden control characters that cause subtle bugs. Vim can search for non-printable characters using hex escape sequences, letting you find and remove invisible bytes like null characters, carriage returns, or embedded control codes.

How it works

  • \x00 — matches a null byte
  • \x0d — matches a carriage return (^M)
  • [\x00-\x1f] — matches any ASCII control character (0-31)
  • \%x — alternative hex match syntax: \%xff

Example

/[\x00-\x1f]
Finds all control characters in the file including:
- \x00 (NULL)
- \x09 (TAB)
- \x0a (LF)
- \x0d (CR)

Tips

  • Use :%s/[\x00-\x08\x0b-\x1f]//g to strip all control characters except tab and newline
  • Press <C-v> followed by a control key in insert/search mode to type literal control characters
  • Use :set list to make non-printable characters visible
  • ga on a character shows its hex, decimal, and octal values

Next

How do I return to normal mode from absolutely any mode in Vim?