[jupyter-notebook] How do I block comment in Jupyter notebook?

I want to comment out a block of multiple lines in Jupyter Notebook, but can't find out how to do that in this current version.
It used to be in one of the drop down menus but is no longer there.
How do you comment out multi-line blocks of code at once?

This is not a duplicate because the solution given in the following link doesn't seem to work anymore:
How can I block comment code in the IPython notebook?

Ctrl + / does nothing.

This question is related to jupyter-notebook jupyter

The answer is


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.


Select the lines you want to comment out. Then press:

Ctrl + #


Try using the / from the numeric keyboard. Ctrl + / in Chrome wasn't working for me, but when I used the /(division symbol) from the numeric it worked.


TL;DR:

Using MacBook Pro with Spanish - ISO Keyboard.

Solution: Ctrl + -

Full story

This is an old post but reading it got me thinking about possible shortcuts.

My keyboard is a Latin Apple MacBook Pro, which is called Spanish - ISO. I tried the changing keyboard distribution to U.S. solution... this works but with this solution I have to switch keyboards every time I want to comment which... sucks.

So I tried ctrl + - and it works. The - is where the / is located in an english keyboard but doing Cmd + - only changes the Chrome's zoom so I tried Ctrl which isn't as used as Cmd in macOS.

My takeaway with this would be: if I have more shortcut problems I might try the original shortcut but using the key where the U.S. keyboard would have it.


Use triple single quotes ''' at the beginning and end. It will be ignored as a doc string within the function.

'''
This is how you would
write multiple lines of code
in Jupyter notebooks.
'''

I can't figure out how to print that in multiple lines but you can add a line anywhere in between those quotes and your code will be fine.


Select the lines on windows jupyter notebook and then hit Ctrl+#.


Fn + Cmd + / in Safari browser on MacOS


If you have a Mac and not a English keyboard: Cmd-/ is still easy to produce.

Follow the below steps:

  1. Just go into the Mac's System Settings, Keyboard, tab "Input Sources" or whatever it might be called in English
  2. Add the one for English (shows up as ABC, strange way to spell English).

Whenever you want a Cmd-/, you have to change to the ABC keyboard (in your menu row at the top of your screen,if you have ticked it to be shown there in the System Settings - Keyboard tab).

Cmd and the key to the left of the right "shift key" gives you Cmd-/.

P.S: Don't forget to switch back to your normal keyboard.


On MacOS 10.11 with Firefox and a German keyboard layout it is Ctrl + ?


Quick Addition to Top Answer: CTRL + / is nice because it toggles back and forth between adding and removing # at beginning of all selected lines. Didn't see that exact nuance mentioned so just wanted to add it here. (This worked in Firefox Developer Edition 54.0b12 on Windows 7).


I have not yet managed to find the best way possible. Since I am using a keyboard with Finnish layout, some of the answers do not work for me (e.g. user5036413's answer).

However, in the meantime, I have come up with a solution that at least helps me not to comment each and every line one by one. I am using Chrome browser in MS Windows and I have not checked other possibilities though.

The solution:
It uses the fact that you can have multiple line cursors in an Ipython Notebook.

Press the Alt button and keep holding it. The cursor should change its shape into a big plus sign. The next step is, using your mouse, to point to the beginning of the first line you want to comment and while holding the Alt button pull down your mouse until the last line you want to comment. Finally, you can release the Alt button and then use the # character to comment. Voila! You have now commented multiple lines.


I tried this on Mac OSX with Chrome 42.0.2311.90 (64-bit) and this works by using CMD + /

The version of the notebook server is 3.1.0-cbccb68 and is running on:
Python 2.7.9 |Anaconda 2.1.0 (x86_64)| (default, Dec 15 2014, 10:37:34) 
[GCC 4.2.1 (Apple Inc. build 5577)]

Could it be a browser related problem? Did you try Firefox or IE?


Another thing to add, in the version I'm using, the code has to be initialized in order to be to comment it out using CTRL and / . If you haven't ran the code and the code isn't colorized it wont work.


After searching for a while I have found a solution to comment on an AZERTY mac. The shortcut is Ctrl +/= key


On a Finnish keyboard use Ctrl + ' to comment on multiple lines and use the same keys to de-comment.

Ubuntu 14.04 Google Chrome


I add the same situation and went in a couple of stackoverfow, github and tutorials showing complex solutions. Nothing simple though! Some with "Hold the alt key and move the mouse while the cursor shows a cross" which is not for laptop users (at least for me), some others with configuration files...

I found it after a good sleep night. My environment is laptop, ubuntu and Jupyter/Ipython 5.1.0 :

Just select/highlight one line, a block or something, and then "Ctrl"+"/" and it's magic :)


For a Dutch keyboard layout (on Debian 9 in Chromium 57) it is Ctrl + °


I am using chrome, Linux Mint; and for commenting and dis-commenting bundle of lines:

Ctrl + /