Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to use dmenu and wmctrl to write a script that would present to me a list of all currently open windows and then switch to the one I select. It looks something like this:

switch_to script

Now, I'm using the window id I get from wmctrl -l (for instance, the "0x00c0005c" visible in the screenshot on the 2nd line) to determine exactly which window has been selected (I'm not using the window class because 2 windows belonging to the same application will have the same class, but different ids).

The only problem is: wmctrl -i -a works sometimes, but doesn't work other times - and some times, it switches, but to the wrong window! It's quite random. After searching online, the only web page I found that even mentions the issue is this one. It says:

WIN as a numerical window ID doesn't seem to work consistently. Perhaps not all options support it or something crazy is going on. It used to work very well for me. Re-test.

Can anyone tell me how I can resolve or work around this issue?

I'm using version 1.07-3 of wmctrl from the official repositories on an up-to-date Arch Linux machine, and Awesome Window Manager, if it helps. Thanks

share|improve this question
    
I have a similar script using simlply wmctrl -a and the window title, which works around the problem, as long as you don't have windows with the same title. –  crater2150 Jan 8 '13 at 17:03
    
@crater2150 Even that (wmctrl -a) is inconsistent for me. It might be a bug with Awesome window manager itself. –  Vicky Chijwani Jan 8 '13 at 23:16
    
I use awesome too, and the only time it has problems is with clients, which are on no tag at all. I remember having problems with an earlier version of awesome (I've got 3.5), but in between I also rewrote my script, so I'm not sure if awesome was the problem. –  crater2150 Jan 9 '13 at 11:54
    
@crater2150 would you mind sharing this script of yours? :) –  Vicky Chijwani Jan 9 '13 at 21:20
1  
Great idea! Personally I am using a simple oneliner wmctrl -i -a $(wmctrl -l |cut -c1-40 |dmenu | cut -d " " -f 1) to test this. It seems quite reliable to me, handling cases where a window is on the same desktop, different desktop, minimized etc.. Even when multiple tags are active (though that removes the multiple tagging and activates only the tag of the target window.). Anyway, is the effect of this worse, better, or the same as your script? –  kampu May 22 '13 at 7:11

1 Answer 1

up vote 0 down vote accepted

I got the thing working after fiddling around with it today, but I haven't been able to pinpoint what the problem was when I tried it last time (I did switch to Linux Mint in the interim; maybe that solved the issue somehow.)

Here's the working script for anyone interested:

#!/bin/sh

# source dmenu config file if it exists
if [ -f $HOME/.dmenurc ]; then
    . $HOME/.dmenurc
else
    DMENU='dmenu -i'
fi

# get list of all windows, and their count
wmctrl_output=$(wmctrl -lx)
win_count=$(echo "$wmctrl_output" | wc -l)
# get rid of the hostname and the number in the 2nd column
hostname=$(uname -n)
win_list=$(echo "$wmctrl_output" | \
    sed -r -e 's/[^@]'$hostname'//' | \
    sed -r -e 's/ [0-9][0-9]? / /')

# select a window ($target) and extract its id ($target_id)
target=$(echo "$win_list" | $DMENU -l $win_count -p "Switch to: ")
target_id=$(echo "$target" | cut -d' ' -f1)

# switch to target window
cmd="wmctrl -i -a \"$target_id\""
eval "$cmd"

and my ~/.dmenurc:

DMENU_FONT='-*-*-medium-r-*-*-18-*-*-*-*-*-*-*'
DMENU="dmenu -i -fn $DMENU_FONT -nb #1E2320 -nf #DBDBDB -sb #3f3f3f -sf #ffffff"

All you need to is put the script somewhere on your $PATH, make it executable, and bind a key to it.

share|improve this answer

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.