358

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.

electric-lady
  • 13
  • 1
  • 5
chrisfs
  • 6,182
  • 6
  • 29
  • 35
  • Are automatic parentheses and quotes working? An earlier ipython version lost that functionality (at least, it wasn't default), but the latest stable version has it again. I can imagine commenting lines is bundled with those and other code editing features. Perhaps you need to update ipython. – drevicko Apr 27 '15 at 12:36
  • 11
    The solution of the linked question still works with IPython 3.0. Unfortunately, you need an american keyboard layout to use it (as given in the answer). With my german layout this has never been working, thus the original question and answer is still valid. – Jakob Apr 28 '15 at 05:00
  • 2
    possible duplicate of [How can I block comment code in the IPython notebook?](http://stackoverflow.com/questions/19318298/how-can-i-block-comment-code-in-the-ipython-notebook) – Jakob Apr 28 '15 at 05:02
  • I have an American keyboard. – chrisfs Apr 28 '15 at 06:50
  • I've updated my answer in the linked question to deal with non-english keyboard layouts. Give it a try. – Jakob Apr 28 '15 at 07:09
  • @Jakob I have an English/American keyboard, so testing a solution for a non english keyboards would not be practical. For whatever reason, the problem has gone away. I am simply looking for a while to close this question now – chrisfs Apr 28 '15 at 20:05
  • @chrisfs Great that it finally works! The update I posted is about defining a custom keyboard shortcut for the toggle comment event, so it should be suited for all keyboard layouts. I'm not sure how to close (and not delete) a question without answering it, sorry. – Jakob Apr 29 '15 at 06:39
  • How do I close this question myself ? Seems self serving to Answer and select my answer when the answer is that it 'fixed itself' – – chrisfs Jun 22 '15 at 16:55
  • + ; is what works on my ABNT keyboard (brazil) – Rodolfo Bugarin Oct 27 '21 at 21:37

18 Answers18

547

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.

llinfeng
  • 1,316
  • 1
  • 15
  • 39
user5036413
  • 5,494
  • 1
  • 10
  • 3
77

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.

Dataman
  • 3,457
  • 3
  • 19
  • 31
  • Clever. I just tried this in Firefox and it worked but had to careful with my cursor selection. – Michael Szczepaniak Oct 17 '16 at 18:02
  • 17
    Works like a charm! But just a clarification. After getting the big + sign by depressing the Alt key, do **NOT** select all the text in the block of code you want to comment out !! Just drag the pointer down the thin left edge of the text, selecting the beginning of the line in each of the lines in the block. Then pressing # will insert a # at the beginning of each of the lines. If instead you select all of the text, it will REPLACE the text in each line with a pound sign (#). – John Strong Nov 26 '16 at 23:45
  • 4
    You can select the whole text, actually. Just press the left arrow to deselect, and then type what you want and it will be applied to each line. :) – Matt Feb 03 '17 at 12:12
  • 1
    I like it, because it is more general than just to comment out a part. Do you know however that one can comment out part of the code with put it into a triple quote (like a multiline string)? – Arpad Horvath -- Слава Україні Oct 03 '18 at 19:09
  • 2
    crazy insane but works cheers – seralouk Mar 11 '20 at 11:10
48

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.

Abhishek
  • 407
  • 3
  • 18
Gaduks
  • 671
  • 6
  • 13
19

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

Joseph True
  • 621
  • 8
  • 6
18

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

Ubuntu 14.04 Google Chrome

17

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.

loco.loop
  • 1,441
  • 1
  • 15
  • 27
8

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

Ctrl + #

phi
  • 550
  • 8
  • 11
7

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?

Koen
  • 858
  • 2
  • 7
  • 22
  • I was using Firefox. I have a PC running Win 7 – chrisfs Apr 27 '15 at 08:19
  • Can you update your ipython version as mentioned by @drevicko? What version of ipython are you using? What version of python? – Koen Apr 28 '15 at 01:12
  • For whatever strange and undecipherable reason, ctrl-/ works now. Why it didn't work a day ago and it works now , I have no answer for. I have not changed my install, FWIW I am using The version of the notebook server is 3.0.0-f75fda4 and is running on: Python 2.7.8 |Anaconda 2.2.0 (64-bit)| (default, Jul 2 2014, 15:12:11) [MSC v.1500 64 bit (AMD64)] – chrisfs Apr 28 '15 at 06:45
  • How do I close this question myself ? Seems self serving to Answer and select my answer when the answer is that it 'fixed itself' – chrisfs Apr 28 '15 at 06:51
6

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.

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
Scott Weller
  • 89
  • 1
  • 4
  • That does not work for me. Jupyter complains `IndentationError: expected an indented block` no matter if I indent stuff betwee the quotes or not. – teylyn Sep 30 '16 at 02:27
  • Still prints out in the output cell, doesn't it? I commented out a for loop and I see this in the output cell below: `'\nfor column in homeData:\n homeDataColumn = homeData[column]\n print(type(homeDataColumn.values[0]))\n'` – JimLohse Oct 11 '16 at 19:06
  • You can then change the cell from 'Code' in the dropdown menu, to 'rawNB Convert' so there is no output when you run the cell – SherylHohman Mar 27 '17 at 06:13
  • It don't answer the question. – Natacha Sep 22 '18 at 19:53
  • This works for me, and is exactly what I was looking for when I came to this question. I wanted a quick way to comment out, and then add back in my code blocks without having to go through and delete every single '#'. Thank you. – Heather Claxton Jul 19 '20 at 02:20
6

Fn + Cmd + / in Safari browser on MacOS

smci
  • 32,567
  • 20
  • 113
  • 146
4

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
dmainz
  • 985
  • 7
  • 6
3

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

carla
  • 1,970
  • 1
  • 31
  • 44
sauravu
  • 39
  • 1
2

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

Casper Gerritsen
  • 314
  • 3
  • 13
2

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

alEx
  • 193
  • 6
  • 12
2

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

2

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

Ctrl + /

Debashis Sahoo
  • 5,388
  • 5
  • 36
  • 41
0

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.

Tare Gaskin
  • 1,209
  • 2
  • 10
  • 13
-1

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.

Keshav Pradeep Ramanath
  • 1,623
  • 4
  • 24
  • 33