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

相關內容