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?

- 829,060

- 1,831
- 2
- 14
- 12
3 Answers
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.

- 1,831
- 2
- 14
- 12
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.
-
-
Works great for me too, I'm using the default Elementary OS 0.4.1 Loki terminal, thanks. – Rafael Berro Jul 02 '17 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 '20 at 17:02
-
@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

- 248
export PROMPT_COMMAND=...
, if such a thing already exists in your.bashrc
. – swalog Jun 12 '14 at 13:26/etc/profile.d/vte.sh
overrides thePROMPT_COMMAND
variable. To fix this, you can modifyvte.sh
, and change the part withPROMPT_COMMAND="__vte_prompt_command"
toPROMPT_COMMAND="${PROMPT_COMMAND};__vte_prompt_command"
– swalog Jun 15 '14 at 22:06.zshrc
. I'm using oh-my-zsh, not sure if that is related. – Andreas Mueller Jan 21 '16 at 20:03gnome-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:14vte.sh
from thevte-common
arch linux package. I'm using zsh, and sourcing thevte.sh
file in.zshrc
makes the difference in tabs working correctly. I'm usingGNOME 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:40vte.sh
to.zshrc
, closed the window, and triedFile -> Open Terminal
from another window which I'd forgotten to close (feeling silly). It works now :) – sigvaldm Jun 01 '18 at 07:03