Repository: ipython/ipython

Issue:
%alias_magic to accept argument to %magic
It would be great to have an ability to pass argument to `%alias_magic`. For example, `%alias_magic h history -l 30` so I can just type `h` to get 30 lines of last history.



Discussion:
@joonro I agree, and this feature was intended to be added at a later date.  Places to update:
- [ ] `IPython.core.magic.MagicAlias`
- [ ] `IPython.core.magic.MagicsManager.register_alias`
- [ ] `IPython.core.magics.basic.BasicMagics.alias_magic`


Resolving patch:
diff --git a/IPython/core/magic.py b/IPython/core/magic.py
--- a/IPython/core/magic.py
+++ b/IPython/core/magic.py
@@ -426,7 +426,7 @@ def register_function(self, func, magic_kind='line', magic_name=None):
         setattr(self.user_magics, magic_name, func)
         record_magic(self.magics, magic_kind, magic_name, func)
 
-    def register_alias(self, alias_name, magic_name, magic_kind='line'):
+    def register_alias(self, alias_name, magic_name, magic_kind='line', magic_params=None):
         """Register an alias to a magic function.
 
         The alias is an instance of :class:`MagicAlias`, which holds the
@@ -452,7 +452,7 @@ def register_alias(self, alias_name, magic_name, magic_kind='line'):
             raise ValueError('magic_kind must be one of %s, %s given' %
                              magic_kinds, magic_kind)
 
-        alias = MagicAlias(self.shell, magic_name, magic_kind)
+        alias = MagicAlias(self.shell, magic_name, magic_kind, magic_params)
         setattr(self.user_magics, alias_name, alias)
         record_magic(self.magics, magic_kind, alias_name, alias)
 
@@ -653,9 +653,10 @@ class MagicAlias(object):
     Use the :meth:`MagicsManager.register_alias` method or the
     `%alias_magic` magic function to create and register a new alias.
     """
-    def __init__(self, shell, magic_name, magic_kind):
+    def __init__(self, shell, magic_name, magic_kind, magic_params=None):
         self.shell = shell
         self.magic_name = magic_name
+        self.magic_params = magic_params
         self.magic_kind = magic_kind
 
         self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name)
@@ -675,6 +676,10 @@ def __call__(self, *args, **kwargs):
                              "magic aliases cannot call themselves.")
         self._in_call = True
         try:
+            if self.magic_params:
+                args_list = list(args)
+                args_list[0] = self.magic_params + " " + args[0]
+                args = tuple(args_list)
             return fn(*args, **kwargs)
         finally:
             self._in_call = False
diff --git a/IPython/core/magics/basic.py b/IPython/core/magics/basic.py
--- a/IPython/core/magics/basic.py
+++ b/IPython/core/magics/basic.py
@@ -91,6 +91,10 @@ class BasicMagics(Magics):
         'target',
         help="""Name of the existing line or cell magic."""
     )
+    @magic_arguments.argument(
+        '-p', '--params', default=None,
+        help="""Parameters passed to the magic function."""
+    )
     @line_magic
     def alias_magic(self, line=''):
         """Create an alias for an existing line or cell magic.
@@ -118,7 +122,11 @@ def alias_magic(self, line=''):
 
           In [6]: %whereami
           Out[6]: u'/home/testuser'
+          
+          In [7]: %alias_magic h history -p "-l 30" --line
+          Created `%h` as an alias for `%history -l 30`.
         """
+
         args = magic_arguments.parse_argstring(self.alias_magic, line)
         shell = self.shell
         mman = self.shell.magics_manager
@@ -127,6 +135,12 @@ def alias_magic(self, line=''):
         target = args.target.lstrip(escs)
         name = args.name.lstrip(escs)
 
+        params = args.params
+        if (params and
+                ((params.startswith('"') and params.endswith('"'))
+                or (params.startswith("'") and params.endswith("'")))):
+            params = params[1:-1]
+
         # Find the requested magics.
         m_line = shell.find_magic(target, 'line')
         m_cell = shell.find_magic(target, 'cell')
@@ -147,17 +161,19 @@ def alias_magic(self, line=''):
             args.line = bool(m_line)
             args.cell = bool(m_cell)
 
+        params_str = "" if params is None else " " + params
+
         if args.line:
-            mman.register_alias(name, target, 'line')
-            print('Created `%s%s` as an alias for `%s%s`.' % (
+            mman.register_alias(name, target, 'line', params)
+            print('Created `%s%s` as an alias for `%s%s%s`.' % (
                 magic_escapes['line'], name,
-                magic_escapes['line'], target))
+                magic_escapes['line'], target, params_str))
 
         if args.cell:
-            mman.register_alias(name, target, 'cell')
-            print('Created `%s%s` as an alias for `%s%s`.' % (
+            mman.register_alias(name, target, 'cell', params)
+            print('Created `%s%s` as an alias for `%s%s%s`.' % (
                 magic_escapes['cell'], name,
-                magic_escapes['cell'], target))
+                magic_escapes['cell'], target, params_str))
 
     @line_magic
     def lsmagic(self, parameter_s=''):


Test patch:
diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py
--- a/IPython/core/tests/test_magic.py
+++ b/IPython/core/tests/test_magic.py
@@ -6,6 +6,7 @@
 
 import io
 import os
+import re
 import sys
 import warnings
 from unittest import TestCase
@@ -14,6 +15,8 @@
 
 import nose.tools as nt
 
+import shlex
+
 from IPython import get_ipython
 from IPython.core import magic
 from IPython.core.error import UsageError
@@ -867,6 +870,11 @@ def test_alias_magic():
     nt.assert_equal(ip.run_line_magic('env', ''),
                     ip.run_line_magic('env_alias', ''))
 
+    # Test that line alias with parameters passed in is created successfully.
+    ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
+    nt.assert_in('history_alias', mm.magics['line'])
+
+
 def test_save():
     """Test %save."""
     ip = get_ipython()