将代码从Python缓冲区发送到从Mx ansiterm运行的IPython会话

2024-04-27 05:10:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 代码终端ipythonshell缓冲区emacsansiterm
3条回答

用python-模式.el在

M-x自定义变量RET py shell name RET ansi term

M-x ansi术语RET ipython RET

在ipythonshell中执行Python缓冲区中的C-C C-C

注意:默认py shell名称前面有一个shebang

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

(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

^{pr2}$

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

以下是如何使用它:

  1. 打开ansi-term缓冲区:

    M-xansi-termRET/usr/bin/ipythonRET

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

    M-xisend-associateRET*ansi-term*RET

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

相关问题 更多 >