When in a directory, I sometimes want another terminal to be opened in the same directory.

For example, I'm in ~/code/someproject/src/, and I open a new terminal, by default, it opens in ~, how do I launch a new terminal in the current directory?

I'm running Arch Linux with urxvt as my terminal and i3 as my window manager.

  • How do you open the terminal?
    – choroba
    May 27, 2014 at 13:23
  • <Alt><Shift><Enter> May 27, 2014 at 13:32
  • 1
    Try, from terminal 1, xterm &. May 27, 2014 at 15:36

6 Answers 6


There is a very informative thread over in the i3-faq forum that has various scripts/programs that tackle this very issue.


I went for the c program xcwd which really does a great job. It also works from within GUI programs.


when you launch a terminal from within a existing terminal, the original terminal's environment will be preserved including current working directory and any other environment variables that you have set. This is why urxvt & works


None of the answers I have seen work when using urxvtd, in which case all terminal windows are associated to the same urxvtd daemon process and there seems to be no easy way to identify the correct shell instance to get the PWD.

To address this, I used a ugly hack. I use zsh with a preexec trick to display in the window title the current directory (along with the current and last command, time, etc.), which I think is useful anyway. I do it with something of the following form in my zshrc (you will need to adapt it, see this command; see here for full latest version):

preexec () {
  local WD="$(pwd | sed "s/^\/home\/$USER/~/")"
  export LASTDATE="`date +%T`"
  if [[ "$TERM" == "rxvt-unicode" || "$TERM" == "rxvt-unicode-256color" ]]; then
    export COMMAND="$(echo $1 | tr -d '\n')"
    echo -ne "\e]0;$LOCALNAME $HOST:$WD$ $COMMAND ($LASTDATE)\a"

Then I just made a script to run a new urxvt in the same directory as the focused urxvt instance, simply by extracting the title in the current focused window (see here for latest version):


# run ARGV, with -cd FOLDER if FOLDER can be extracted from title of current
# window (see zsh config for how the title gets put in the window)

# http://superuser.com/a/403369/77814
quoted_args="$(printf " %q" "$@")"

# inspired by
# https://faq.i3wm.org/question/150/how-to-launch-a-terminal-from-here/%3C/p%3E.html
ID=$(xdpyinfo | grep focus | cut -d ',' -f1 | rev | cut -d ' ' -f1 | rev)
CLASS=$(xprop -id "$ID" | grep -m1 WM_CLASS | cut -d'"' -f2)

# https://stackoverflow.com/a/19411918
if [ "${CLASS^^}" != "URXVT" ]
  # no urxvt focused -- just do the default
  # optionally we could try to extract the pwd with
  # https://github.com/schischi-a/xcwd or something
  exec $quoted_args

TITLE=$(xprop -id "$ID" | grep -m1 WM_NAME)
MYPWD=$(echo "$TITLE" | cut -d'$' -f1 | cut -d'"' -f2- | cut -d':' -f2-)

if [ ! -z "$MYPWD2" -a -d "$MYPWD2" -a -r "$MYPWD2" -a -x "$MYPWD2" ]
  exec $quoted_args -cd "$MYPWD2"
  exec $quoted_args

I recently developed a urxvt extension which is keeping in shared memory (IPC) the working directory of shell PID from last focused terminal.

The full documentation and code is here: https://raphael.medaer.me/2019/06/21/urxvt-open-current-wd.html


I have a really, really, really ugly hack that I wrote to do this.


My hostname is bulletMaster, and my home directory is /root/


Somewhat similar to simotek's answer:

I use st, which, like many other terminal emulators opens a new terminal in the current working directory if run from command line.

An alias like: alias opennewterm="st >/dev/null 2>&1 & disown" opens a terminal in the current directory and doesn't block the terminal.

If you're using zsh, you can bind that to a keyboard shortcut by putting the following in your .zshrc:

bindkey -s "^[n" "opennewterm\n"

which types opennewterm and a newline when Alt+n is pressed.

You must log in to answer this question.

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