【问题标题】:how to clear ipython buffer in emacs?如何清除 emacs 中的 ipython 缓冲区?
【发布时间】:2011-11-08 17:57:22
【问题描述】:

我正在使用 ipython.el 在 emacs 缓冲区中运行和交互式 ipython shell。请问有什么办法可以清屏吗?由于它不在终端上运行,import os; os.system('CLS') 技巧将不起作用。谢谢。

【问题讨论】:

    标签: emacs ipython


    【解决方案1】:

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

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

    我在回复this question时发布了一些其他选项。

    【讨论】:

    • 您能否更新此答案,以便使用 C-l 连接到 python-shell-mode
    • @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)
    【解决方案2】:

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

    它绑定到 C-c M-o。

    (comint-clear-buffer)

    清除comint缓冲区。

    【讨论】:

    • comint 代表什么?答案虽然有效。 :)
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 2023-04-11
      • 2012-08-05
      • 1970-01-01
      相关资源
      最近更新 更多