Inheritance diagram for IPython.core.magics.code:
Implementation of code management magic functions.
Magics related to code management (loading, saving, editing, ...).
Print docstring if incorrect arguments were passed
Make an entry in the options_table for fn, with value optstr
Alias to %edit.
Bring up an editor and execute the resulting code.
%edit runs IPython’s editor hook. The default version of this hook is set to call the editor specified by your $EDITOR environment variable. If this isn’t found, it will default to vi under Linux/Unix and to notepad under Windows. See the end of this docstring for how to change the editor hook.
You can also set the value of this editor via the TerminalInteractiveShell.editor option in your configuration file. This is useful if you wish to use a different editor from your typical default with IPython (and for Windows users who typically don’t set environment variables).
This command allows you to conveniently edit multi-line code right in your IPython session.
If called without arguments, %edit opens up an empty editor with a temporary file and will execute the contents of this file when you close it (don’t forget to save it!).
-n <number>: open the editor at a specified line number. By default, the IPython editor hook uses the unix syntax ‘editor +N filename’, but you can configure this by providing your own modified hook if your favorite editor supports line-number specifications with a different syntax.
-p: this will call the editor with the same data as the previous time it was used, regardless of how long ago (in your current session) it was.
-r: use ‘raw’ input. This option only applies to input taken from the user’s history. By default, the ‘processed’ history is used, so that magics are loaded in their transformed version to valid Python. If this option is given, the raw input as typed as the command line is used instead. When you exit the editor, it will be executed by IPython’s own processor.
-x: do not execute the edited code immediately upon exit. This is mainly useful if you are editing programs which need to be called with command line arguments, which you can then do using %run.
If arguments are given, the following possibilities exist:
Note: opening at an exact line is only supported under Unix, and some editors (like kedit and gedit up to Gnome 2.8) do not understand the ‘+NUMBER’ parameter necessary for this feature. Good editors like (X)Emacs, vi, jed, pico and joe all do.
After executing your code, %edit will return as output the code you typed in the editor (except when it was an existing file). This way you can reload the code in further invocations of %edit as a variable, via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of the output.
Note that %edit is also available through the alias %ed.
This is an example of creating a simple function inside the editor and then modifying it. First, start up the editor:
In : ed Editing... done. Executing edited code... Out: 'def foo():\n print "foo() was defined in an editing session"\n'
We can then call the function foo():
In : foo() foo() was defined in an editing session
Now we edit foo. IPython automatically loads the editor with the (temporary) file where foo() was previously defined:
In : ed foo Editing... done. Executing edited code...
And if we call foo() again we get the modified version:
In : foo() foo() has now been changed!
Here is an example of how to edit a code snippet successive times. First we call the editor:
In : ed Editing... done. Executing edited code... hello Out: "print 'hello'\n"
Now we call it again with the previous output (stored in _):
In : ed _ Editing... done. Executing edited code... hello world Out: "print 'hello world'\n"
Now we call it with the output #8 (stored in _8, also as Out):
In : ed _8 Editing... done. Executing edited code... hello again Out: "print 'hello again'\n"
Changing the default editor hook:
If you wish to write your own editor hook, you can put it in a configuration file which you load at startup time. The default hook is defined in the IPython.core.hooks module, and you can use that as a starting example for further modifications. That file also has general instructions on how to set a new hook for use once you’ve defined it.
Format a string for latex inclusion.
Load code into the current frontend.
%load [options] source
where source can be a filename, URL, input history range or macro
Alias of %load
%loadpy has gained some flexibility and droped the requirement of a .py extension. So it has been renamed simply into %load. You can look at %load‘s docstring for more info.
Parse options passed to an argument string.
The interface is similar to that of getopt(), but it returns back a Struct with the options as keys and the stripped argument string still as a string.
arg_str is quoted as a true sys.argv vector by using shlex.split. This allows us to easily expand variables, glob files, quote arguments, etc.
-mode: default ‘string’. If given as ‘list’, the argument string is returned as a list (split on whitespace) instead of a string.
-list_all: put all option values in lists. Normally only options appearing more than once are put in a list.
-posix (True): whether to split the input line in POSIX mode or not, as per the conventions outlined in the shlex module from the standard library.
Upload code to Github’s Gist paste bin, returning the URL.
The argument can be an input history range, a filename, or the name of a string or macro.
- -d: Pass a custom description for the gist. The default will say
- “Pasted from IPython”.
Save a set of lines or a macro to a given filename.
-r: use ‘raw’ input. By default, the ‘processed’ history is used, so that magics are loaded in their transformed version to valid Python. If this option is given, the raw input as typed as the command line is used instead.
-f: force overwrite. If file exists, %save will prompt for overwrite unless -f is given.
This function uses the same syntax as %history for input ranges, then saves the lines to the filename you specify.
It adds a ‘.py’ extension to the file if you don’t do so yourself, and it asks for confirmation before overwriting existing files.
If -r option is used, the default extension is .ipy.