-->

Show info about current character in status bar in

2020-07-22 10:05发布

问题:

I'm missing one useful feature which others text editors often offer. In bottom status bar they show ASCII and UTF code of current character - character before or after current position (not sure now). I cannot find package doing that or native feature that does that.

Thank you for your help.

回答1:

Here is one such plugin, it displays the character code in decimal: Show Character Code

Simple Sublime Text plugin for displaying decimal code of the current character in the status bar

Although it shows only the decimal value for the character code



回答2:

I made a plugin for this :)

Create a anyname.py file in your Packages/User/ directory.

import sublime, sublime_plugin, textwrap, unicodedata

class utfcodeCommand(sublime_plugin.EventListener):
    def on_selection_modified(self, view):
        # some test chars = $ €
        sublime.status_message('Copying with pretty format')
        selected = view.substr(view.sel()[0].a)
        char = str(selected)
        view.set_status('Charcode', "ASCII: " + str(ord(selected)) + " UTF: " + str(char.encode("unicode_escape"))[2:-1])

This should show you the ASCII and Unicode code in the status bar of the character to the right of the caret.

Tell me if this works for you, tested with ST3 on Kubuntu Linux 12.04 x64. Probably won't work on ST2 because of the different Python versions.



回答3:

I ran into several issues with the code posted by Sergey Telshevsky in ST2 / Python 2.7:

I got a SyntaxError: Non-ASCII character '\xe2' in file ./display_character_code.py on line 7 because of the # some test chars = $ € - removing this commented out code, or declaring a character encoding at the top of the Python code, e.g. # -*- coding: UTF-8 -*- gets rid of the error. I also got UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' when selecting the sample "€" (because it is not an ASCII character). And even after fixing these, the Unicode key was never displayed; e.g. the status bar showed ASCII: 123 UTF:. So I reworked his example and came up with the following:

import sublime_plugin

class statusCharCodes(sublime_plugin.EventListener):
    def on_selection_modified(self, view):
        selected = view.substr(view.sel()[0].a)

        try: 
            ascii = str(ord(selected.encode("ascii"))).zfill(3)
        except: 
            ascii = "n/a"

        try:
            utf = "U+" + str(format(ord(selected),"x")).zfill(4).upper()
        except: 
            utf = "n/a"

        view.set_status("Charcode", "ASCII: " + ascii + " UTF: " + utf)

Example output: