Alex Gaudio

IPython Shell Integration

This post shows you how to take IPython’s sweet bash/shell integration to another level. Of particular interest is my last example, which utilizes IPython’s syntax and bash integration to create a scripting language that supports pure Python + IPython features + bash (as supported by IPython).

First off, some distinctions: IPython has aliases, macros, and magics. They are different things.

Aliases are shortcuts to bash commands that get piped out to the os in a subprocess. One alias is a tuple. ie. (“showmydirectory”, “ls”).

Macros are shortcuts to python code stored as a string. They are editable via the ‘%edit’ magic command

Magics are functions special to IPython that take parameters and utilize IPython internals to do helpful things. They can be identified by the ‘%’ at the beginning.

On to some code:

Import everything within your path that is executable as an IPython alias. This means you can use bash commands like ‘git status’ or ‘tail -f somefilename

grep something’ without explicitly using the ‘!’ to define a bash command.