1

I have configured my keyboard shortcuts so that Alt + left takes me to the work space left, and Alt + right takes me to the work space to the right, but I would rather have one set of keys to cycle. Ideally, something like

workspace 1 + Alt + tab ---> worskspace 2
workspace 2 + Alt + tab ---> worskspace 3
workspace 3 + Alt + tab ---> worskspace 4
workspace 4 + Alt + tab ---> worskspace 1

The problem is the last line. I don't see any way to go from work space 4 back to work space 1. How to move to the right modulo 4 ?

Jacob Vlijm
  • 83,767
uhuola
  • 11

2 Answers2

3

Cycle through your viewports

With a small script, it is very well possible to browse through the workspaces (actually viewports):

  • forward:

    enter image description here

    (If the last viewport is reached, the script moves to the first one)

  • ...or backward:

    enter image description here

    (If the first viewport is reached, the script moves to the last one)

The script

#!/usr/bin/env python3
import subprocess
import sys

move = sys.argv[1]

# get the needed info from wmctrl -d
wsdata = subprocess.check_output(["wmctrl", "-d"]).decode("utf-8").split()
# retrieve total size of workspace
ws = [int(n) for n in wsdata[3].split("x")]
# panel/launcher height/width
pans = [int(n) for n in wsdata[7].split(",")]
# work area
wa = [int(n) for n in wsdata[8].split("x")]
# x/y resolution
res_h = pans[0]+wa[0]; res_v = pans[1]+wa[1]
# current position in the spanning workspace
VP = [int(n) for n in wsdata[5].split(",")]

def last_h():
    # test if we are on the last viewport horizontally
    return VP[0]+res_h == ws[0]

def first_h():
    # test if we are on the first viewport horizontally
    return VP[0] == 0

def last_v():
    # test if we are on the last viewport vertically
    return VP[1]+res_v == ws[1]

def first_v():
    # test if we are on the first viewport vertically
    return VP[1] == 0

if move == "next":
    if last_h() == False:
        command = str(VP[0]+res_h)+","+str(VP[1])
    elif last_v() == True:
        command = "0,0"
    else:
        command = "0,"+str(VP[1]+res_v)

if move == "prev":
    if first_h() == False:
        command = str(VP[0]-res_h)+","+str(VP[1])
    elif first_v() == True:
        command = str(ws[0]-res_h)+","+str(ws[1]-res_v)
    else:
        command = str(ws[0]-res_h)+","+str(VP[1]-res_v)

subprocess.Popen(["wmctrl", "-o", command])

How to use

  1. The script needs wmctrl:

    sudo apt-get install wmctrl
    
  2. Copy the script into an empty file, save it as through_viewports.py

  3. Add two commands to two different shortcut keys:

    python3 /path/to/through_viewports.py next
    

    to go to the next viewport, and:

    python3 /path/to/through_viewports.py prev
    

    to go to the previous viewport

    Open System Settings > Keyboard > Shortcuts > Custom Shortcuts. Click the + and add both commands to shortcuts you like.

That's it The script detects how your viewports are set up and cycles through them.

How it works, the concept

In Unity, viewports are arranged in one big matrix, which all together makes the single workspace, the Unity desktop exists of.

Using the command:

wmctrl -d

in the output, we can read all information we need to find out where we currently are in the matrix.

0  * DG: 5120x2400  VP: 0,0  WA: 65,24 1215x776  N/A
  • 5120x2400 is the total size of all viewports (matrix)
  • 0,0 is the x/y position of the current viewport in the matrix (top left, pixels)
  • from WA: 65,24 1215x776 we can derive the screen's resolution (65,24 are the width/height of the Launcher/panel, 1215x776 is the remaining area)

Once we have the correct information, the script calculates the target position in the matrix and sets it with the command:

wmctrl -o x,y
Jacob Vlijm
  • 83,767
  • @DKBose isn't that what I wrote in the answer? "In Unity, viewports are arranged in one big matrix, which all together makes the single workspace, the Unity desktop exists of" Unity indeed has only one workspace by definition. – Jacob Vlijm Mar 08 '16 at 07:12
  • thank you for your answer, this is really complete and clear! I just wanted to know: is it important the location where I put the file "through_viewports.py"? Thank you, Cloé – girardcarillo May 04 '19 at 19:47
  • I would just store it locally unless you'd use it for all users. – Jacob Vlijm May 04 '19 at 20:47
2

In 12.04 I had solved this problem by editing a key with gconf-editor, but didn't have that same key in 16.04 so here's what worked for me:

sudo apt-get install compizconfig-settings-manager

which installs a GUI advanced-settings utility, then

ccsm

which launches it. And then I went to Desktop Wall > Viewport Switching > Allow Wrap-Around and checked the box.

ph7four
  • 21