Tip: Using sublime text editing shortcuts in jupyter notebook cells

Hi all,

Ive been using sublime text editor for code projects for a while. Whilst jupyter notebooks is great for data science projects, I was missing some of those useful shortcuts like adding lines above and below, editing variables, multiple cursors etc.

For those who use sublime you know what i mean.

Well there is a way to get some of that mojo back!! Sublime editing in a Jupyter cell!!!


See this post by Robin Wilson.

What it does is allow you do a bunch of Sublime Text Commands in a jupyter cell.

I’m assuming you need to have sublime text 3 installed on your system, then follow these instructions:

So, how do you do this? It’s really simple.

  • Find your Jupyter configuration folder by running jupyter --config-dir
  • create a folder under this called custom and create custom.js file in the custom sub-folder in your favourite editor
  • Add the following lines to the bottom of the file
require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
    function(sublime_keymap, cell, IPython) {
        // setTimeout(function(){ // uncomment line to fake race-condition
        cell.Cell.options_default.cm_config.keyMap = 'sublime';
        var cells = IPython.notebook.get_cells();
        for(var cl=0; cl< cells.length ; cl++){
            cells[cl].code_mirror.setOption('keyMap', 'sublime');
        }
 
        // }, 1000)// uncomment  line to fake race condition 
    } 
);

It brings some nice tools to editing in a notebook cell. If you run into issues, like installation or keystroke clashes, start by reading the comments below the post.

It seems to only work in the current editable cell, not across the notebook. You can always join, edit and split cells as a work around.

sublime keyboard comands for mac
sublime keystrokes for win/linux

Enjoy!

1 Like

Just to clarify - you can also do this without any config changes; it just uses different shortcuts. E.g. hold down Alt (or Option) and click to get multiple cursor editing :slight_smile:

haha… Thanks Ob1 Jeremy. :grinning:

got the multiple cursor working . Its cmd + click for mac

Is there a list of these shortcuts anywhere. The p listing or ‘keystroke help’ h doesn’t seem have them.

#EDIT: Actually what I would really like is a good fuzzy match feature/plugin for python

Currently, as far as I know, pressing tab after a . gives me the list, but i still have to know the operations spelling to find by typing or use multiple arrow downs

What i want is the following, Eg typing torch.nn. then tab then typing
2d should give me all the operations with ‘2d’ and shorten this list as i type matching letters

I have added another post here about the #Edit comment above : fuzzy matching jupyter feature request. I posted a feature request on jupyter github issues.

If anybody else runs into the same problem;
Jupyter (on Ubuntu 16.04) seemed to occasionally import the tab instead of spaces indentation from ST when using the tab key

Solved this by adding this to the custom.js file, from here:

    IPython.notebook.get_cells().map(
        function(c) {  return c.code_mirror.options.indentWithTabs=false;  }
    );
    CodeMirror.defaults.indentWithTabs=false;

Hey,
Maybe you should try VSCode? Here is the link to download VSCode.

VSCode provides this and has all shortcuts you use in Sublime plus, its open source :slight_smile: