Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've created some shortcuts mimicking the Unity's Mod4 + num behavior.

wmctrl -xa Sublime || subl

What I don't like, is that once sublime is running, it always focuses the first opened window. I'd like to focus the last "focused" window. Same as in Unity. Is there a flag for that?

share|improve this question

This script does what you want:


workspace=$(wmctrl -d | grep '\*' | cut -d ' ' -f1)
win_list=$(wmctrl -lx | grep $app | grep " $workspace " | awk '{print $1}')

IDs=$(xprop -root|grep "^_NET_CLIENT_LIST_STACKING" | tr "," " ")

for (( idx=${#IDs[@]}-1 ; idx>=0 ; idx-- )) ; do
    for i in $win_list; do
        if [ $((i)) = $((IDs[idx])) ]; then
            wmctrl -ia $i
            exit 0

exit 1

EDIT: This script always focuses on the last focused window, as opposed to cycling through the windows in the order they were opened.

EDIT 2: I modified the script (turns out wmctrl and xprop use slightly different formats for displaying hexadecimal numbers).

EDIT 3: app name should be taken from the 3rd column of wmctrl -lx to avoid certain conflicts.

share|improve this answer
The question was already answered, providing an extra answer can be helpful but then explain why and what this is a better / different solution. – Requist Dec 16 '14 at 20:11
I added an explanation although the first line already says that this script does exactly what the OP asked for. The accepted solution, while useful, does not. – Raul Laasner Dec 16 '14 at 20:24
up vote 2 down vote accepted

I've made a very robust app switcher using wmctrl. Check my Ubuntu Forums post and my askubuntu answer.

This is the script to launch:

workspace_number=`wmctrl -d | grep '\*' | cut -d' ' -f 1`
win_list=`wmctrl -lx | grep $app_name | grep " $workspace_number " | awk '{print $1}'`

active_win_id=`xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}' | awk -F', ' '{print $1}'`
if [ "$active_win_id" == "0" ]; then

# 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}'`

if [[ -n "${switch_to}" ]]
        (wmctrl -ia "$switch_to") &
        if [[ -n "$2" ]]
                ($2) &

exit 0
share|improve this answer
So, this doesn't go directly to the last-focused window, but instead lets you iterate through the application's windows in the order in which they were opened. Really nice script....(I'm using it). – jonS90 Apr 9 '14 at 2:35
Exactly. Glad you like it. I've made a whole minimal wmctrl tiling manager (aka a script) and hopefully will publish soon. – mreq Apr 9 '14 at 7:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.