jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.72k stars 4.96k forks source link

Customize keymaps - comment | uncomment toggle #3753

Open rezaffm opened 6 years ago

rezaffm commented 6 years ago

Well, I am getting a bit frustrated. I tried to unbind the key for commenting in edit mode following the instructions that are given here: http://jupyter-notebook.readthedocs.io/en/stable/extending/keymaps.html - but the unbinding never took place.

Consequently, also the re-binding, for instance to "Ctrl-7" never worked as well (German keyboard). So could maybe someone please provide an example configuration or tell me whether this is unexpected behavior or a case of "uts" (user to stupid)?

I ve been playing around with this for a while now and cd not solve it.

takluyver commented 6 years ago

Unfortunately there are two different shortcut systems in action - Jupyter's own (which that doc explains how to configure), and Codemirror's. Codemirror is the text editor component we use for cells. So most of the editing shortcuts are caught by Codemirror's shortcut system.

rezaffm commented 6 years ago

Is there any "way around" or should someone actually go and use the extension to modify keyshortcuts?

takluyver commented 6 years ago

There is some way around, I think, but I can never remember exactly what you need to do. Search for cm_config in examples and be prepared for some trial and error, because I think the ways to do it have changed over the years.

rezaffm commented 6 years ago

Well, I think I do not have to much luck with these shortcuts: https://github.com/ipython-contrib/jupyter_contrib_nbextensions/issues/1321

rezaffm commented 6 years ago

I searched through google and fine some "tutorials" to change the keyboard:

However, they all do not work. Searching the CodeMirror-Side I found the "keyMap" and "extraKeys" options that you could you during the instantiation of the editor (havent tried it yet). Looking into the addons of CodeMirror (comment.js), they toggleComment-Function is still there (same name). In the keymap e.g for instance it is called: { "Ctrl-/": "toggleCommentIndented" }

I might be thinking to easy, but the whole issue for None-US keyboards could then be solved by adding an "extraKey", for instance: extraKey = {'Ctrl+Shift+7': "toggleCommentIndented}.

How can you access CodeMirror the easierst way? Could you put me on the right track here? Should someone create a custom.js and access codemirror there?

rezaffm commented 6 years ago

I ve been playing around, while I found out that the toggleComment is here:

Jupyter.Cell.options_default.cm_config.extraKeys

It still has no effect, whether I change the key in the console or in a config.js File.

What am I missing?

rezaffm commented 6 years ago

Well, so changing anything on the Cell object had no effect. The only thig that worked finally was the following (custom.js in .jupyter/custom folder):

define([ 'codemirror/lib/codemirror', ], function(CodeMirror) { CodeMirror.keyMap.pcDefault["Ctrl-7"] = "toggleComment" } );

This toggles lines no by using Ctrl and 7.

phj6688 commented 4 years ago

I've the same problem!! its annoying

jeanmonet commented 4 years ago

Any chance of getting this to be part of a configurable option?

AlexYoOS commented 3 years ago

I know there is a way to manually write commands in a .js file in the jupyter notebook installation, but also editing jupyter notebook shortcuts from the shortcut menu actually works when following this strict set of rules, whereafter the notebook must be restarted:

Screenshot 2021-01-14 at 09 38 07

What however is not written in there, is that some keys seem to be exempt from functionality, e.g. if you use ssin a key combination, the key combination is added, but actually does not work

Createdd commented 2 years ago

I solved it like I described here: https://stackoverflow.com/a/71480779/7056765