I'm trying to figure out how to get the workspace number from a terminal script in gnome. Any ideas?


If you aren't using Compiz, you can use xdotool Install xdotool.


xdotool get_desktop

This will return 0 if run from the first workspace, 1 if run from the second etc.


An old, and answered thread, but I was just after the same info. You can do this with standard xorg tools with:

xprop -root -notype _NET_CURRENT_DESKTOP
  • Thank you for this answer. I am using it to create a separate tmux session for each Gnome workspace. I just need to use this instead: xprop -root -notype _NET_SHOWING_DESKTOP
    – Jochen
    Jan 8 '21 at 22:50

If you are using compiz, this will be a bit more difficult.

edit: this now works both with and without compiz, finally...

I wrote a "little" python script to do it:

from subprocess import Popen, PIPE
getoutput = lambda x: Popen(x, stdout=PIPE).communicate()[0]
compiz_running = list(i for i in getoutput(("ps", "-aef", )).split("\n")
    if "compiz --replace" in i and not "grep" in i) != []

if compiz_running:
    # get the position of the current workspace
    ws = list(int(i.strip(",")) for i in  getoutput(("xprop", "-root",
        "-notype", "_NET_DESKTOP_VIEWPORT", )).split()[-2:])
    # get the number of horizontal and vertical workspaces
    hsize = int(getoutput(("gconftool",
        "--get", "/apps/compiz/general/screen0/options/hsize", )))
    vsize = int(getoutput(("gconftool",
        "--get", "/apps/compiz/general/screen0/options/vsize", )))
    # get the dimentions of a single workspace
    x, y = list(int(i) for i in getoutput(("xwininfo", "-root",
        "-stats", )).split("geometry ")[1].split("+")[0].split("x"))
    # enumerate workspaces
    workspaces, n = [], 0
    for j in range(vsize):
        for i in range(hsize):
            workspaces.append([n, [x*i, y*j, ], ])
            n += 1
    print list(i for i in workspaces if i[1] == ws)[0][0]
# if compiz is not running
else: # this code via @DoR
    print getoutput(("xdotool", "get_desktop", )).strip() 

Save this somewhere and mark it as executable. This will output just a number between 0 and the number of workspaces.

This is how the enumeration looks like:

| 0 | 1 |
| 2 | 3 |

You've got to install xdotool Install xdotool for this to work in case compiz is disabled.

  • Thanks. I had to remove " --replace" from the compiz_running test, and also seemed to get the wrong value for vsize (1 vs 2) when running this on Precise 12.04, even though ccsm General Options said my Desktop Size / Vertical Virtual Size is 2.
    – nealmcb
    Aug 18 '13 at 19:40

Without installing anything and if you are using metacity, you can use this :

python -c "import wnck; s=wnck.screen_get_default(); s.force_update(); w=s.get_active_workspace();  w_num=w.get_number(); print(w_num);" 2>/dev/null

It seems that with Unity, the accepted answer

 xdotool get_desktop_viewport

doesn't work - it always returns 0. I guess the screen is configured as a really big viewport of which only part is visible. The alternative is a bit tricky since you have to know the size of your workspace. I.e.:

 xdotool get_desktop_viewport

will return something like "1600 0" if you are in the upper right workspace. The first number is probably the width of the largest display you have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.