Vte.sh does not keep the gnome-terminal directory in new tab

gnome-terminal

I used to keep my working directory when opening a new tab in gnome-terminal and want to restore this functionality. My research pointed me to sourcing /etc/profile.d/vte.sh in my ~/.zshrc (I use Z shell), however that does not change the problem, my new tabs still get opened in ~.

How can I restore this functionality? It can be a dirty hack if necessary.

My versions

~$ 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)

My ~/.zshrc (minimal example)

. /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

Best Answer

A very simple workaround for you would be including a function in your ~/.zshrc that remembers the working directory and changes into it when opening zsh:

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

__cd(){
    \cd "$@"
    pwd >/dev/shm/$USER-pwd
}
alias cd=__cd

We use /dev/shm instead of /tmp to avoid disk writes, although /tmp could already be a tmpfs on your system. \cd is used to avoid a fork bomb.

If you would like this feature for gnome-terminal only, you could include an if statement to check your current terminal or active window. Also, if you are concerned that other users might find out what was your last directory, you could modify the permissions for $USER-pwd with 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