The crazy shortcut keys of JupyterLab and Jupyterlab-vim
OK, I've been using JupyterLab really extensively over the last few days and mainly with keyboard shortcuts. They are super hard to find and remember mainly because like Vi itself there isn't a central place where they all go (except maybe help). So here for everyone else is a list.
First an aside, actually figuring out how the keys work and how to get them into this article was really tricky.
The VIM notation <C-K> means hold the CTRL key down and press the letter K where the first characters are the modifier keys, that is C means CTRL, ALT means the Alt key on Windows or the Option on the Mac. Vi does not use the Command key on the Mac. The one that is the most confusing is the Meta key or <M-X> for instance means hold down the meta and then the letter X. On Vi by default meta is mapped to the backslash \, so <M-X> means is the sequence backslash and then the letter X.
Also as another useful aside, if you don't like reaching up to hit the ESC or even the tab, the overall system defaults are mapped to <C-I> is Tab, <C-[> is Escape (although Vi also accepts <C-C> for this as well), and <C-H> is backspace.
Then the final thing is that if you are trying to type this into Markdown, you have to escape the angle bracket which you do with a backslash. There are some other special cases like for me the backslash starts an entry and there are special cases for a single backslash (aka backtick) by itself where using the HTML escape works ` although this is pretty obscure.
The final thing is that with the WordPress editor, it doesn't like the angle brackets either since it interprets these as raw HTML. Annoying pasting something like <C-S> does not work properly, but typing it does \<C-S> so a strange idiosyncrasy of the editor. But it does work in a Code block which is why the following is a code block and then you transform it into raw HTMLK and it fixes the backslash that I put there to escape it. Argh!
There are set of default keyboard mappings Jupyter Keyboard Shortcuts that take their inspiration from vi. Note that not nearly all of vim actually works, there are no key repeats and escaping out doesn't work. You can hack away at these mappings if you like although that is a bit of a mess. The TL;dr is that you can navigate between cells with a quick
j to go down and
k to go up and
G gets you to the bottom and
gg gets you to the top.
When you want to edit a cell, then press ENTER and this gets you into the cell and you can move around with the usual
k keys. If you want to move to the previous cell it is <C-J> or CTRL-J and <C-K> or CTRL-K. When you are done with a cell and want to go back to going through them all, it is easiest to do a
:q but SHIFT-Escape does the same thing.
While inside the cell if you want to move it down the entire notebook, it is <C-E> or CTRL-E and up is <C-Y> is move it up. And if you want to run the cell and move to the next it is <S-Enter> and <C-Enter> means run the current cell.
And below is the complete-ish list of commands:
- <S-Enter> Run cell and select below
- <C-Enter> Run cell
- <A-Enter> Run cell and insert a new one
- <S-Escape> go exit a cell and go back to jupyter move through cells mode
Jupyterlab supports folding, so you can hide cells with:
- Z, C - Hide code of selected cells
- Z, M - Hide all code
- Z, O - Expand code of selected cells
- Z, R - Expand all code
Note that in JupyterLab there is a somewhat misleading entry called Settings/Text Editor MOde/Vim but this is only for standalone
.py files for this to work in
.ipynb files, we load the jupyter-lab bindings.
With these bindings when you are in a cell, you are vim command mode and you enter edit with a or i and escape with <C-C>, but while you are Jupyter mode that is moving between cells, you get these short cuts additional shortcuts
- j - move to next cell
- k - previous cell
- g, g - Go to first cell
- <S-G> - Go to last cell
- y, y - copy cell
- p - paste cell
- <S-P> - paste cell above
- O - insert cell
- <S-O> - Insert cell above (like vi)
- <C-E> - move cells down
- <C-Y> - Move cells up
- Z, Z - center the current cell
That means to move some cells, you use <S-j> to select, the dd to delete them and then v to paste them to the correct spot.- Enter or TAB - moves you to vim mode for the current cell
You can move across cells as well and stay in vim mode with and navigate and run the commands from Jupyter mode by using <C-O> :
- <C-O>, U - Undo cell action
- <dash> - A dash splits the cell at the cursor
- <C-O>, D - Cut cell
- <C-O>, Y - Copy current cell
- <C-O>, P - Paste the cell in the clipboard
- <C-S-K> and <C-S-J> - Extend marked keys above/below
- <C-O>, O - Insert a cell below
- <C-J> and <C-K> - select above/below
- <C-O>, G - Select the first cell
- <C-O>, <C-G> - Select the last cell
- <C-E> - Move the current cell down
- <C-Y> - move the current cell up
- <S-Enter> - Run current cell, close it and move to next cell
- <C-Enter> - Does nothing in vim mode but when you escape to Jupter runs the current cell
- <C-1>, <C-2>, <C-3> change to code, markdown or raw
- :w - write the file to disk
- :q - quit the cell or the less convenient S-ESC, ESC works as well
- <C-O>, Z, Z - Center the current cell
When you are in a cell then you are in Vim command mode, so you can do things like and the notation here <C-x> means hold the CTRL key and press x, while <S-Enter> is hold the shift key and press Enter.
- j - next line, if it is the last line of the cell go to the next cell in vim mode. (really cool!)
- k - previous line, if it is the first line of a cell, go to the previous cell in vim mode (cool!)
- a - append to the current line
- i - insert and all the other vi command word
- <C-c> - exit the edit mode. Note that ESC is different
- ESC - This actually pushes you completely out of the cell (unlike vi), so you need to hit ENTER to get back
When you are in a cell then you get things like command completion:
- <C-n> shows the command completion key, you apply with ENTER and ignore with ESC
- <C-g> Shows the tool tip
Note that these control keys will not work in Chrome because you can not overload the<C-n> and other control keys. If you really like Chrome, then use Vivaldi which does allow this. And oh by the way it is private too and based on Chromium