1

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

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

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

4

1 回答 1

5

魔术转义在很多地方都是硬编码的,但如果你想做的只是尽量减少 vim 造成的肌肉记忆的损失,你可以告诉输入分割器将你的冒号视为百分比:

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

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

:cd foo

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

如果您希望它始终触发,您可以将此代码放入 IPython 启动文件 (~/.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.

于 2013-01-01T21:49:27.830 回答