Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

2 Answers 2

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:

#!/bin/bash
app_name=$1
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
    active_win_id=""
fi


# 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
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 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 at 7:50

This script does what you want:

#!/bin/bash

app=$1
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 "," " ")
IDs=(${IDs##*#})

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

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 at 20:11
1  
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 at 20:24

Your Answer

 
discard

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.