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?

3 Answers 3

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.

13
  • 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, 2014 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, 2014 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? Oct 19, 2015 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. Jan 21, 2016 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: github.com/korylprince/dotfiles Jan 22, 2016 at 0:49
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.

4
  • Worked great for me. Solus Linux, gnome-terminal, and zshell.
    – psparrow
    Apr 13, 2017 at 17:57
  • Works great for me too, I'm using the default Elementary OS 0.4.1 Loki terminal, thanks. Jul 2, 2017 at 19:15
  • To fix the (mostly negligible) performance impact of saving a file on an actual disk it could be saved on a tmpfs mount. It is lost upon reboot but that seems fine to me.
    – Johnride
    Mar 31, 2020 at 17:02
  • Worked for me on macOS, zsh
    – jacobkim
    Dec 14, 2023 at 5:50
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 <[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

You must log in to answer this question.

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