27

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?

cramopy
  • 3,459
  • 6
  • 28
  • 42
RancheroBeans
  • 500
  • 1
  • 6
  • 15
  • I don't believe so, no. While Jupyter notebooks are handy for tinkering around, I've found them annoying (at best) to use as an IDE/text editor replacement due to missing features like this (not to mention simple find & replace functionality!). –  Jan 09 '17 at 17:57
  • I think you're right - I did find a workaround though. I have VIM keybindings activated within Jupyter alread, and googling around I found that VIM already has a way to handle things like this: http://vim.wikia.com/wiki/Search_and_replace – RancheroBeans Jan 10 '17 at 05:55
  • CTRL+D seems to work in Google Colab just as you describe (on a Chromebook). See also: https://stackoverflow.com/questions/63843035/is-there-a-shortcut-for-selecting-current-word-when-editing-a-cell-in-jupyter-no – Wolfram Arnold Jan 26 '21 at 20:37

5 Answers5

31

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

Saravanabalagi Ramachandran
  • 8,551
  • 11
  • 53
  • 102
  • 4
    `ctrl` + `d` deletes the line for me. Any idea on how to fix this? – maxbellec May 25 '18 at 08:22
  • @maxbellec check this link, to find custom.js file http://www.perfectlyrandom.org/2016/03/19/sublime-text-style-multiple-cursors-in-jupyter-notebook/ – mitsi Jun 27 '18 at 08:15
  • If you already have some other `require` function, do you just add this one to a new line? Or how does that work? – Roelant Mar 06 '19 at 10:38
  • How did you add the ctrl+alt+down arrow command? It looks incredibly useful – Doe a May 25 '21 at 18:29
  • on `Ubutnu/ Linux`, for me under location - `~/.jupyter/`, there is no `custom` folder found. So you might need to create folder called `custom` and then add `custom.js` in this folder with above given content. – MechaCode May 26 '21 at 14:52
4

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).

  • Clean and simple solution. Note that accessing the _nbextensions config_ is now accessed via the Edit menu at the top of a Jupyter notebook. Restarting Jupyter will display the Keymaps menu under edit after enabling "Select CodeMirror Keymap" via the _Nbextensions configuration_ page – Anil Mar 18 '22 at 10:23
  • 1
    ...with the caveat that the `ctrl-enter` behaviour is not handled and running a cell with that shortcut also inserts a newline below the cursor – Anil Mar 18 '22 at 10:31
  • Doesn't work due to the lack of the "Keymaps" option under "Edit" – Zhanwen Chen Mar 13 '23 at 21:55
2

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

dtlam26
  • 1,410
  • 11
  • 19
1

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 
    } 
);
Jamie
  • 2,181
  • 1
  • 21
  • 35
0

The only solution which made this work for me is

pip install jupyterlab_sublime