vte.sh 不将我的 gnome-terminal 目录保留在新选项卡中

Kon*_*ner 6 gnome-terminal

我曾经在 gnome-terminal 中打开新选项卡时保留我的工作目录,并希望恢复此功能。我的研究指出我/etc/profile.d/vte.sh在我的~/.zshrc(我使用 Z shell)中采购,但这并没有改变问题,我的新标签仍然在~.

如何恢复此功能?如有必要,它可能是一个肮脏的黑客。

我的版本

~$ uname -a
Linux konradslaptop2 3.17.2-1-ARCH #1 SMP PREEMPT Thu Oct 30 20:49:39 CET 2014 x86_64 GNU/Linux
~$ gnome-terminal --version
GNOME-Terminal 3.14.2
~$ zsh --version
zsh 5.0.7 (x86_64-unknown-linux-gnu)
Run Code Online (Sandbox Code Playgroud)

我的 ~/.zshrc (最小示例)

. /etc/profile.d/vte.sh
# auto generated by .zsh installation
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then       
    function zle-line-init () {
        printf '%s' "${terminfo[smkx]}"
    }
    function zle-line-finish () {
        printf '%s' "${terminfo[rmkx]}"
    }
    zle -N zle-line-init
    zle -N zle-line-finish
fi
Run Code Online (Sandbox Code Playgroud)

Ter*_*ior 4

对您而言,一个非常简单的解决方法是在您的文件中包含一个函数~/.zshrc,该函数会记住工作目录并在打开 zsh 时对其进行更改:

cd $(<>/dev/shm/$USER-pwd)

__cd(){
    \cd "$@"
    pwd >/dev/shm/$USER-pwd
}
alias cd=__cd
Run Code Online (Sandbox Code Playgroud)

我们使用/dev/shm而不是/tmp来避免磁盘写入,尽管/tmp可能已经是系统上的tmpfs 。\cd用于避免叉子炸弹

如果您只想在 gnome-terminal 中使用此功能,则可以包含 if 语句来检查当前终端或活动窗口。另外,如果您担心其他用户可能会发现您的最后一个目录是什么,您可以修改$USER-pwd权限chmod

if xprop -id $(xprop -root 32x ' $0' _NET_ACTIVE_WINDOW | awk '{print $NF}') WM_CLASS | grep -q gnome-terminal; then
    cd    $(<>/dev/shm/$USER-pwd)
    chmod 600 /dev/shm/$USER-pwd

    __cd(){
        \cd "$@"
        pwd >/dev/shm/$USER-pwd
    }
    alias cd=__cd
fi
Run Code Online (Sandbox Code Playgroud)