将代码从Python缓冲区发送到从M-x ansi-term运行的IPython会话

时间:2012-11-18 22:10:57

标签: python emacs ipython

假设我使用M-x ansi-term在Emacs中打开终端模拟器。这将在Emacs中打开一个缓冲区,其中包含我选择的shell。说我然后从这个shell运行ipython。我可以使用Emacs中的Python代码从另一个缓冲区向此ipython会话发送代码吗?如果是这样的话?

3 个答案:

答案 0 :(得分:5)

我有一个辅助模式用于此目的(除了它不是特定于IPython的,我主要用于shell脚本):isend-mode

以下是您将如何使用它:

  1. 打开ansi-term缓冲区:

    M-X ansi-term RET /usr/bin/ipython RET

  2. 使用您要执行的代码打开缓冲区,并将其关联到解释器缓冲区:

    M-X isend-associate RET *ansi-term* RET

  3. 在python缓冲区中按 C-RET ,将当前行发送到ansi-term缓冲区中的解释器。

答案 1 :(得分:3)

(defun send-buffer-to-ipython ()
  "Send current buffer to the running ipython process."
  (interactive)
  (let* ((ipython-buffer "*ansi-term*") ; the buffer name of your running terminal
         (proc (get-buffer-process ipython-buffer)))
    (unless proc
      (error "no process found"))
    (save-buffer)
    (process-send-string proc
                         (format "execfile(\"%s\")\n" (buffer-file-name)))
    (pop-to-buffer ipython-buffer)      ; show ipython and select it
    ;; (display-buffer ipython-buffer)  ; show ipython but don't select it
    ))

然后将命令send-buffer-to-ipython绑定到您喜欢的任何键。我将其绑定到C-c C-c

(define-key python-mode-map [?\C-c ?\C-c] 'send-buffer-to-ipython)

答案 2 :(得分:2)

使用python-mode.el

M-x customize-variable RET py-shell-name RET ansi-term

M-x ansi-term RET ipython RET

来自Python缓冲区的C-c C-c比在IPython shell中执行

警告:shebang优先于默认的py-shell-name

https://launchpad.net/python-mode/trunk/6.0.12/+download/python-mode.el-6.0.12.tar.gz