3

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.

3 Answers 3

12

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.

7
  • I'm sorry to say that nothing is showing up. Is there any action needed beside creating the file? I suppose it is not.
    – actimel
    Mar 3, 2014 at 17:22
  • Yes it is the only thing you need. ST automatically runs it. See if any errors show up in your ST console. Otherwise you'll have to wait for me or someone else to test this on ST2. Mar 4, 2014 at 7:11
  • Packages/User should be your local-one. You can find it out by clicking Tools/New Plugin... and then saving... Aug 12, 2014 at 9:19
  • This should be released. Aug 12, 2014 at 9:20
  • @VajkHermecz I don't think it's quite necessary, if you want, you may make a package though, I won't mind. What do you mean it overwrites default content? It appends that for me to the contents of the statusbar Aug 12, 2014 at 9:22
4

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
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Dan Lowe
    Dec 30, 2015 at 3:34
  • 2
    The link is not just a URL, it has text, which in this case is the name of the plugin. That allows searching for it later, should the URL change. NB the OP was asking for a package, and this is the name and location of one; it's not like I can copy and paste a whole package into the answer. (That would be unhelpful should the package be updated.) I would have been happy finding this answer when I was asking the same question as the OP.
    – Tom Hundt
    Dec 30, 2015 at 22:29
0

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:

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.