更改IPython魔术函数前缀(从'%'到':'),无论如何?

wiz*_*zup 1 ipython

有没有办法从默认的'%'更改IPython魔术函数前缀?我在ipython_config.py中找不到任何选项

由于我使用的是vim和ghci,我(不知何故)训练自己将':'作为命令前缀.

当我想调用魔术函数并自动为每个IPython魔术函数调用前缀':'时,这非常烦人,例如:cd,:ed和:load

min*_*nrk 5

魔法逃脱在很多地方都是硬编码的,但是如果你想要做的就是尽量减少vim造成的肌肉记忆的惩罚,你可以告诉inputsplitter将你的冒号视为百分比:

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
Run Code Online (Sandbox Code Playgroud)

现在,您应该可以执行以下操作:

:cd foo

for t in range(3):
    :time time.sleep(t)
Run Code Online (Sandbox Code Playgroud)

如果您希望始终触发,可以将此代码放在IPython启动文件中(〜/ .ipython/profile_default/startup/whatever.py).

这些并不完全是公共API,所以我不相信它们不会弄乱任何东西,但它似乎在当前的主机中工作.