25
votes

Is there a command in IPython to clear the screen?

EDIT: As @Saher mentions below, I can clean the screen using import os; os.system('CLS'), but is there a way to do this without having to import all of os?

10
See the answers to the similar question [here][1] [1]: stackoverflow.com/questions/1432480/…Saher Ahwal
you can also put an alias in your ipy_user_conf.py file. i use this quite often so i have it aliased to c rather than having to type !clear.wim
I had to comment out this line from ~/.ipython/ipythonrc readline_parse_and_bind "\C-l": possible-completions After that it worked greatmehtunguh

10 Answers

40
votes

To clear the screen on Windows, use !CLS.

On Unix-like systems, use !clear.

A shell command is executed by the operating system if prepended by an exclamation mark. See http://ipython.readthedocs.io/en/stable/interactive/reference.html#system-shell-access.

Note that commands should also work without the exclamation mark if they are defined as aliases. See http://ipython.readthedocs.io/en/stable/interactive/shell.html?#aliases.

There is also a Ctrl+L shortcut for clearing the screen. See http://ipython.readthedocs.io/en/stable/config/shortcuts/index.html#single-filtered-shortcuts.

15
votes

You can bind it to the common Ctrl-l shortcut by putting this into your ~/.ipython/ipythonrc:

readline_parse_and_bind "\C-l": clear-screen
11
votes

CTRL + L works in both Windows and Ubuntu. And I guess it's best because you don't have to type much.

9
votes

Maybe I'm just using a newer version, but it worked fine for me with just:

cls

On Windows, and on *nix:

clear
6
votes

for me, just to type "clear" is enought.

4
votes

clear is a default alias in ipython 0.11

In [76]: a = get_ipython()

In [77]: a.alias_manager.expand_alias('clear')
Out[77]: u'clear '
2
votes

If you are running windows try os.system('CLS')

You need to import os first though:

import os
1
votes

In macOS 10.13.1, Terminal 2.8, press command-k for Clear to Start.

clear resulted in "NameError: name 'clear' is not defined."

Caution: As a noob I may not fully understand what I did, but command-k seemed to do what I intended.

0
votes
__import__('os').system("reset")
0
votes

On windows using Enthought Canopy Clear, CLS, !CLS, does not clear the previous data, it just moves all the data to above the top of the window. If you scroll down the text is all there.