如何在Emacs中清除IPython缓冲区?

7

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

3个回答

10

看起来 ipython 是基于 comint 的,这意味着以下代码应该适用于你 (使用M-x my-clear或你喜欢的按键绑定调用):

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

我在回答这个问题时提供了一些其他的选项。


请问您能否更新这个答案,以便它能够与C-l一起连接到python-shell-mode? - Kaunteya
1
@Kaunteya 最简单的方法是(add-hook 'inferior-python-mode-hook (lambda () (local-set-key (kbd "C-c l") 'my-clear))) 为了使其适用于任何继承自 compilation-shell-minor-mode 的解释器,并使用 John Wiegley 的 bind-key 包,可以使用 (bind-key (kbd "C-c l") 'my-clear compilation-shell-minor-mode-map) - pentavalentcarbon

7

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

它绑定到C-c M-o。

(comint-clear-buffer)

清除comint缓冲区。


comint 代表什么?回答也可以。 :) - agent18

1

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接