28

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?

Community
  • 1
  • 1
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • 1
    See the answers to the similar question [here][1] [1]: http://stackoverflow.com/questions/1432480/any-way-to-clear-python-shell – Saher Ahwal Jul 31 '11 at 20:47
  • http://stackoverflow.com/questions/1432480/any-way-to-clear-python-shell – Saher Ahwal Jul 31 '11 at 20:47
  • 1
    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 Aug 01 '11 at 00:48
  • 1
    I had to comment out this line from ~/.ipython/ipythonrc readline_parse_and_bind "\C-l": possible-completions After that it worked great – krethika Aug 16 '12 at 16:39

12 Answers12

48

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.

mzjn
  • 48,958
  • 13
  • 128
  • 248
15

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
Jakub Roztocil
  • 15,930
  • 5
  • 50
  • 52
  • The command `clear-screen` does not seem to work on my system (Windows with Spyderlib), but I like the idea of binding a solution to `C-l` – Amelio Vazquez-Reina Jul 31 '11 at 21:08
  • 1
    For me, this line was already in my ipythonrc readline_parse_and_bind "\C-l": possible-completions I had to comment it out, and then it worked great. – krethika Aug 16 '12 at 16:36
  • `SyntaxError: invalid syntax` for me on Windows – A T Jan 28 '13 at 15:26
12

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

lu5er
  • 3,229
  • 2
  • 29
  • 50
10

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

cls

On Windows, and on *nix:

clear
A T
  • 13,008
  • 21
  • 97
  • 158
6

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

Mauro
  • 817
  • 1
  • 9
  • 11
  • This works for me on Ubuntu (I assume almost all unix systems). However not on windows, but `!CLS` and `cls` works. – quapka May 09 '16 at 07:18
4

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 '
Alex Gaudio
  • 1,894
  • 1
  • 16
  • 13
2

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

You need to import os first though:

import os
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
1

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

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.

0
__import__('os').system("reset")
Dan D.
  • 73,243
  • 15
  • 104
  • 123
0

On Windows Powershell cls (in lowercase with no exclamation mark) clears the screen just fine.

Omar T.
  • 11
  • 3
0

I use ctrl + l in Windows 10 and it's works perfectly.

Yaucer
  • 3
  • 4
  • The ctrl + L shortcut has already been mentioned in the other answers. – Gino Mempin May 08 '23 at 00:35
  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/34330120) – Gino Mempin May 08 '23 at 00:35