3

I'd like to rename %paste to something like %pp so that it takes fewer keystrokes. I worked out a way to do that but it seems complicated. Is there a better way?

def foo(self, bar):
    get_ipython().magic("paste")

get_ipython().define_magic('pp', foo)
ceiling cat
  • 5,501
  • 9
  • 38
  • 51
  • Try to use alias for such function, see http://stackoverflow.com/questions/13511386/python-built-in-functions-vs-magic-functions-and-overriding – Mihai8 Feb 05 '13 at 20:18

2 Answers2

5

From IPython 0.13, there's a new %alias_magic magic function, which you would use as:

%alias_magic pp paste
Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • If I exit IPython and start it again I lose the alias. Do you know a way to make the alias persist across IPython sessions? – Noah_S May 21 '18 at 16:36
  • You could put it in a startup script: http://ipython.readthedocs.io/en/stable/interactive/tutorial.html#startup-files – Thomas K May 23 '18 at 09:32
  • The problem is that "%alias_magic pp paste" isn't valid python syntax. This results in a syntax error when starting ipython: ```echo "%alias_magic pp paste" > ~/.ipython/profile_default/startup/paste_alias.py``` – Noah_S May 23 '18 at 23:23
  • The answer I'm looking for is something like ```from IPython.core.magic import MagicsManager; manager = MagicsManager(); manager.register_alias('pp', 'paste')``` but that is not quite right – Noah_S May 23 '18 at 23:34
  • 1
    `get_ipython().magics_manager().register_alias('pp', 'paste')` should do it. You can also use magics in a startup file by giving it a `.ipy` extension. – Thomas K May 25 '18 at 18:31
  • 1
    Great, thank you! FYI I got "magics manager not callable" and changed it to this to work: `get_ipython().magics_manager.register_alias('pp', 'paste')` – Noah_S May 25 '18 at 18:49
3

use %alias magic to do it (if you want it to be permanent use %store):

In [8]: %alias??

"""Define an alias for a system command.

   '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
   ... 
root
  • 76,608
  • 25
  • 108
  • 120