Cross-platform way to enter Unicode characters

The previous post describes the hoops I jumped through to enter Unicode characters on a Mac. Here’s a script to run from the command line that will copy Unicode characters to the system clipboard. It runs anywhere the Python module pyperclip runs.

    #!/usr/bin/env python3

    import sys
    import pyperclip

    cp = sys.argv[1]
    ch = eval(f"chr(0x{cp})")
    print(ch)
    pyperclip.copy(ch)

I called this script U so I could type

    U 03c0

at the command line, for example, it would print π to the command line and also copy it to the clipboard.

Unlike the MacOS solution in the previous post, this works for any Unicode value, i.e. for code points above FFFF.

On my Linux box I had to install xclip before pyperclip would work.

3 thoughts on “Cross-platform way to enter Unicode characters

  1. It’s not a cross-platform solution, but if you find yourself on macOS and want to be able to do this without installing anything (Python or Python packages), you could use this zsh function:

    U() { print -n “\U$1” | pbcopy }

    The usage is the same as yours. The “pbcopy” command is built-in and copies its standard input to the clipboard.

  2. In my previous comment, the straight double quotes (U+0022) within the zsh function got turned into curly quotes. Be sure to change them back to straight quotes if you want to try the function!

Comments are closed.