Skip to content

Instantly share code, notes, and snippets.

@CD3
Created July 16, 2017 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CD3/61ec20e7dd15ee64d4c3967a89ba2957 to your computer and use it in GitHub Desktop.
Save CD3/61ec20e7dd15ee64d4c3967a89ba2957 to your computer and use it in GitHub Desktop.
Small script that will open a new terminal in the working directory of the currently focused terminal. Useful with i3.
#! /bin/bash
ID=$(xprop -root | gawk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')
PID=$(xprop -id $ID | gawk '/_NET_WM_PID\(CARDINAL\)/{print $NF}')
SH=$(basename $SHELL)
ZPID=$(pstree -lpA $PID | sed -n "/$SH/ s/.*$SH(\([0-9]\+\)).*/\1/p")
if [[ -n $ZPID ]]
then
CWD=$(readlink /proc/$ZPID/cwd)
fi
if ! which $TERMINAL
then
TERMINAL=terminology
fi
if ! which $TERMINAL
then
TERMINAL=xterm
fi
if [[ -n $CWD ]]
then
cd $CWD && $TERMINAL
else
$TERMINAL
fi
@CD3
Copy link
Author

CD3 commented Jul 16, 2017

I use this for my i3 terminal launch. Its useful for example if you are editing a script in vim and you want to open another terminal to run the script. This script will launch the terminal from the working directory of the vim terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment