Vim editor - zsh shell ipython magic

2019-07-18 20:31发布

I am trying to use the %ed magic in IPython to use vim as the editor.

  • vim is installed
  • ipython and ipython qtconsole both work
  • zsh is latest as updated with my oh-my-zsh install

I exported my prefernce to zsh

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

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

However, when I launch IPython and then invoke the %ed magic it fails

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

How do I get this to work?

1条回答
霸刀☆藐视天下
2楼-- · 2019-07-18 20:55

Try using IPython's profile configuration as a means of specifying editor. To do this:

Firstly, generate default configuration files:

$ ipython profile create

Next, find your ~/.ipython/profile_default/..._config.py file to edit. For example on IPython 2.4.1,

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

Find the commented-out .editor setting, un-comment, and set it to vim. For example in IPython 2.4.1, this would look like

c.TerminalInteractiveShell.editor = 'vim'

You will now find when you start IPython, you can %ed and it will call up 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]:

Explanation

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 according to 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.

Also I mention the version because settings seem different across some versions, for 2.4.1 the setting is called:

c.TerminalInteractiveShell.editor = ...

Whereas in the answer given at IPython setting text editor, this setting was named differently:

c.IPythonWidget.editor = ...

Since it seems different between versions, after you generate the default configuration files, check and see how it is written in your IPython version, and act accordingly.

查看更多
登录 后发表回答