Search code examples
linuxfocuskeyboard-shortcutsconsole-applicationwindow-managers

Cycle through windows of the same application using wmcrtl


I am configuring xbindkeys to change window focus using shortcuts. For example, I managed to create a shortcut to focus on a an application window, let's say a terminator window:

wmctrl -xa terminator

Unfortunately it focuses always at the same terminator window, preventing me to cycle through the terminator windows.

Could you suggest me a command to focus on a terminator window and, if pressed again, will cycle through all the terminator windows, please?

UPDATE 30 Mar 2013

I modified this script http://lars.st0ne.at/blog/switch%20between%20windows%20within%20the%20same%20application to make a script such that

script.sh NAME

focus on application NAME or cycle through all the windows of NAME if a window of it is already focused, but it doesn't work properly.

Here is the script

win_class=$1 # 'terminator' # $1

# get list of all windows matching with the class above
win_list=$(wmctrl -x -l | grep -i $win_class | awk '{print $1}' )

# get id of the focused window
active_win_id=$(xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}')

# 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

# switch to window
wmctrl -i -a $switch_to

The script does focus on a windows of the application, and cycle through them until it reach a window, I guess the last created. At that point, cycling doesn't work anymore.


Solution

  • I've found a problem in the script, if no window has focus.

    May you try the following modified script:

    #!/bin/bash
    win_class=$1 # 'terminator' # $1
    
    # get list of all windows matching with the class above
    win_list=$(wmctrl -x -l | grep -i $win_class | awk '{print $1}' )
    
    # get id of the focused window
    active_win_id=$(xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}')
    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
    
    # switch to window
    wmctrl -i -a $switch_to