Clearing the screen in IPython

爷,独闯天下 提交于 2019-12-18 11:45:45

问题


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?


回答1:


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.




回答2:


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



回答3:


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




回答4:


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

cls

On Windows, and on *nix:

clear



回答5:


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




回答6:


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 '



回答7:


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

You need to import os first though:

import os



回答8:


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



回答9:


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.




回答10:


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.



来源:https://stackoverflow.com/questions/6892191/clearing-the-screen-in-ipython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!