36

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?

47

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
    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
  • 1
    /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: github.com/korylprince/dotfiles – korylprince Jan 22 '16 at 0:49
7

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. – psparrow Apr 13 '17 at 17:57
  • Works great for me too, I'm using the default Elementary OS 0.4.1 Loki terminal, thanks. – Rafael Berro Jul 2 '17 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.