Skip to content

Save X11 window workspace placements to a file, then restore those windows to their correct workspaces later.

Notifications You must be signed in to change notification settings

dflock/window-workspace-save-restore

Repository files navigation

X11 Window Workspace Placement Save & Restore

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:

# 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:

$ sudo apt install wmctrl

Installation

$ sudo cp window-workspace-save.sh /usr/bin/window-workspace-save
$ sudo cp window-workspace-restore.sh /usr/bin/window-workspace-restore

Usage

# 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:

#!/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:

*/5 * * * * $HOME/bin/backup-chrome-window-placement.sh > $HOME/tmp/backup-vscode-window-placement.log 2>&1

or this to run it every minute:

* * * * * $HOME/bin/backup-chrome-window-placement.sh > $HOME/tmp/backup-vscode-window-placement.log 2>&1

About

Save X11 window workspace placements to a file, then restore those windows to their correct workspaces later.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Languages