5

Is there a way to comment multiple lines of code in an ipython cell in one go?

UNagaswamy
  • 2,068
  • 3
  • 30
  • 35

4 Answers4

3

You can use triple-quoted strings. Although this prints a repeats out statement, which I would like to avoid.

'''
This is a multiline
comment.
'''
Pedro9
  • 186
  • 1
  • 11
0

To comment multiple lines you can use:

ctrl + /

This won't print a repeat out statement the way the triple quotes does.

""" This is a multi line 
    comment.
"""

Out[1]:' This is a multi line \n    comment.\n'
candid
  • 189
  • 1
  • 13
0

A better way is to use Alt-# shortcut.

This is a readline binding, so it will work on any shell which has GNU readline available, like Bash.

To use it simply type the code in an ipython block like:

In [1]: if True:
   ...:     pass 

Then to comment the complete block, press Alt-# anywhere within the cell.
The code will then change to:

In [1]: #if True:
   ...: #    pass
   ...: #    

To uncomment, we need to pass a numerical argument to this readline function. It can be done by Alt-some_number anywhere within the cell.

Note that most of the terminals have keybindings for changing tabs mapped to Alt- 1 through 9. So if you have a tab opened, try to use a number that won't be allotted to a tab. Like, if you have 5 tabs opened, use Alt-6 (Any number works. It just needs to be passed to readline instead of being intercepted by the terminal)

sm7
  • 53
  • 8
0

For me Ctrl + ^/~. The other solutions didn't work for me. I'm using windows 10 and Jupyter Notebook.

Numbermind
  • 93
  • 1
  • 12