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

How do I search for a character by its ASCII or Unicode code point value in a Vim pattern?

Answer

/\%d{decimal}

Explanation

Vim's regex engine supports special atoms that match characters by their numeric value rather than their glyph. This lets you find invisible, non-printable, or visually ambiguous characters — like smart quotes, non-breaking spaces, en dashes, or control characters — without needing to paste them into the search field.

How it works

Atom Base Example
\%d{n} Decimal \%d9 = tab (ASCII 9)
\%x{n} Hexadecimal \%x09 = tab
\%u{n} Unicode (4 hex digits) \%u2013 = en dash (–)
\%U{n} Unicode (8 hex digits) \%U00002013 = en dash

Example

Replace all en dashes (U+2013) with regular hyphens:

:%s/\%u2013/-/g

Find all non-breaking spaces (U+00A0, decimal 160):

/\%d160

Strip zero-width spaces (U+200B) that may cause invisible diff noise:

:%s/\%u200B//g

Tips

  • Use ga to reveal the decimal/hex value of any character under the cursor, then construct the atom from that output
  • These atoms compose with the rest of Vim's regex: [\%d32\%d9] matches either a space or a tab
  • Particularly useful when cleaning up documents copy-pasted from Word, PDFs, or web pages that contain Unicode lookalike punctuation

Next

How do I add more keystrokes to the end of an existing macro without re-recording the whole thing?