In IPython, how do I create aliases for% magics?

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

+4
source share
2 answers

Update: The first answer (below) does not accept parameters. Therefore, put this snippet at the end of ipy_user_conf.py (it is located 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 the upgrade: Should it be% magic? You can use a macro and store magic to reproduce this behavior without% magic.

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

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

You can also define your own alias names for magic functions. In your ipythonrc, putting the line as:

execute IPYTHON .magic_pf = IPython .magic_profile

will define% pf as the new name for% Profile.

But I do not know how to add a parameter.

+2
source

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

See here for more details.

+3
source

All Articles