gnome-terminal:在新选项卡中跟踪目录

gnome-terminal:在新选项卡中跟踪目录

我使用的是 Arch linux,当我打开一个新的终端选项卡时,它总是转到$HOME.如何才能使当我打开新选项卡时,它在我之前所在的目录中打开 shell?

答案1

有一个漏洞与此问题相关的

您需要做的就是将以下行添加到您的.bashrcor .zshrc

. /etc/profile.d/vte.sh

至少在 Arch 上,脚本会检查您是否正在运行 bash 或 zsh,如果没有则退出。

答案2

也可以交叉发帖来自超级用户的hacky解决方案:

[此]在每个命令之后将当前文件夹保存在文件中(在我看来不会造成太大伤害),并在保存的当前文件夹中打开一个新终端。

将以下内容添加到.zshrc[或者.bashrc]

# emulate bash PROMPT_COMMAND (only for zsh)
precmd() { eval "$PROMPT_COMMAND" }
# open new terminal in same dir
PROMPT_COMMAND='pwd > "${HOME}/.cwd"'
[[ -f "${HOME}/.cwd" ]] && cd "$(< ${HOME}/.cwd)"

请注意,打开新目录时,您也会将您放入上次使用的目录中窗户

答案3

@swalog 他的作品启发了我评论删除所有不必要的部分,同时vte.sh不修改提示或终端标题。请注意,我不使用zsh,因此我删除了zsh相关代码。

# Copyright © 2006 Shaun McCance <[email protected]>
# Copyright © 2013 Peter De Wachter <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# 28 Sep 2019: Tukusej’s Sirs modified this by stripping down all unnecessary parts for his usage
# (src: https://unix.stackexchange.com/questions/93476/gnome-terminal-keep-track-of-directory-in-new-tab#comment219157_93477)

# Not an interactive shell?
[[ $- == *i* ]] || return 0

# Not running under vte?
[ "${VTE_VERSION:-0}" -ge 3405 ] || return 0

__vte_urlencode() (
    # This is important to make sure string manipulation is handled
    # byte-by-byte.
    LC_ALL=C
    str="$1"
    while [ -n "$str" ]; do
        safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}"
        printf "%s" "$safe"
        str="${str#"$safe"}"
        if [ -n "$str" ]; then
            printf "%%%02X" "'$str"
            str="${str#?}"
        fi
    done
)

__vte_prompt_command() {
    local command=$(HISTTIMEFORMAT= history 1 | sed 's/^ *[0-9]\+ *//')
    command="${command//;/ }"
    local pwd='~'
    printf "\033]7;file://%s%s\007" "${HOSTNAME:-}" "$(__vte_urlencode "${PWD}")"
}

case "$TERM" in
    xterm*|vte*)
        [ -n "$BASH_VERSION" ] && PROMPT_COMMAND="${PROMPT_COMMAND};__vte_prompt_command"
        ;;
esac

相关内容