How to Block Comment Code in the Ipython Notebook

How do I block comment in Jupyter notebook?

Ctrl + / works for me in Chrome browser in MS Windows. On a Mac, use Cmd + / (thanks Anton K).


Please note, if / did not work out of the box, try pressing the / key on the Numpad. Credit: @DreamFlasher in comments to this question.

How can I block comment code in the IPython notebook?

Default solution

In IPython 2.x and 3.x (cmd|ctrl)-/ works but requires an english (american) keyboard layout, see https://github.com/ipython/ipython/pull/3673.

Other keyboard layouts

In case you have a non-english keyboard layout, you can define a custom keybinding for the codemirror editor via your custom.js. To this end add e.g. the following lines

define([
'base/js/namespace',
'base/js/events'
],
function(IPython, events) {
events.on("app_initialized.NotebookApp",
function () {
IPython.Cell.options_default.cm_config.extraKeys = {"Ctrl-," : "toggleComment"};
}
);
}
);

to use Ctrl+, to toggle (block) comments. I use this with a german keyboard layout and IPython 3.0.
The previous solution (see edits) worked fine with chrome, but not with firefox.

Old solution (IPython 1.x)

If you are using IPython 1.x you can try the comment-uncomment.js from https://github.com/ipython-contrib/IPython-notebook-extensions - I haven't tried this yet, but I guess its a good start.

Commenting multiple lines in an ipython cell

You can use triple-quoted strings. Although this prints a repeats out statement, which I would like to avoid.

'''
This is a multiline
comment.
'''

Jupyter notebook shortcut to uncomment

CTRL+/ is a toggle for comment and uncomment! Thanks George for the answer...

shortcuts for editing code indentation in jupyter notebook

Ctrl-] : indent
Ctrl-[ : shifting back indentation

if it doesn't work, then try:

Tab : indent
Shift + Tab : shfting back indentation


Related Topics



Leave a reply



Submit