7

I am running and interactive ipython shell in an emacs buffer using ipython.el. I wonder if there is a way to clear the screen? Since it is not running on a terminal, the import os; os.system('CLS') trick will not work. Thanks.

hatmatrix
  • 42,883
  • 45
  • 137
  • 231

3 Answers3

10

It looks like ipython is based on comint, which means the following code should work for you (called with M-x my-clear or your favourite key binding):

(defun my-clear ()
  (interactive)
  (let ((comint-buffer-maximum-size 0))
    (comint-truncate-buffer)))

I posted some other options in response to this question.

Community
  • 1
  • 1
Tyler
  • 9,872
  • 2
  • 33
  • 57
  • Can you please update this answer so that it gets hooked up to python-shell-mode with C-l – Kaunteya Mar 11 '15 at 14:44
  • 1
    @Kaunteya The simplest way is `(add-hook 'inferior-python-mode-hook (lambda () (local-set-key (kbd "C-c l") 'my-clear))) ` To make it more general for any interpreter that inherits from `compilation-shell-minor-mode`, and using John Wiegley's `bind-key` package, `(bind-key (kbd "C-c l") 'my-clear compilation-shell-minor-mode-map)`. – pentavalentcarbon Aug 28 '18 at 19:05
7

C-c M-o runs the command comint-clear-buffer (found in inferior-python-mode-map), which is an interactive compiled Lisp function.

It is bound to C-c M-o.

(comint-clear-buffer)

Clear the comint buffer.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
aparkerlue
  • 1,485
  • 1
  • 15
  • 18
1

This clears the screen and the current session variables (using %reset):

(defun my-reset-ipython ()
  "Clear Emacs *Python* buffer and resets iPython variables.

Prints date and time of reset to iPython console and to
*Messages* buffer.

Assumes python has been configured to use iPython:

  (setq python-shell-interpreter \"ipython\")

This function does not reset the iPython console line numbering
or history (either because you can't do that in an iPython
console or the author couldn't find out how!)."
  ;; Allow function to be called via M-x
  (interactive)
  ;; Define variables: date-time string, message string, command to be passed to python
  ;; Requires let* in order for python-command to resolve reset-time and reset-statement
  (let* ((reset-time (format-time-string "%A, %B %e, %Y @ %-I:%M %p"))
         (reset-statement "Reset iPython on ")
         (python-command (concat "print('" reset-statement reset-time "')" )))
    ;; Reset iPython console
    (python-shell-send-string "%reset -f" "*Python*")
    ;; Print message to iPython console indicating reset
    (python-shell-send-string  python-command "*Python*")
    ;; Allow command to be called from another buffer
    (with-current-buffer "*Python*"
      (comint-clear-buffer))
    ;; Print message to minibuffer and *Messages*
    (message (concat reset-statement "%s") reset-time)))

;; Mimic default binding for comint-clear-buffer (C-c M-o)
(define-key inferior-python-mode-map (kbd "C-c M-l") 'my-reset-ipython)
Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67