61

How can I see the Unicode code point of the character where the cursor is? For example, if my cursor is on a character, I'd like Vim to tell me that it is U+2318.

Alternative information, such as the base-10 representation (8984) or the UTF-8 representation (E2 8C 98) would be acceptable.

I ask about Unicode and UTF-8 because they are most common, but if the answer generalizes to other character sets and encodings, that would be good to know as well.

1 Answer 1

76

You can use %b or %B in statusline or rulerformat. From :help statusline:

b N   Value of character under cursor. 
B N   As above, in hexadecimal. 

For example:

set statusline=%b\ 0x%B

Gives you:

8984 0x2318

Another way is to use ga or the :ascii command. From :help ga:

:as[cii]        or                                      ga :as :ascii
ga                      Print the ascii value of the character under the
                        cursor in decimal, hexadecimal and octal.

Which will give you:

<⌘> 8984, Hex 2318, Octal 21430

Another useful mapping is g8:

e2 8c 98

Which prints the hex value of the actual bytes stored in the file (this command assumes UTF-8).

In addition there are some plugins you could use:

  • unicode.vim adds various useful unicode-related commands. Use :UnicodeName to get details of the character under the cursor.

  • characterize.vim; this expands the ga command with the unicode name, similar to unicode.vim.

  • My own uni CLI tool has a little Vim integration script which provides :UnicodeName from unicode.vim (but none of the other features).

4
  • "this command assumes UTF-8" - the documentation says this, but my experience is that it works in any encoding (but does not respect fileencoding).
    – Random832
    Commented Feb 24, 2015 at 13:27
  • 4
    After further experimentation, a if encoding is set to a non-utf8 multibyte encoding such as cp932, g8 will only print the first byte, but ga will show the full character number.
    – Random832
    Commented Feb 24, 2015 at 13:35
  • Is there a way to have the output of g8 appear in statusline by itself? Kinda of like the original question with the "alternative information" part.
    – 0fnt
    Commented Oct 16, 2016 at 15:39
  • See vi.stackexchange.com/questions/27019/….
    – Aktau
    Commented Apr 2 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.