Is there a way to get the name of the last-used workspace?
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.
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.
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:
f.write(event['old']['name'])
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.
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
i3.workspace('back_and_forth')
lastWSName = i3.filter(i3.get_workspaces(), focused=True)[0]['name']
sleep(0.01)
i3.workspace('back_and_forth')
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.)
Asked: 2014-01-17 09:31:47 +0000
Seen: 113 times
Last updated: Jan 17 '14