urxvtpwd (1433B)
1 #!/bin/bash 2 3 # run ARGV, with -cd FOLDER if FOLDER can be extracted from title of current 4 # window (see zsh config for how the title gets put in the window) 5 6 CHOST=$(hostname | cut -d. -f1) 7 8 # http://superuser.com/a/403369/77814 9 quoted_args="$(printf " %q" "$@")" 10 11 # inspired by 12 # https://faq.i3wm.org/question/150/how-to-launch-a-terminal-from-here/%3C/p%3E.html 13 ID=$(xdpyinfo | grep focus | cut -d ',' -f1 | rev | cut -d ' ' -f1 | rev) 14 CLASS=$(xprop -id "$ID" | grep -m1 WM_CLASS | cut -d'"' -f2) 15 16 # http://stackoverflow.com/a/19411918 17 if [ "${CLASS^^}" != "URXVT" ] 18 then 19 # no urxvt focused -- just do the default 20 # optionally we could try to extract the pwd with 21 # https://github.com/schischi-a/xcwd or something 22 exec $quoted_args 23 fi 24 25 TITLE=$(xprop -id "$ID" | grep -m1 WM_NAME) 26 MYPWD=$(echo "$TITLE" | cut -d'$' -f1 | cut -d'"' -f2- | cut -d':' -f2-) 27 MYHOST=$(echo "$TITLE" | cut -d'$' -f1 | cut -d'"' -f2- | cut -d':' -f1 | tr -d ' ') 28 MYCOLON=$(echo "$TITLE" | cut -d'$' -f1 | cut -d'"' -f2- | grep -E '^[ a-z.]*:') 29 MYPWD2="${MYPWD/#\~/$HOME}" 30 31 if [ -z "$MYHOST" -o -z "$MYCOLON" -o "$MYHOST" = "$CHOST" ] 32 then 33 if [ ! -z "$MYPWD2" -a -d "$MYPWD2" -a -r "$MYPWD2" -a -x "$MYPWD2" ] 34 then 35 exec $quoted_args -cd "$MYPWD2" 36 else 37 exec $quoted_args 38 fi 39 else 40 if [ ! -z "$MYPWD2" ] 41 then 42 exec $quoted_args -e ssh -t "$MYHOST" "cd \"$MYPWD2\"; zsh" 43 else 44 exec $quoted_args -e ssh -t "$MYHOST" 45 fi 46 fi 47