Is there a way to detect which workspace you are currently in from the command line?

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

Example:

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

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:

#!/usr/bin/python
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.