Atom/Sublime like Multiple selections in Jupyter

16.9k views Asked by At

How can I select matching keywords in a Jupyter notebook via a keyboard shortcut? For example, in the Atom/Sublime editor I can hit cmd + D on a mac (or Ctrl + d on Windows) while the cursor is over 'var' and each time I do that the next 'var' will be highlighted. I can then type the new variable name and 'var' is replaced with whatever I typed.

var = "hello"
print(var)
print(var)

Is there an equivalent in a Jupyter notebook?

5

There are 5 answers

0
Jamie On

The above solution worked for me, but I found that it had the undesirable effect of entering a "tab" character when I hit enter. Here is the associated GitHub issue: https://github.com/jupyter/notebook/issues/4769#issuecomment-511935127

Per that post, I found that this solution gives the right ctrl + d behavior, and keeps tabs-as-spaces.

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 c=0; c< cells.length ; c++){
            cells[c].code_mirror.setOption('keyMap', 'sublime');
        }
        // }, 1000)// uncomment  line to fake race condition 
    } 
);
5
Saravanabalagi Ramachandran On

Add custom.js to

C:\Users\username\.jupyter\custom      # for Windows and 
~/.jupyter/custom/                     # for Mac 

with content

require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
    function(sublime_keymap, cell, IPython) {
        cell.Cell.options_default.cm_config.keyMap = 'sublime';
        cell.Cell.options_default.cm_config.extraKeys["Ctrl-Enter"] = function(cm) {}
        var cells = IPython.notebook.get_cells();
        for(var cl=0; cl< cells.length ; cl++){
            cells[cl].code_mirror.setOption('keyMap', 'sublime');
            cells[cl].code_mirror.setOption("extraKeys", {
                "Ctrl-Enter": function(cm) {}
            });
        }
    } 
);

and restart jupyter. Now Ctrl+D should work like it does in Sublime.

You can see that Ctrl-Enter functionality is disabled as it would be very convenient to run current cell rather than creating new line for most users. You can choose to have that functionality by commenting that line out.

You can disable other key config that you don't want in a similar way.

enter image description here

3
michimichiamo On

Most recent (and easy) way

The best way right now to achieve Sublime-like keymapping in Jupyter Notebook: Select CodeMirror Keymap from jupyter-contrib-nbextensions. As reported in the homepage:

The jupyter_contrib_nbextensions package contains a collection of community-contributed unofficial extensions that add functionality to the Jupyter notebook.

I personally use several extensions from this package and I find them very useful. As reported in the installation docs, you simply need to run:

pip install jupyter_contrib_nbextensions

to install the extensions (or better, I would suggest:

python -m pip install jupyter_contrib_nbextensions

where python points to the python executable of the installation you are using within Jupyter Notebook). You can also use conda if you prefer.

Anyway, you then need to copy some JS and CSS stuff to make the extensions work within Jupyter Notebook, which you can achieve through:

jupyter contrib nbextension install --user

again, assuming that jupyter points to the jupyter executable you are using to run your notebooks.

At this point, you simply need to enable the extension: navigate the nbextensions_configurator (that comes as a dependency with the jupyter_contrib_nbextensions package), which you can easily do through the Jupyter Notebook dashboard (to be clear, the page you open to run your notebooks) by browsing the Nbextensions tab and check the box corresponding to Select CodeMirror Keymap.

Done! Launching a notebook it will be sufficient to click on Edit>Keymaps>Sublime to achieve the desired behaviour.

keymaps selection

I know this is a rather old question, but I happened to come across it before finding out about jupyter_contrib_nbextensions (and in particular the Select CodeMirror Keymap extension). Thus, I decided to post this answer, hopefully to help other people like me and to let them avoid some further search or messing up with customized JS files (which could scary someone).

1
dtlam26 On

In jupyter lab now you can add in the extension by searching sublime

enter image description here

Click install and rebuild jupyter. **Notice: when you click install look at the terminal console, the building result will be shown there

enter image description here

0
Biggus Dickus On

The only solution which made this work for me is

pip install jupyterlab_sublime