13

How can I comment out multiple cells in Jupyter Ipython / JupyterLab notebook? The code is in Python.

The keyboard shortcuts Ctrl + / on Microsoft Windows and Cmd + / on Mac OS X only work if the selected code is within one cell. However, if I select several cells, then these keyboard shortcut don't work anymore.


Selecting several cells can be done by clicking on the margin of cell, then holding CTRL or SHIFT, and clicking on the margin of another cell:

enter image description here

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • 3
    Ctrl + / should world for multiple lines. What browser are you using? – gbeaven Jan 15 '19 at 20:49
  • Have you seen this https://stackoverflow.com/questions/29885371/how-do-i-comment-out-multiple-lines-in-jupyter-ipython-notebook Ctrl+ / works fine on my Firefox. – Nav Jan 15 '19 at 20:50
  • @gbeaven Ctrl + / does work for multiple lines but only if these lines are within the same Jupyter cell. – Franck Dernoncourt Jan 15 '19 at 20:50
  • I think the only way is by converting the selected cells into markdown. Check https://stackoverflow.com/questions/32444840/jupyter-how-to-comment-out-cells – kHarshit Jan 16 '19 at 12:09

2 Answers2

8

As stated in the comments, you can:

a) Convert all them to markdown (select and press m)

b) Go cell by cell selecting all text (control+a) and then (control + /) to comment

c) Merge all cells in one, and then comment. Unfortunately, I don't think this option is reversible

If anyone knows another option, I'd be happy to know about it!

3

There are two ways to solve the intention of having these cells treated as text:

1) in Jupyter, multi-select the cells and press m for markdown; now they will behave like markdown/text which is just as good as commenting them out. Note that this process is reversible by multi-selecting the cells and pressing y to convert the cells to code. A drawback of this approach is the formatting that will apply by default to your markdown cells. To have them appear like code but behave as text seems to require the manual intervention of wrapping each markdown cell in <pre> tags as in: <pre>print("my code")</pre>

2) If there is a need to have them treated as code but commented out, the only way to do this without going one by one (that I could find), is to merge the cells into one, but that requires trickery when you have more than two cells. Once done, the command / (or control / on Windows) will work on multiple lines.
To do this: If only two cells involved

  • multi-select the cells
  • in Jupyter menus: Edit -> Merge Cell Above (or Merge Cell Below)
  • Select the contents within the whole cell
  • now you can use control/command / (windows or mac) to comment all code out

For more than two cells - try this:

  • multi-select all cells
  • control-c or command-c (copy command)
  • paste the results into your favorite text editor
  • select all and copy the results within your text editor
  • now paste back into a single cell of your notebook
  • now select within the notebook and use control/command / (windows or mac)
  • now multi-select the rest of your cells with the original split up content
  • delete these cells leaving only your newly merged and commented out cell behind

As observed by others - this process is not easily reversible. You can uncomment all the lines of code but splitting them back into their original cells would require manual effort I think.

I tested all of this in Anaconda Jupyter using a sublime text editor for the text editor steps. Here is the version information from my system from when I did these tests:

The version of the notebook server is 5.0.0 and is running on:
Kernel Info:
Python 3.6.1 |Anaconda custom (x86_64)| (default, May 11 2017, 13:04:09) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
TMWP
  • 1,545
  • 1
  • 17
  • 31
  • Are there any updates on this? Would be really nice to shift-click multiples cells, hit ctrl + / and keep going. – CreekGeek Nov 09 '20 at 23:11
  • I am not aware of any updates to this but if anyone else knows one please do add it to the comments. – TMWP Jan 21 '21 at 14:58