dotfiles/i3/terminal.sh

23 lines
650 B
Bash
Executable file

#!/bin/bash
# If a window with a path in its title is focused, open a terminal with that
# location as working directory. Like this, terminals are opened in the same
# directory as the currently focused one, if no command is running. This is
# very hackish, but it works.
if [[ $(xtitle) =~ (^| )(~?)(/.*) ]]; then
path="${BASH_REMATCH[3]}"
[[ ${BASH_REMATCH[2]} == "~" ]] && path="$HOME$path"
while [[ $path != / && -n $path && ! -d $path ]]; do
path="$(dirname "$path")"
done
fi
# Open terminal at specified path
cmd=i3-sensible-terminal
if [[ -n $path ]]; then
exec $cmd --default-working-directory="$path" "$@"
else
exec $cmd "$@"
fi