46

I'm on Arch linux, and when I open a new terminal tab, it always goes to $HOME. How can I make it so that when I open a new tab, it opens the shell in the directory I was in previously?

korylprince
  • 1,831
  • 2
  • 14
  • 12

3 Answers3

63

There is a bug related to this issue

All you need to do is add the following line to your .bashrc or .zshrc:

. /etc/profile.d/vte.sh

At least on Arch, the script checks if you are running either bash or zsh and exits if you are not.

korylprince
  • 1,831
  • 2
  • 14
  • 12
  • 3
    It should be noted that this might not have an effect, unless it is added after export PROMPT_COMMAND=..., if such a thing already exists in your .bashrc. – swalog Jun 12 '14 at 13:26
  • 2
    /etc/profile.d/vte.sh overrides the PROMPT_COMMAND variable. To fix this, you can modify vte.sh, and change the part with PROMPT_COMMAND="__vte_prompt_command" to PROMPT_COMMAND="${PROMPT_COMMAND};__vte_prompt_command" – swalog Jun 15 '14 at 22:06
  • 2
    I've been careful to put this in the right place, check that the script exists, etc, but it does not have the desired effect. I'm running 4.2.3-1-ARCH with Gnome Terminal 3.18.1. Any suggestions? – Jack Senechal Oct 19 '15 at 23:27
  • This doesn't work for me for zsh when adding it to my .zshrc. I'm using oh-my-zsh, not sure if that is related. – Andreas Mueller Jan 21 '16 at 20:03
  • 1
    I'm using oh-my-zsh. I've up to date on arch as of at most a couple weeks. Check out my dotfiles repo to compare: https://github.com/korylprince/dotfiles – korylprince Jan 22 '16 at 00:49
  • @humanityANDpeace sorry to hear that. It looks like zsh doesn't suffer from this issue. – korylprince Aug 17 '16 at 00:20
  • @korylprince I have to retract my earlier comment, after having looked into the matter more (indeed after patching the source of gnome-terminal to guess the cwd from the /proc/$SHELLSPID/@cwd) to find eventually some more background. The solution you gave is the very right way after all. Even though the word bug or regression comes up with the change, it is indeed somewhat advantages compared to tracking the PID of the shell subprocess' PID. Gnome VTE project (internally used by gnome-terminal) tracks CWDchanges via reading encoded data emited by the shell prompt,hence the.bashrc – humanityANDpeace Aug 17 '16 at 08:14
  • @humanityANDpeace good to hear! Sounds like you had an adventure :) – korylprince Aug 18 '16 at 02:44
  • Unfortunately, this didn't work for me. Perhaps it's outdated? – sigvaldm May 29 '18 at 05:48
  • @sigvaldm I'm not sure what to tell you. I'm using vte.sh from the vte-common arch linux package. I'm using zsh, and sourcing the vte.sh file in .zshrc makes the difference in tabs working correctly. I'm using GNOME Terminal 3.28.1 using VTE 0.52.1 +GNUTLS. You can see my full dot files at https://github.com/korylprince/dotfiles – korylprince Jun 01 '18 at 01:40
  • @korylprince I'm sorry, this was all my bad. I added vte.sh to .zshrc, closed the window, and tried File -> Open Terminal from another window which I'd forgotten to close (feeling silly). It works now :) – sigvaldm Jun 01 '18 at 07:03
  • It worked for me! Thanks a lot it will save a lot of frustration at work! Can someone explain why this fix works? – Llopeth Oct 15 '21 at 09:41
  • This happens in the __vte_osc7 function (for zsh) in /etc/profile.d/vte.sh. It prints an escape sequence that gnome-terminal uses to set the directory. – korylprince Oct 19 '21 at 03:57
10

Might as well crosspost this hacky solution from superuser:

[This] saves the current folder in a file, after every command (Doesn't hurt too much IMO) and opens a new terminal in the saved current folder.

add the following to .zshrc [or .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)"

Note that this you will also put you in your last-used directory when opening a new window.

xjcl
  • 341
0

@swalog inspired me in his comment to strip down all unnecessary parts of the vte.sh while not modifying the the prompt nor the terminal title. Note that I don’t use zsh, therefore I removed zsh-related code.

# Copyright © 2006 Shaun McCance <shaunm@gnome.org>
# Copyright © 2013 Peter De Wachter <pdewacht@gmail.com>
#
# 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