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
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.
-
3It 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
.– swalogCommented Jun 12, 2014 at 13:26 -
2
/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"
– swalogCommented Jun 15, 2014 at 22:06 -
2I'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? Commented 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. Commented Jan 21, 2016 at 20:03 -
1I'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 Commented Jan 22, 2016 at 0:49
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.
-
Worked great for me. Solus Linux, gnome-terminal, and zshell.– psparrowCommented Apr 13, 2017 at 17:57
-
Works great for me too, I'm using the default Elementary OS 0.4.1 Loki terminal, thanks. Commented 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.– JohnrideCommented Mar 31, 2020 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 <[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