Redwood
Redwood

Reputation: 69322

In IPython how do I create aliases for %magics?

Let's say I want to create the alias %xed for %edit -x. How would I do it?

Upvotes: 7

Views: 2534

Answers (3)

iloveitaly
iloveitaly

Reputation: 2165

You can do this with %alias_magic

%alias_magic s suggestion -p "0"

This is the equivalent of running %suggestion 0 (for ipython autoimport). Without params passed to the magic, you can omit -p.

You can add this to your ipython startup:

c.InteractiveShellApp.exec_lines.append('%alias_magic s suggestion -p "0"')

Upvotes: 1

Carl Younger
Carl Younger

Reputation: 3070

The answer given above uses the old magic system. get_ipython().expose_magic is dead. You now just import and use decorators for all this.

See here for more details.

Upvotes: 3

Igal Serban
Igal Serban

Reputation: 10684

Update: The first response( below) does not accept parameters. So put this snippet at the end of the ipy_user_conf.py file ( it is in your home directory ).

def ed_xed(self,arg):
    ip = self.api
    return ip.magic.im_class.magic_edit(ip.IP," -x %s "%arg)

ip.expose_magic('xed',ed_xed)

Before update: Does it has to be %magic? You can use the macro and store magic to reproduce this behavior without the magic %.

In [5]: %edit -x
In [6]: macro xed 5
In [7]: store xed
In [8]: xed

for magic alias from the documentation ( %magic? ):

You can also define your own aliased names for magic functions. In your ipythonrc file, placing a line like:

execute IPYTHON.magic_pf = IPYTHON.magic_profile

will define %pf as a new name for %profile.

But I don't know how too add the parameter.

Upvotes: 2

Related Questions