如何在emacs中清除python缓冲区



我正在使用ipython.el在emacs缓冲区中运行交互式python shell。我想知道是否有办法清除屏幕?因为它不是在终端上运行,所以import os; os.system('CLS')技巧将不起作用。谢谢。

看起来ippython是基于comint的,这意味着下面的代码应该为你工作(用M-x my-clear或你喜欢的键绑定调用):

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

我贴了一些其他的选项来回答这个问题

C-c M-o运行命令comint-clear-buffer(在internal -python-mode-map),这是一个交互式编译Lisp函数。

与C-c - M-o结合

(comint-clear-buffer)

清空联合缓冲区

清除屏幕当前会话变量(使用%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)

最新更新