6

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
3
  • 1
    I forgot to mention that, but roxterm, which is very similar to gnome-terminal, has this functionality by default. On Debian/Ubuntu, the package to install is roxterm-gtk3 or roxterm-gtk2. May 20, 2015 at 15:05
  • Do you use a custom command to start zsh? Then it might be like my problem.
    – qznc
    Aug 3, 2016 at 11:10
  • @qznc: As of 2022, including vte.sh now works with and without custom command, however only for new tabs, not windows. Sep 13, 2022 at 6:59

3 Answers 3

4
+50

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
8
  • 1
    But that's shared between all terminals, and all users, right? Nov 22, 2014 at 18:19
  • 1
    @VolkerSiegel Your .zshrc file will not affect other users. See the updated answer. Nov 22, 2014 at 18:55
  • 1
    Sure, the .zshrc is local, but all are sharing the same file /dev/shm/pwd to store the directory, isn't it? The "all" is all terminals, and all users. Regarding users: If you are the only one using it, that's no user conflict - but who knows, someone else on your system might copy your .zshrc, because it's good! Or source it... Regarding all terminals - it can behave very confusing. Your last cd may have been hours ago in a different window, deep into some tree. That's where your second tab will open, right? Nov 22, 2014 at 19:35
  • 1
    Regarding /dev/shm/pwd, he could just replace it with ~/.pwd if that's a problem. For all the terminals, there's not much I can do, I'm afraid. Perhaps pwd >/dev/shm/pwd could be included in the __precmd. Nov 22, 2014 at 20:17
  • 1
    You could also use /dev/shm/$USER-pwd, to keep that shm optimisation (~ can be on a slow remote drive, so it makes sense.) With the terminal of KDE, konsole, you can easily do things per window, or per tab, as they are all identifiable by environment variables. Do a printenv | sort | grep '^KONSOLE_\|SHELL_SESSION_ID' in konsole - it shows KONSOLE_DBUS_SERVICE=:1.78, KONSOLE_DBUS_SESSION=/Sessions/503, KONSOLE_DBUS_WINDOW=/Windows/498, KONSOLE_PROFILE_NAME=Shell, SHELL_SESSION_ID=30c0547e27e54000a7558ce28eec67c3. (There is no such feature in gnome-terminal) Nov 23, 2014 at 1:12
1

I'm not sure what exactly you want to restore (see below);

If you want to open gnome-terminal starting in a different directory, there is an option to specify the directory:

gnome-terminal --working-directory=/tmp

You could certainly use a script as custom startup command, which does a cd, and then starts the shell - but that does not sound like a good approach.


If it is about the default directory of the shell in a new tab being set to the current directory of the tab that was current when the new tab action was triggered: That just works for me, so I can only give hints - I run an older version, GNOME Terminal 3.6.2.

I'm pretty sure it's not the script /etc/profile.d/vte.sh that sets the directory; In my version - where it works fine - the script does not contain code related to that, and I have never used it.

The terminal program itself can just set the directory before forking the shell process.
But actually, it does not even need to do that - you want to have the same directory like before. All that is needed it not to touch the directory at all.

That said, there is a bug describing this problem:
gnome-terminal: Bug 697475 - New tab is not opened in same directory as previous tab
The discussion is very long, I do not have a summary; vte.sh is discussed there, and from skipping through, it looks like it may be fixed, but not fully released yet.

2
  • Yes, that Bug is the same that I have, however the fix proposed there to include vte.sh does not work in my case. Nov 22, 2014 at 14:47
  • 1
    I did not read all comments - did you find out what is the point of this vte.sh - in which way, roughly, is it related to the problem? Nov 22, 2014 at 18:17
0

Update from 2022:

  • including /etc/profile.d/vte.sh in ~/.zshrc does work now, however only for new tabs, not for new windows
  • the workaround from @admirabilis repeated below used to work but it now breaks directory completion with the tab key
  • both points above are true when configuring zsh as a "custom command" in Gnome Terminal as well as when setting zsh as default shell using chsh
cd $(<>/dev/shm/$USER-pwd)

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

Versions

Interestingly, both Gnome Terminal and ZSH are still on the same major version as 8 years before.

~$ uname -a
Linux archlinux 5.19.5-arch1-1 #1 SMP PREEMPT_DYNAMIC Mon, 29 Aug 2022 15:51:05 +0000 x86_64 GNU/Linux
~$ gnome-terminal --version
# GNOME Terminal 3.44.1 using VTE 0.68.0 +BIDI +GNUTLS +ICU +SYSTEMD
~$ zsh --version
zsh 5.9 (x86_64-pc-linux-gnu)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .