https://github.com/dflock/window-workspace-save-restore
Save X11 window workspace placements to a file, then restore those windows to their correct workspaces later.
https://github.com/dflock/window-workspace-save-restore
linux window workspaces x11
Last synced: 11 months ago
JSON representation
Save X11 window workspace placements to a file, then restore those windows to their correct workspaces later.
- Host: GitHub
- URL: https://github.com/dflock/window-workspace-save-restore
- Owner: dflock
- Created: 2021-01-29T05:35:13.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-09-03T20:49:00.000Z (over 1 year ago)
- Last Synced: 2025-04-15T16:14:43.347Z (about 1 year ago)
- Topics: linux, window, workspaces, x11
- Language: Shell
- Homepage:
- Size: 14.6 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
Awesome Lists containing this project
README
# X11 Window Workspace Placement Save & Restore
:author: Duncan Lock
These two scripts let you:
* Save the current workspace placement of X11 windows, to stdout.
* Restore the workspace placement of X11 windows from stdin.
This allows you to dump window workspace placements to a file, then restore those windows to their correct workspaces later.
You can filter the list through grep on the way out or back in, to only affect a subset of the windows.
== Why is this useful?
I currently have VSCode open, with 7 windows, arranged across 3 workspaces. If I quit and restart VSCode, all the windows will come back, but they will all be opened in the current workspace - and I'll have to move the back to the appropriate workspace. These two scripts let you automate this:
[source,console]
----
# Run save to save VSCode window placements
$ window-workspace-save | grep "Visual Studio" > ~/tmp/vscode-windows.txt
# Restart VSCode... then restore the VSCode windows to their correct workspaces:
$ cat ~/tmp/vscode-windows.txt | window-workspace-restore
----
This works with any X11 windows, not just VSCode.
== Requirements
You need `wmctrl` installed. See: https://www.freedesktop.org/wiki/Software/wmctrl/
For Debian/Ubuntu, you can do:
[source,console]
----
$ sudo apt install wmctrl
----
== Installation
[source,console]
----
$ sudo cp window-workspace-save.sh /usr/bin/window-workspace-save
$ sudo cp window-workspace-restore.sh /usr/bin/window-workspace-restore
----
== Usage
[source,console]
----
# Restore the workspace placement of windows from a file:
$ cat ~/tmp/windows.txt | window-workspace-restore
# Restore the workspace placement of just the VSCode windows from a file:
$ grep 'Visual Studio Code' ~/tmp/windows.txt | window-workspace-restore
# Save the workspace placement of all windows to a file:
$ window-workspace-save > ~/tmp/windows.txt
# Save the workspace placement of all Google Chrome windows to a file:
$ window-workspace-save | grep 'Google Chrome' > ~/tmp/chrome-windows.txt
----
== Caveats
=== Only works with windows with unique titles
This uses `wmctrl` to do this. Wmctrl _does have_ window IDs, but these aren't stable, so when applications are restarted they will get new window IDs. The only thing that you can use is the window's title. So, if you have lots of terminal windows open all called "Terminal", then these will probably get moved around arbitrarily.
Seems to work OK for applications with lots of document/tab windows, but not so well with lots of instances of single window applications, which sometimes do have the same window title.
=== Doesn't really help with crashes
You need to run the save script _first_, _before_ you can restore - so it works OK with deliberate restarts. If the app crashes - and you haven't run the save script... then you're out of luck. One way around this is to run the script regularly every few minutes using `cron`. For example, you could run this every 5 mins to keep a recent backup of all your 'Visual Studio Code' window placements:
[source,bash]
----
#!/usr/bin/env bash
# Print the current environment, without colours/escapes, so it doesn't mess up the terminal
printenv | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g'
set -o xtrace
export DISPLAY=:0.0
# Save Visual Studio Code window placements to a variable
windows=$(/home/duncan/bin/window-workspace-save | grep 'Visual Studio Code')
# Only overwrite the file if there are windows
if [ -n "$windows" ]; then
echo "$windows" > $HOME/.config/windows/vscode-windows.txt
fi
----
Save this as a script somewhere and make it executable (`chmod +x $HOME/bin/backup-vscode-window-placement.sh`), then add this to your crontab to run it every 5 mins:
[source,shell]
----
*/5 * * * * $HOME/bin/backup-chrome-window-placement.sh > $HOME/tmp/backup-vscode-window-placement.log 2>&1
----
or this to run it every minute:
[source,shell]
----
* * * * * $HOME/bin/backup-chrome-window-placement.sh > $HOME/tmp/backup-vscode-window-placement.log 2>&1
----