UNagaswamy
UNagaswamy

Reputation: 2150

Commenting multiple lines in an ipython cell

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

Upvotes: 5

Views: 10629

Answers (4)

Numbermind
Numbermind

Reputation: 103

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

Upvotes: 0

sm7
sm7

Reputation: 61

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)

Upvotes: 0

candid
candid

Reputation: 179

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'

Upvotes: 0

Pedro9
Pedro9

Reputation: 196

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

'''
This is a multiline
comment.
'''

Upvotes: 3

Related Questions