3

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 '14 at 13:23
  • <Alt><Shift><Enter> – Richard Hum May 27 '14 at 13:32
  • 1
    Try, from terminal 1, xterm &. – MariusMatutiae May 27 '14 at 15:36
3

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

https://faq.i3wm.org/question/150/how-to-launch-a-terminal-from-here/

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

| improve this answer | |
3

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

| improve this answer | |
0

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"
  fi
}

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):

#!/bin/bash

# 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" ]
then
  # 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
fi

TITLE=$(xprop -id "$ID" | grep -m1 WM_NAME)
MYPWD=$(echo "$TITLE" | cut -d'$' -f1 | cut -d'"' -f2- | cut -d':' -f2-)
MYPWD2="${MYPWD/#\~/$HOME}"

if [ ! -z "$MYPWD2" -a -d "$MYPWD2" -a -r "$MYPWD2" -a -x "$MYPWD2" ]
then 
  exec $quoted_args -cd "$MYPWD2"
else
  exec $quoted_args
fi
| improve this answer | |
0

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

| improve this answer | |
0

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

https://github.com/RichardHum/bin/blob/master/curdirterm

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

| improve this answer | |
0

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.

| improve this answer | |

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.