How do I block comment in Jupyter notebook?

Jupyter NotebookJupyter

Jupyter Notebook Problem Overview


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:
https://stackoverflow.com/questions/19318298/how-can-i-block-comment-code-in-the-ipython-notebook

Ctrl + / does nothing.

Jupyter Notebook Solutions


Solution 1 - 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.

Solution 2 - Jupyter Notebook

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.

Solution 3 - Jupyter Notebook

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.

Solution 4 - Jupyter Notebook

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

Solution 5 - Jupyter Notebook

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

Ubuntu 14.04 Google Chrome

Solution 6 - Jupyter Notebook

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.

Solution 7 - Jupyter Notebook

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

Ctrl + #

Solution 8 - Jupyter Notebook

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?

Solution 9 - Jupyter Notebook

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.

Solution 10 - Jupyter Notebook

Fn + Cmd + / in Safari browser on MacOS

Solution 11 - Jupyter Notebook

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

Solution 12 - Jupyter Notebook

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

Solution 13 - Jupyter Notebook

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

Solution 14 - Jupyter Notebook

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

Solution 15 - Jupyter Notebook

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

> Ctrl + /

Solution 16 - Jupyter Notebook

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

Solution 17 - Jupyter Notebook

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.

Solution 18 - Jupyter Notebook

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchrisfsView Question on Stackoverflow
Solution 1 - Jupyter Notebookuser5036413View Answer on Stackoverflow
Solution 2 - Jupyter NotebookDatamanView Answer on Stackoverflow
Solution 3 - Jupyter NotebookGaduksView Answer on Stackoverflow
Solution 4 - Jupyter NotebookJoseph TrueView Answer on Stackoverflow
Solution 5 - Jupyter NotebookMr. Unnormalized PosteriorView Answer on Stackoverflow
Solution 6 - Jupyter Notebookloco.loopView Answer on Stackoverflow
Solution 7 - Jupyter NotebookphiView Answer on Stackoverflow
Solution 8 - Jupyter NotebookKoenView Answer on Stackoverflow
Solution 9 - Jupyter NotebookScott WellerView Answer on Stackoverflow
Solution 10 - Jupyter NotebooksmciView Answer on Stackoverflow
Solution 11 - Jupyter NotebookdmainzView Answer on Stackoverflow
Solution 12 - Jupyter NotebooksauravuView Answer on Stackoverflow
Solution 13 - Jupyter NotebookalExView Answer on Stackoverflow
Solution 14 - Jupyter NotebookKevin Van PutView Answer on Stackoverflow
Solution 15 - Jupyter Notebookdebaonline4uView Answer on Stackoverflow
Solution 16 - Jupyter NotebookCasper GerritsenView Answer on Stackoverflow
Solution 17 - Jupyter NotebookTare GaskinView Answer on Stackoverflow
Solution 18 - Jupyter NotebookIngmar KronfeldtView Answer on Stackoverflow