如何防止 emacs 使用 autojump 加载 `update_terminal_cwd`?

Ave*_*han 6 bash emacs macos

在 MacOSX 10.7 上,Apple 引入/etc/bashrc了一个函数:update_terminal_cwd检查 Apple Terminal 是否正在运行,以及我们是否不在 emacs 中(下面粘贴了代码段)。不幸的是,通过将 autojump 包含到我的 bashrc 配置文件中,调用劣质 shell(即M-x shell)会导致调用 update_terminal_cwd。对于在劣质 shell 中调用的任何命令,我都会得到以下输出:

bash: update_terminal_cwd: command not found
achan[10:29 AM]~ > echo $PROMPT_COMMAND
Run Code Online (Sandbox Code Playgroud)

当我在 bashrc 中包含 autojump 时echo $PROMPT_COMMAND,emacs 内部看起来像这样:

{ [[ "$AUTOJUMP_HOME" == "$HOME" ]] && (autojump -a "$(pwd -P)"&)>/dev/null 2>>"${AUTOJUMP_DATA_DIR}/.autojump_errors";} 2>/dev/null ; update_terminal_cwd;
Run Code Online (Sandbox Code Playgroud)

如果我不包括自动跳转,我只会得到一个空字符串echo $PROMPT_COMMAND

在自动跳转代码中,它插入了第一部分:

{ [[ "$AUTOJUMP_HOME" == "$HOME" ]] && (autojump -a "$(pwd -P)"&)>/dev/null 2>>"${AUTOJUMP_DATA_DIR}/.autojump_errors";} 2>/dev/null ;
Run Code Online (Sandbox Code Playgroud)

update_terminal_cwd刚刚通过附加被添加$PROMPT_COMMAND到上面的字符串的末尾。

不知何故,自动跳转正在一个环境中被实例化:

[ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]

是真的。但是,因为它是在 emacs 中运行的,所以没有定义 update_terminal_cwd;因此错误。

自动跳转人员建议我改用 iTerm2 或找出 $INSIDE_EMACS 不起作用的原因。

我怀疑它是这样的:

调用M-x shell以某种方式与调用 emacs 时已经存在的 shell 环境相关联。这就是为什么在 emacs 运行时更改 .bashrc 文件不会将更改传播到 emacs。相反,您必须创建一个新的 bash 环境,然后在看到这些更改之前使用劣质 shell 调用 emacs。

有没有办法告诉劣质 shell 忽略默认环境并尝试从头开始启动 bash?

苹果 /etc/bashrc

# System-wide .bashrc file for interactive bash(1) shells.
if [ -z "$PS1" ]; then
   return
fi

PS1='\h:\W \u\$ '
# Make bash check its window size after a process completes
shopt -s checkwinsize
# Tell the terminal about the working directory at each prompt.
if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
    update_terminal_cwd() {
        # Identify the directory using a "file:" scheme URL,
        # including the host name to disambiguate local vs.
        # remote connections. Percent-escape spaces.
    local SEARCH=' '
    local REPLACE='%20'
    local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
    printf '\e]7;%s\a' "$PWD_URL"
    }
    PROMPT_COMMAND="update_terminal_cwd; $PROMPT_COMMAND"
fi
Run Code Online (Sandbox Code Playgroud)

Bri*_*ift 0

M-x shell你可以通过以下方式获得一个具有相当空环境的 shell(以与 emacs 使用的相同选项启动):

exec -c bash --noediting -i
Run Code Online (Sandbox Code Playgroud)

或者您可以将上述命令放入~/.emacs_bash~/.emacs.d/init_bash.sh

如果这会删除太多环境,exec您可以$PROMPT_COMMAND在 .emacs_bash 启动脚本中设置为正确的值,而不是也许。