如何将 bash 别名移植到 ipython > 0.10?

Posted

技术标签:

【中文标题】如何将 bash 别名移植到 ipython > 0.10?【英文标题】:How do you port bash aliases to ipython > 0.10? 【发布时间】:2011-11-26 22:12:21 【问题描述】:

如何将 bash 别名移植到 IPython 版本 >= 0.11?

IPython

http://ipython.scipy.org/Wiki/tips

【问题讨论】:

这看起来不错所以..有什么问题? Alex,最好在问题中提出你的问题,然后写下你的答案作为这个问题的答案——如果没有其他人提供更好的方法,那么接受你的答案:-) 你想把它放在 IPython wiki 上吗? wiki.ipython.org/Cookbook/Moving_config_to_IPython_0.11 您可以将链式 .replace('=',' ').replace('"','').replace("'",'') 替换为单个 string.translate(s,table,deletechars) 感谢 wim、Sean、Thomas 和 smci 的 cmets。我已经跟踪他们了 【参考方案1】:

这是我的解决方案。欢迎改进!

在您的 ipython 配置中,添加以下行:(我的在这里:~/.config/ipython/profile_default/ipython_config.py)

c = get_config()

## Port bash aliases to ipython
import os, string
a = os.popen("bash -l -c 'alias'").read()
a = a.translate(string.maketrans("=", ' '), '\'"').split('alias ')
a = [tuple(x.strip().split(' ', 1)) for x in a]
c.AliasManager.user_aliases = [x for x in a if len(x) == 2]

【讨论】:

如果你在另一个文件中定义了别名,你需要做os.popen("bash -lc '. /the/file/where/you/have/aliases && alias'").read()。这也适用于 zsh。 这在ipython 0.13.2中不起作用,函数get_config()不存在。 重要的是要提一下:我的 ~/.bashrc 已设置为在非交互模式下不会执行,因此"bash -lc" 没有返回任何内容。如果你有同样的问题,试试这个os.popen("bash -lci alias")-i 选项用于交互模式,使 bash 的工作如常。

以上是关于如何将 bash 别名移植到 ipython > 0.10?的主要内容,如果未能解决你的问题,请参考以下文章

如何将 IPython 历史记录到文本文件?

别名 g='git' 并且 bash 完成仍然有效 [重复]

将 IPython 变量作为参数传递给 bash 命令

怎样将AI paddle 进行移植使用,paddle项目复制到pycharm中,sciview,NameError: name ‘get_ipython‘ is not defined

sh Bash:别名,echo:将天气预报别名追加到〜/ .bashrc

bash中的别名的多个命令,当第一个是ssh时