Sublime Forum

Options for typing Unicode characters

#1

Hello,

Build: 3126, OS: Windows

I´ve recently been using Sublime and are gradually switching to it for various purposes, as it makes typing extremely efficient. However, what I haven´t come to terms with is a hassle-free typing of Unicode-characters in HEX-format, as I need to do this regulary in my work; I especially encounter weird and seldomly used characters for linguistic purposes very often.

What I do at the moment are three different things.
(1) I set my keyboard to ISL with special modifications that can be downloaded here:

http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=uniipakeyboard

This is especially useful for diacritics. as I can stack them with a few strokes.

(2) For certain language-specific characters I hold ALT and type a decimal code, but this doesn´t work with all the characters.

(3) The others I just copy from a list which is gradually evolving. I find this procedure especially annoying, and would like to get away from that.

What I would like is to be able to type them via Unicode-HEX, for example 03DC for “ϝ” (digamma), then press one or two keys to convert them. In LibreOffice Writer this is possible via 03DC + ALT + c, something like that would suffice.

Is there any better way than doing this or some existing packages that can easily be implemented? What are some options?

I don´t mind if the characters are displayed in Sublime, as if they don´t exist in the font, as I choose a font in which they will appear in the end. I also don´t mind remembering Unicode-numbers, if there is already a shortcut somewhere involving them.

(As you may see from this post, I´m not very apt at programming, and probably even an idiot. If the solutions involve some tinkering with Sublime, I probably need some further elucidation and some kind of starting point.)

I am greatful for any help whatsoever.

1 Like

Keybinding context question
#2

as a super simple (but not-yet-ideal) example of how you could achieve this in ST, you could open the ST console (View menu -> Show Console, which is a Python interpreter), and type \u03DC and it will output the relevant character in the console for you to copy and paste into your main document. A plugin could easily be built around this functionality to make it easier, for example you press a key, it prompts for a hex value, and inserts the character. I realize you said you are not apt at programming, but I don’t have time to investigate this right now - just wanted to post a little idea :slight_smile:

you could also look at this proposal for the LaTexTools plugin, which shows a preview of symbols for you to click on:


(ovbiously the idea there is a bit different, but you may decide you like the UX)

1 Like

#3

This was a fun little project!

You can get the exact same behavior as in LibreOffice, but you have to create some scripts and keybindings for it. Let’s start with the TextCommand:

import sublime
import sublime_plugin


class ConvertHexToUnicodeCharacter(sublime_plugin.TextCommand):
    """Converts all selections to unicode characters, if applicable."""
    def run(self, edit):
        for region in self.view.sel():
            candidate = self.view.substr(region)
            try:
                number = int(candidate, 16)
            except Exception as e:
                sublime.error_message('Failed to convert "{}" into a decimal '
                                      'number.'.format(candidate))
                continue
            try:
                character = chr(number)
            except ValueError as e:
                sublime.error_message("Failed to convert {} ({} in hex). This "
                                      "probably means it's not a valid unicode "
                                      "code point.".format(number, candidate))
                continue
            except OverflowError as e:
                sublime.error_message("{} is too big of a number..."
                                      .format(candidate))
                continue

            # Everything went okay. Let's replace the selection by the unicode
            # character.
            self.view.replace(edit, region, character)

Go to Tools -> Developer -> New Plugin, and paste the above in the new view. Save this as ConvertHexToUnicodeCharacter.py in your Packages/User directory.

Now, this command will do something to the current selection, so it’s not quite what we want yet. Let’s write a macro to change that:

[
    {
        // command provided by sublime
        "command": "move",
        "args": 
        {
            "by": "words",
            "forward": false,
            "extend": true
        }
    },
    {
        // this is the command that we just wrote
        "command": "convert_hex_to_unicode_character"
    },
    {
        // command provided by sublime
        "command": "move",
        "args": 
        {
            "by": "characters",
            "forward": true,
            "extend": false
        }
    }
]

Go to Tools -> Developer -> New Plugin and paste the above in there. Save this file as ConvertToUnicodeToTheLeft.sublime-macro, also in your Packages/User directory.

At this point your new Macro should show up when you go to Tools -> Macros -> User -> ConvertToUnicodeToTheLeft, and it should do precisely what LibreOffice does.

To bind it to a shortcut, go to Preferences -> Key Bindings. Sublime will present you with two views. The left view is the default keybindings. The right view is your personal keybindings. If you never wrote a keybinding, just paste this in the right view:

[
    {
        "keys": ["alt+c"],
        "command": "run_macro_file",
        "args":
        {
            "file": "Packages/User/ConvertToUnicodeToTheLeft.sublime-macro"
        }
    }
]

and save it. Now when you press altC, the hexadecimal number to the left of the cursor should get replaced by its equivalent unicode character. This’ll also work with multiple selections.

4 Likes

#4

As an alternative approach, surely there must be decent Windows software for inserting characters. Maybe something with a nice, searchable interface: hit the magic keystroke to activate, type “dig”, and pick the lowercase digamma off the list. I can imagine software that provides a better user experience than Sublime possibly could, but as I’m not a Windows user I don’t know what’s available.

0 Likes

#5

Wow, it all worked out nicely! Thank you very much for the detailed instructions of how to implement the code. Now I will start tackling it step by step to understand it!

1 Like

#6

Alternatively, you can enter hex codes in Windows by holding Alt and pressing the + (plus sign) key followed by a hexadecimal character code. For numeric hex codes like 2010 (hyphen), this is very easy if you can memorize them. It’s more tedious for hex codes that involve letters.

(If that input method doesn’t work, you’ll need to modify a registry key and reboot to enable hexadecimal character entry support, though I forget what the necessary registry key is off the top of my head. I’m thinking this works out of the box in Windows 10, but didn’t in previous versions of Windows.)

Also: BabelMap is superior to the Windows Character Map application for character selection. It hasn’t been updated for last month’s update to Unicode (10) yet though; I’d guess the author is waiting for Windows 10’s “Fall Creator’s Update” which should include Unicode 10 support.

0 Likes