The i3 FAQ has migrated to All content here is read-only.
Ask Your Question

Is there a way to get the name of the last-used workspace?

asked 2014-01-17 09:31:47 +0000

paramnesioid gravatar image

I would like a way to get the name of the last-used workspace. I don't mind if I have to use a script.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-01-17 14:47:55 +0000

TonyC gravatar image

updated 2014-01-17 14:57:22 +0000

I thought this was interesting so I thought I would give it a try.

#!/usr/bin/env python2

import i3

LAST_WS_PATH = '/tmp/i3.last_workspace'

def on_ws_focus(event, data, subscription):
    if 'old' in event:
        with open(LAST_WS_PATH, 'w') as f:

i3.Subscription(on_ws_focus, 'workspace')

Now after you run the script, to get the name of the last used workspace, use cat /tmp/i3.last_workspace. For instance, you could get the exact same thing as back and forth with i3-msg workspace $(cat /tmp/i3.last_workspace).

If you have a tmpfs, the write will happen in memory, so it won't be that expensive.

edit flag offensive delete link more


Thanks TonyC!

paramnesioid gravatar imageparamnesioid ( 2014-01-17 15:19:45 +0000 )edit

answered 2014-01-17 12:27:18 +0000

Adaephon gravatar image

Unfortunatelly the name of the previously used workspace is not exposed in any way.

You can go to the last used workspace with

$ i3-msg workspace back_and_forth

which toggles between the current and last workspace.

So you could use a script to switch to the last workspace, get the current name from i3-msg -t get_workspaces and switch back.

In Python using this python library it looks like that:

import i3
from time import sleep

lastWSName = i3.filter(i3.get_workspaces(), focused=True)[0]['name']

The sleep(0.01) is needed because it seems that the second workspace switch will not work most of the time if called without delay. (0.01 is just an experimental value, that works for my current machine. 0.001 was to short most of the time.)

edit flag offensive delete link more

Question Tools


Asked: 2014-01-17 09:31:47 +0000

Seen: 113 times

Last updated: Jan 17 '14