.bashrc:如何检查shell运行的终端

问题描述 投票:2回答:3

我的.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,以便它们只在适当的终端中执行。

bash shell emacs
3个回答
2
投票

在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版本号。


1
投票

bash禁用行编辑,因为它在其环境中看到名为EMACS的变量。您可以使用相同的变量来有条件地创建这些绑定:

if [[ ! -v EMACS ]]; then
    bind '"\e[A"':history-search-backward
    bind '"\e[B"':history-search-forward
fi

0
投票

此代码段专门测试是否在bash中启用了行编辑。它适用于所有地方,而不仅仅是在emacs shell中:

if [[ "$(set -o | grep 'emacs\|\bvi\b' | cut -f2 | tr '\n' ':')" != 'off:off:' ]]; then
  echo "line editing is on"
fi

它可能会简化......

© www.soinside.com 2019 - 2024. All rights reserved.