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