Make wmctrl ignore other-than-current workspace's windows

I rely on wmctrl a lot to switch between windows, basically to avoid typing alt-tab many times. I've always used one workspace but now I want to begin using more than one.

I was wondering if it is possible to modify the context of wmctrl so that when I type wmctrl -l, only windows from the current workspace are shown, instead of all windows from all workspaces.

For example, assuming I have a Firefox window opened in workspace 1. I have a keyboard shortcut on ctrl+alt+f that executes wmctrl -a firefox, which switches to Firefox. If I open a new Firefox window on workspace 2 and type ctrl+alt+f, it will switch to the window at workspace 1, which is not what I want.

Any suggestions/ideas on how to solve this?

EDIT: I'm using compiz (Unity)


If you're using Compiz (run wmctrl -m if not sure), wmctrl only sees 1 desktop (2nd field of wmctrl -l, ie 0) but you can use the geometry (-G) option to know what window is on what virtual desktop. All windows in your current desktop will have an x-position between 0 and your screen's width. Same for the y-position between 0 and your screen's height. So you can use something like that

#!/bin/bash

SCREEN_W=$(xwininfo -root | sed -n 's/^  Width: \(.*\)$/\1/p')
SCREEN_H=$(xwininfo -root | sed -n 's/^  Height: \(.*\)$/\1/p')

NAME='Navigator.Firefox'

wmctrl -xlG | awk -v W="$SCREEN_W" -v H="$SCREEN_H" -v NAME="$NAME" '$7==NAME && $3>=0 && $3<W && $4>=0 && $4<H {print $1}' | while read WINS; do wmctrl -ia "$WINS"; done

exit 0

You can hardcode your screen's width and height if you will, and NAME if you want a one-liner. Im not sure how you want to handle multiple windows matching NAME. That will focus them all.

For metacity, desktop number of windows can be found using wmctrl -l and grepping the 2nd field.