Not sure if this guide will be of any use. I found it while cleaning up drafts from a few years back. I don’t run i3 anymore (just Gnome) so I haven’t tested it and don’t recall why I didn’t publish this.
In any case, instead of deleting, I just wanted to leave it here:
i3wm: Switch to Previous Workspace When Closing Last Window in a Workspace
I’m still using i3 on my Kali Linux setup and I want to tweak a small part of my workflow. I want i3 to switch back to the previous workspace when I close the last window in the current workspace.
Right now when I close the only window in a workspace (e.g. use Super + Q to close a text editor on Workspace 2), the focus just sits on the now empty Workspace 2. I find this a bit annoying because I’d rather it switch back to the previous workspace (let’s say Workspace 1 where my browser is usually) instead of leaving me staring at a blank desktop. I know others might prefer this default behavior.
I did this on Arch + i3 before but I don’t have those dot files and I forgot how I did it. So here’s how I did it:
- Create a script that will check if the current workspace has any windows. If not, it will switch to the previous workspace.
- Bind a key in your i3 configuration to run this script when you close a window.
Here’s an example script (switch_workspace.sh):
#!/bin/bash # Check if the current workspace has any windows if [ “$(i3-msg -t get_tree | jq -r ‘.. | select(.focused?two bright green speech bubbles float side by side, casting small shadows on their surface. the bubbles are glossy, with a slight reflection on the edges. each bubble has three small green dots inside, representing conversation. the background is white, making the green bubbles stand out. the image conveys a sense of communication and discussion. == true).nodes | length’)” -eq 0 ]; then # Switch to the last focused workspace i3-msg workspace back_and_forth fi
Make sure to give the script executable permissions:
chmod +x switch_workspace.sh
And add a key binding to your i3 configuration (~/.config/i3/config):
# Bind this script to the key combination you prefer
bindsym $mod+q exec --no-startup-id /path/to/your/script/switch_workspace.sh
Now, when you close a window with $mod+q, it should run this script, checking if the current workspace is empty and switching to the previous workspace if it is. Adjust the keybinding as needed.