Sublime Forum

RichEdit alt+x or alt+c shortcuts, substitutes alt<code>

#1

In RichEdit editors (e.g. WordPad), inserting a hexadecimal code of a character and pressing Alt+X substitutes the value with the corresponding character.

It is a vital feature for those, who don’t have numeric keyboards (or fn-num switches).

For example decimal ALT0160 for unbreakable space is a0 in hex. After pressing the keys, it replaces 'a0' with ' '.

Can we have it in Sublime?

0 Likes

#2

Alt (to the left of the space bar) plus 34 (on the numeric keypad) correctly inputs a quote " character etc. in Windows - it’s an OS thing, not a RichTextBox thing.

Maybe you have a keybinding set that conflicts with it?

>>> sublime.log_input(True)
key evt: alt+keypad3
key evt: alt+keypad4
chr evt: " (0x22)
0 Likes

#3

I think what @vferko was asking about was actually typing the text a0 into the document and then pressing a keyboard key that would read the text out and convert it (due to using a keyboard without a numeric keypad).

If that’s the case that seems like something that could be implemented with a plugin of some sort.

3 Likes

#4

@OdatNurd, you’re right - I misread :slightly_smiling:

something like the following maybe, with a keybinding for the replace_char_code command:

import sublime
import sublime_plugin
import binascii

class ReplaceCharCodeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        hexcode = self.view.substr(self.view.sel()[0])
        self.view.replace(edit, self.view.sel()[0], binascii.unhexlify(hexcode).decode('UTF-8'))
2 Likes

#5

Thank you greatly for your input, however, I am a total n00b with sublime scripts. Can you point me to instructions where you guys usually put these snippet codes?

One more thing, if you type a code and press the keyboard combination twice, it decodes the character back. (You can see it in action in wordpad.) Can you update your snippet code? Thanks again!

0 Likes

#6

Tools menu -> Developer -> New Plugin…

Replace the contents with:

import sublime
import sublime_plugin
import binascii

class ReplaceCharCodeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        selection_contents = self.view.substr(self.view.sel()[0])
        is_hex = None
        try:
            value = int(selection_contents, 16)
            is_hex = True
        except ValueError:
            is_hex = False
        
        replace_with = None
        if is_hex:
            replace_with = binascii.unhexlify(selection_contents)
        else:
            replace_with = binascii.hexlify(bytes(selection_contents, 'UTF-8'))
        self.view.replace(edit, self.view.sel()[0], replace_with.decode('UTF-8'))

Save it in the folder ST defaults to, as a file named with a .py extension.

In your User Keybindings (from the Preferences menu), add:

{ "keys": ["alt+x"], "command": "replace_char_code" },

and save it

4 Likes

#7

UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xa0 in position 0: invalid start byte

I almost broke my brain debugging before I realised the Unicode a0 (U+00A0) is actually c2a0 in UTF-8.

For others wondering, here’s the reference table. Use correct utf-8 codes. :slightly_smiling:

Thank you @kingkeith for your solution!

0 Likes

#8

Instead of binascii.unhexlify and .hexlify, you could also just use chr and ord, which use unicode codepoints instead of the utf-8 encoding. And int ofc, like this: chr(int("a0", 16)).

2 Likes