Skip to content

Instantly share code, notes, and snippets.

@luxigo
Created November 10, 2015 07:22
Show Gist options
  • Save luxigo/ecdc284967ecd1e33530 to your computer and use it in GitHub Desktop.
Save luxigo/ecdc284967ecd1e33530 to your computer and use it in GitHub Desktop.
Give focus to new gnome-terminal window
#!/bin/bash
GNOME_TERMINAL_SERVER=/usr/lib/gnome-terminal/gnome-terminal-server
GNOME_TERMINAL=/usr/bin/gnome-terminal
# get last window in list for $GTS_PID
lastWindowId() {
wmctrl -p $GTS_PID -l | tail -n 1 | cut -f 1 -d ' '
}
# get gnome-terminal-server pid
GTS_PID=$(pgrep -f $GNOME_TERMINAL_SERVER)
# get last terminal window in list, if applicable
[ -n "$GTS_PID" ] && PREV=$(lastWindowId)
# launch terminal
$GNOME_TERMINAL "$@" || exit
RET=$?
# get gnome-terminal-server pid, if not running before
[ -z "$GTS_PID" ] && GTS_PID=$(pgrep -f $GNOME_TERMINAL_SERVER)
# wait for new gnome-terminal-server window
COUNT=0
while true ; do
LAST=$(lastWindowId)
[ "$LAST" != "$PREV" ] && break
sleep 0.1
(( ++COUNT > 1000 )) && exit 1
done
# bring window to front and give focus
wmctrl -i -a $LAST
exit $RET
@luxigo
Copy link
Author

luxigo commented Nov 10, 2015

At the time I write those lines, gnome-shell new window focus policy is weird (say *'ed up or broken)

Nothing more annoying than opening a new terminal with a keyboard shortcut and begin to type (say continue to type) in the previous window... The same method needs to be applied for browser windows opened via keyboard shortcut

Place this script in ~/bin or /usr/local/bin

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