Skip to content

Instantly share code, notes, and snippets.

@jairovsky
Last active December 7, 2018 19:03
Show Gist options
  • Save jairovsky/b727d80272f8bb9e159400f220f7bdb0 to your computer and use it in GitHub Desktop.
Save jairovsky/b727d80272f8bb9e159400f220f7bdb0 to your computer and use it in GitHub Desktop.
bash: focus window (with support for cycling through multiple windows of the same program)
#!/bin/bash
# copied without shame with a little adaptation from https://stackoverflow.com/a/33607859
active_win_id=`xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}' | awk -F', ' '{print $1}'`
if [ "$active_win_id" == "0" ]; then
active_win_id=""
fi
app_name=$1
workspace_number=`wmctrl -d | grep '\*' | cut -d' ' -f 1`
win_list=`wmctrl -lx | grep -r "$app_name" - | grep " $workspace_number " - | awk '{print $1}'`
# get next window to focus on, removing id active
switch_to=`echo $win_list | sed s/.*$active_win_id// | awk '{print $1}'`
# if the current window is the last in the list ... take the first one
if [ "$switch_to" == "" ];then
switch_to=`echo $win_list | awk '{print $1}'`
fi
if [[ -n "${switch_to}" ]]
then
(wmctrl -ia "$switch_to") &
else
if [[ -n "$2" ]]
then
($2) &
fi
fi
exit 0
@jairovsky
Copy link
Author

download it:

curl -L -o ~/bin/switch_window.sh https://gist.github.com/jairovsky/b727d80272f8bb9e159400f220f7bdb0/raw/e8e15293effb94e599a6f7920f9c41f40a570171/switch_window.sh
chmod +x ~/bin/switch_window.sh

use it:

switch_window.sh "Firefox"
switch_window.sh "evince"
switch_window.sh "\- Visual Studio Code"

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