7

Does anyone know if there a way to bind a code snippet to a keyboard shortcut in Jupyter Lab? For example in R Studio you can use Ctrl+Shift+M to write the pipe operator (%>%) quickly and I got used to that functionality so I would like to replicate it.

I looked at the Keyboard Shortcut menu under Settings but I'm not sure how to use the JSON schema to write such an Override (if it is even possible from there), and the documentation wasn't very clear.

krassowski
  • 13,598
  • 4
  • 60
  • 92
Sraffa
  • 1,658
  • 12
  • 27

2 Answers2

6

In JupyterLab 2.1+ you can configure your keyboard shortcuts for R with the following:

{
    "shortcuts": [
        {
            "command": "apputils:run-first-enabled",
            "selector": "body",
            "keys": ["Alt -"],
            "args": {
                "commands": [
                    "console:replace-selection",
                    "fileeditor:replace-selection",
                    "notebook:replace-selection",
                ],
                "args": {"text": "<- "}
            }
        },
        {
            "command": "apputils:run-first-enabled",
            "selector": "body",
            "keys": ["Accel Shift M"],
            "args": {
                "commands": [
                    "console:replace-selection",
                    "fileeditor:replace-selection",
                    "notebook:replace-selection",
                ],
                "args": {"text": "%>% "}
            }
        }
    ]
}

Accel means Ctrl on Linux and Windows, or Command on Mac.

Just go to the Advanced Settings Editor -> Keyboard Shortcuts, paste and save:

illustration

Related issues in JupyterLab repository: #4519, #7908, #10114.

Alternatively, for more advanced functionality, like kernel-specific shortcuts or automatic padding you can use jupyterext-text-shortcuts extension.

krassowski
  • 13,598
  • 4
  • 60
  • 92
3

OK I don't have a good answer for Jupyter Lab, but for good old jupyter notebook, just ran the below cell would give you what you want:

IRdisplay::display_javascript("Jupyter.keyboard_manager.edit_shortcuts.add_shortcut('Ctrl-Shift-M', {
    help : 'add pipe symbol',
    help_index : 'zz',
    handler : function (event) {
        var target = Jupyter.notebook.get_selected_cell()
        var cursor = target.code_mirror.getCursor()
        var before = target.get_pre_cursor()
        var after = target.get_post_cursor()
        target.set_text(before + ' %>% ' + after)
        cursor.ch += 5
        target.code_mirror.setCursor(cursor)
        return false;
    }}
);")
PaulDong
  • 711
  • 7
  • 19
  • this unfortunately returns a javascript error for me: `Javascript Error: Unexpected token ':'` (I am using JupyterLab 1.2,4) – michalrudko Jan 09 '20 at 13:51
  • @mrjoseph unfortunately my answer only applies to Jupyter Notebook. I had been trying to figure out a way to do the same in the Jupyter Lab, but it seems all overly complicated. – PaulDong Jan 10 '20 at 01:24
  • Wait, doesn't regular jupyter notebook already have this feature? I can definitely press the key combo to insert a magrittr pipe, I was doing it for ages. There was en extension for jupyterlab to do the same, but I cannot find it anymore. :/ – jena Apr 07 '22 at 18:29