Configuring Sublime Text 3 as IPython Editor on Mac OS

Sunday, July 21, 2019

One powerful feature of IPython is its integration with text editors. On Mac and Linux the default editor is vim.

If you are using Sublime Text 3 as your editor, here's how to configure it to be called when you run the edit magic command in an iPython session.

Step 1

Make a symlink to subl (which is Sublime Text's tool to work with files on the command line).

ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl

The above assumes that you have installed Sublime Text in the Applications folder and you have set the ~/bin folder in your path.

Step 2

Edit your .bash_profile file as follows :

PATH=$HOME/bin:$PATH

EDITOR='subl -w'

To test your configuration open a terminal and start ipython. We will try out the example from the iPython documentation :

In [1]: edit
Editing... done. Executing edited code...
Out[1]: 'def foo():\n    print "foo() was defined in an editing
session"\n

We can then call the function foo() :

In [2]: foo()
foo() was defined in an editing session

Now we edit foo. IPython automatically loads the editor with the (temporary) file where foo() was previously defined:

In [3]: edit -p
Editing... done. Executing edited code...

And if we call foo() again we get the modified version:

In [4]: foo()
foo() has now been changed!

Reference :

  1. https://ipython.readthedocs.io/en/stable/config/details.html#editor-configuration
  2. https://www.sublimetext.com/docs/3/osx_command_line.html

Comments

There are currently no comments

New Comment

In the interest of keeping comments relevant to the subject posted, please note all comments will be moderated. Comments which are out of topic will be deleted.

required

required (not published)

optional

captcha