wizzup
wizzup

Reputation: 2411

change IPython magic function prefix (from '%' to ':'), Anyway to?

Is there any way to change IPython magic function prefix from default '%' ? I can't found any option in ipython_config.py

Since I am using vim and ghci, I (somehow) trained myself to think for ':' as command prefix already.

This very annoying when I want to call magic function and automatic prefixing ':' to every IPython magic function call , e.g, :cd, :ed and :load

Upvotes: 1

Views: 322

Answers (1)

minrk
minrk

Reputation: 38608

The magic escape is hard-coded in a lot of places, but if all you want to do is minimize the penalty of your vim-inflicted muscle memory, you can tell the inputsplitter to treat your colons as percents:

import re
from IPython.core import splitinput
from IPython.core.inputsplitter import transform_escaped

# this is a one-character change, adding colon to the second group,
# so the line splitter will interpret ':' as an escape char
splitinput.line_split = line_split = re.compile("""
             ^(\s*)               # any leading space
             ([,;/%:]|!!?|\?\??)?  # escape character or characters
             \s*(%{0,2}[\w\.\*]*)     # function/method, possibly with leading %
                                  # to correctly treat things like '?%magic'
             (.*?$|$)             # rest of line
             """, re.VERBOSE)

# treat colon the same as percent:
transform_escaped.tr[':'] = transform_escaped._tr_magic

Now, you should be able to do things like:

:cd foo

for t in range(3):
    :time time.sleep(t)

If you want this to always fire, you can put this code in an IPython startup file (~/.ipython/profile_default/startup/whatever.py).

These are not exactly public APIs, so I wouldn't trust them to not mess anything up, but it seems to work in current master.

Upvotes: 5

Related Questions