代码之家  ›  专栏  ›  技术社区  ›  sayth

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

  •  2
  • sayth  · 技术社区  · 9 年前

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

    • vim已安装
    • ipython和ipythonqtconsole都可以工作
    • zsh是最新的,更新了我的oh my zsh安装

    我将首选项导出到zsh

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

    然而,当我启动IPython然后调用%d魔法时,它失败了

    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
    

    我该如何实现这一点?

    1 回复  |  直到 9 年前
        1
  •  3
  •   clarity123    9 年前

    尝试使用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 :

    文件夹

    IPython使用存储在配置文件中的各种配置文件 IPYTHONDIR公司。生成默认配置文件并启动 配置IPython,执行“IPython profile create”,然后编辑“*_config”。py'文件位于IPYTHONDIR/profile_default中。

    IPYTHONDIR 根据 人类伊普顿 :

    IPYTHONDIR公司

    这是IPython存储其所有配置文件的位置。默认值为$HOME/。如果未定义IPYTHONDIR,则为ipython。

    您可以看到IPYTHONDIR的计算值 ipython locate .

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

    c.TerminalInteractiveShell.editor = ...
    

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

    c.IPythonWidget.editor = ...
    

    由于不同版本之间似乎有所不同,因此在生成默认配置文件后,请检查并查看它在IPython版本中的编写方式,并采取相应的措施。