Is it possible to do block-comments in Google Colab?
Asked Answered
T

9

30

In Jupyter Notebook, you can toggle/comment a block of selected code with Ctrl + /, but this doesn't work in Google Colaboratory notebooks. Is there a way to comment out several lines of Python code easily in a Colaboratory notebook?

I've looked in the documentation, but I came up short.

Taw answered 30/12, 2019 at 19:45 Comment(2)
A related question is the subject of a meta question.Forceps
Time to get a macro keyboard!Forceps
C
41

In the default Colab keyboard shortcuts, you can block comment text using Ctrl+/.

If this does not work for you, check Tools->Keyboard Shortcuts and search for "Comment Current Line" to see what the shortcut is within your current settings.

Charqui answered 2/1, 2020 at 17:16 Comment(6)
So strange, I could swear I tried this out on Colab and it didn't work for me. Works now though. Thanks.Taw
If you're in Mac you need to push the Command key as well.El
It is still not working for me. What do you mean by push the Command key?Phytosociology
@El thanks! I can confirm the working shortcut on mac os is Ctrl+Cmd+/Vain
Thanks @jakevdp! For me (in macOS Big Sur 11.4) it works to say ⌘ / (that is, just cmd+/, no ctrl required).Stoup
I'm on French keyboard you have to use CTRL+SHIFT+/Typhogenic
N
9

You can select the lines of code and press (Ctrl + /) to comment or uncomment your selected lines of code.

You can also use triple single quotes (''') at the start and end of the code block you are interested to comment out.

''' ...Your commented code... '''

Nuss answered 25/2, 2020 at 14:10 Comment(1)
I think your second solution is not correct. the three quotes it is for multiline string, not for comment. look this notebookJoachima
F
2

If you are a Mac user, you can use Command + / for commenting out, and if you want to uncomment again, select the lines and then Command + /.

Ferrocene answered 6/4, 2021 at 0:18 Comment(0)
C
2

For Mac users who don't have an American keyboard like me: it seems like Colab thinks you are using an American keyboard for shortcuts, and so to make the '/' character you have to press the second button to the right of the 'm'. I'll leave an image to explain.

My keyboard looks like this:

My layout

As I said, I have to press the . key to make '/' as if I were using an American keyboard.

Cessionary answered 6/5, 2021 at 8:13 Comment(1)
Are you sure it is not the next key, with "_"/"-" (underscore/dash)? United States keyboard layoutForceps
C
1

For some reason, I need to do Ctrl + Shift + :.

Which technically is Ctrl + /.

(I use an AZERTYUIOP keyboard.)

Carabin answered 30/7, 2021 at 10:7 Comment(0)
A
1

if you come here with an Italian keyboard you should press: command + control + .

Asuncionasunder answered 11/2, 2023 at 6:28 Comment(0)
H
0

For Mac users:

  • first select the lines that you want to comment
  • second push these buttons: Command + Ctrl + Shift + /

Especially in the Italian keyboard it would be: Command + Ctrl + Shift + 7 (because the "/" character is combined with button "7")

Hadj answered 17/7, 2021 at 15:15 Comment(0)
R
0

in my case on mac is : ctr + cmd + -

Redfish answered 5/10, 2023 at 14:29 Comment(0)
E
-1

When using Mac the keyboard shortcut is +/.

Note that depending on your layout, / cannot be directly addressed and its combination is different when combined with . For example, on a German keyboard / is [Shift]+7 but the combination +/ is +[Shift]+ß.

When you are unsure about which combination is correct, use the inbuilt keyboard viewer which provides a live view of the meaning of keys while selecting modifiers.

Keyboard Viewer: default keyboard mapping

enter image description here

Keyboard Viewer: modifier keys keyboard mapping

enter image description here

Elodiaelodie answered 3/5, 2022 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.