Vim 编辑器 - zsh shell ipython magic %ed 找不到编辑器

Vim editor - zsh shell ipython magic %ed cannot find editor

我正在尝试使用 IPython 中的 %ed 魔法来使用 vim 作为编辑器。

我将我的偏好导出到 zsh

$ echo "export EDITOR=/usr/bin/vim" >> ~/.zshrc

$ echo "export VISUAL=/usr/bin/vim" >> ~/.zshrc

但是,当我启动 IPython 然后调用 %ed 魔法时,它失败了

In [1]: %ed
IPython will make a temporary file named: /tmp/ipython_edit_pu4Yql.py
Editing.../bin/sh: 1: mvim: not found
WARNING: Could not open editor

如何让它工作?

尝试使用 IPython 的配置文件配置作为指定编辑器的方法。为此:

首先,生成默认配置文件:

$ ipython profile create

接下来,找到要编辑的 ~/.ipython/profile_default/..._config.py 文件。例如 IPython 2.4.1,

$ vim ~/.ipython/profile_default/ipython_config.py

找到注释掉的.editor设置,取消注释,设置为vim。例如在 IPython 2.4.1 中,这看起来像

c.TerminalInteractiveShell.editor = 'vim'

你现在会发现当你启动IPython时,你可以%ed它会调用vim:

$ ipython
Python 2.7.11+ (default, Feb 22 2016, 16:38:42)
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: %ed
IPython will make a temporary file named: /tmp/ipython_edit_Tze8Ur/ipython_edit
_gghQG5.py
Editing... done. Executing edited code...
It works
Out[1]: 'print "It works"\n'

In [2]:

说明

man ipython:

FILES

IPython uses various configuration files stored in profiles within IPYTHONDIR. To generate the default configuration files and start configuring IPython, do 'ipython profile create', and edit '*_config.py' files located in IPYTHONDIR/profile_default.

IPYTHONDIR 根据 man ipython:

IPYTHONDIR

This is the location where IPython stores all its configuration files. The default is $HOME/.ipython if IPYTHONDIR is not defined.

You can see the computed value of IPYTHONDIR with ipython locate.

我还提到了版本,因为某些版本的设置似乎有所不同,对于 2.4.1,设置称为:

c.TerminalInteractiveShell.editor = ...

而在 IPython setting text editor 给出的答案中,此设置的名称不同:

c.IPythonWidget.editor = ...

由于版本之间似乎有所不同,生成默认配置文件后,请查看它在您的IPython版本中是如何编写的,并据此进行操作。