You are viewing an older version (9.10.0). Go to latest (9.13.0)
{ } Raw JSON

bundles / IPython 9.10.0 / IPython / core / magics / osm / OSMagics / alias

function

IPython.core.magics.osm:OSMagics.alias

source: /IPython/core/magics/osm.py :94

Signature

def   alias ( self parameter_s = '' )

Summary

Define an alias for a system command.

Extended Summary

'%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'

Then, typing 'alias_name params' will execute the system command 'cmd params' (from your underlying operating system).

Aliases have lower precedence than magic functions and Python normal variables, so if 'foo' is both a Python variable and an alias, the alias can not be executed until 'del foo' removes the Python variable.

You can use the %l specifier in an alias definition to represent the whole line when the alias is called. For example

In [2]: alias bracket echo "Input in brackets: <%l>"
In [3]: bracket hello world
Input in brackets: <hello world>

You can also define aliases with parameters using %s specifiers (one per parameter)

In [1]: alias parts echo first %s second %s
In [2]: %parts A B
first A second B
In [3]: %parts A
Incorrect number of arguments: 2 expected.
parts is an alias to: 'echo first %s second %s'

Note that %l and %s are mutually exclusive. You can only use one or the other in your aliases.

Aliases expand Python variables just like system calls using ! or !! do: all expressions prefixed with '$' get expanded. For details of the semantic rules, see PEP-215: https://peps.python.org/pep-0215/. This is the library used by IPython for variable expansion. If you want to access a true shell variable, an extra $ is necessary to prevent its expansion by IPython

In [6]: alias show echo
In [7]: PATH='A Python string'
In [8]: show $PATH
A Python string
In [9]: show $$PATH
/usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...

You can use the alias facility to access all of $PATH. See the %rehashx function, which automatically creates aliases for the contents of your $PATH.

If called with no parameters, %alias prints the current alias table for your system. For posix systems, the default aliases are 'cat', 'cp', 'mv', 'rm', 'rmdir', and 'mkdir', and other platform-specific aliases are added. For windows-based systems, the default aliases are 'copy', 'ddir', 'echo', 'ls', 'ldir', 'mkdir', 'ren', and 'rmdir'.

You can see the definition of alias by adding a question mark in the end

In [1]: cat?
Repr: <alias cat for 'cat'>

Aliases

  • IPython.core.magics.OSMagics.alias