我的.bashrc中有以下内容:
bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forward
但是,当我从Emacs中调用shell
时,我收到以下消息:
bash:bind:warning:未启用行编辑
bash:bind:warning:未启用行编辑
结果我的提示搞砸了。
我如何检测(从我的.bashrc中),我的shell是从emacs调用的,或者是不是从标准终端调用?
我的目标是将调用包装到bind
,以便它们只在适当的终端中执行。
在Emacs 25和bash 4.2下,探测一个名为EMACS
的变量对我不起作用。
但是,在Emacs内外寻找shell环境的差异时,我发现了一个名为INSIDE_EMACS
的变量,只有在从Emacs运行时才会设置。
因此,对我有用的解决方案是:
if [[ ! -v INSIDE_EMACS ]]; then
bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forward
fi
回应INSIDE_EMACS
返回Emacs版本号。
bash
禁用行编辑,因为它在其环境中看到名为EMACS
的变量。您可以使用相同的变量来有条件地创建这些绑定:
if [[ ! -v EMACS ]]; then
bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forward
fi
此代码段专门测试是否在bash中启用了行编辑。它适用于所有地方,而不仅仅是在emacs shell中:
if [[ "$(set -o | grep 'emacs\|\bvi\b' | cut -f2 | tr '\n' ':')" != 'off:off:' ]]; then
echo "line editing is on"
fi
它可能会简化......