Search code examples
visual-studio-codekeyboard-shortcutsspecial-charactersquantum-computingide-customization

How to create VSCode bindings to input bra and ket efficiently


I'm currently using VSCode for Q# programming. This sometimes entails including simple qubit expressions in the comments for clarity. It is of course possible to just settle with using regular angle brackets (such as |00> or <00|), but it looks nicer using the appropriate Unicode characters (such as |00⟩ or ⟨00|). Copying and pasting these characters whenever needed is a bit cumbersome, so it would be nice to have key bindings in VSCode just for this purpose. Actually, I'd like to be able to configure VSCode for quick access to any selection of characters I might be interested at the moment.


Solution

  • VSCode customization supports a type command which does exactly that - types in its argument. In order to create an entry for a keybinding, open the command prompt (Ctrl+Shift+P or ⌘+Shift+P on Mac) and type Preferences: Open Keyboard Shortcuts (JSON) and insert entries of the form:

    {
        "key": "<key-binding>",
        "command": "type",
        "args": {
            "text": "<character>"
        }
    }
    

    where <key-binding> is the usual description of the keybinding and <character> is the desired character literal. So, for the bra-ket case above, my customization looks like this:

    [
        {
            "key": "ctrl+shift+.",
            "command": "type",
            "args": {
                "text": "⟩"
            }
        },
        {
            "key": "ctrl+shift+,",
            "command": "type",
            "args": {
                "text": "⟨"
            }
        }
    ]