Bash script to move focused container to new empty workspace

So I migrated from i3 to sway. Had a python script that I found on the internets that did this, and really like the functionality. Figured I’d give an attempt at making my own script in bash. My programming skills and bash scripting aren’t great, so I had chatGPT help me with some syntax. Thought others might be interested so am sharing here.


<span style="color:#323232;">#!/bin/bash
</span><span style="color:#323232;">
</span><span style="color:#323232;"># this script moves a container to an empty workspace
</span><span style="color:#323232;">#  and switches to that workspace
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Define list of available workspaces
</span><span style="color:#323232;">all_workspaces_list=(1 2 3 4 5 6 7 8 9 10)
</span><span style="color:#323232;">
</span><span style="color:#323232;"># get workspaces currently being used
</span><span style="color:#323232;">used_workspaces=$(swaymsg -t get_workspaces | grep -oP '"name": "Kd+')
</span><span style="color:#323232;"># create a list from used workspaces
</span><span style="color:#323232;">used_workspaces_list=($used_workspaces)
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Check for first of all_workspaces_list not in used_workspaces_list
</span><span style="color:#323232;">for workspace in "${all_workspaces_list[@]}"; do
</span><span style="color:#323232;">    if [[ ! "${used_workspaces_list[*]}" =~ "$workspace" ]]; then
</span><span style="color:#323232;">        free_workspace=$workspace
</span><span style="color:#323232;">        break # stop loop after finding first available workspace
</span><span style="color:#323232;">    fi
</span><span style="color:#323232;">done
</span><span style="color:#323232;">
</span><span style="color:#323232;">swaymsg move container to workspace number $free_workspace
</span><span style="color:#323232;">swaymsg workspace number $free_workspace
</span>
StrangeAstronomer,

I’ll join in too - here’s mine

Skyhighatrist, (edited )

<span style="color:#323232;">#!/bin/bash
</span><span style="color:#323232;">
</span><span style="color:#323232;">w=$(($(i3-msg -t get_workspaces | tr , 'n' | grep '"num":' | cut -d : -f 2 | sort -rn | head -1)+ 1))
</span><span style="color:#323232;">
</span><span style="color:#323232;">i3-msg move container to workspace $w
</span><span style="color:#323232;">
</span><span style="color:#323232;">i3-msg workspace $w
</span><span style="color:#323232;">
</span><span style="color:#323232;">i3-msg move workspace to output $1
</span>

Here’s my version. It’s a little different than yours and is obviously for i3. I have it bound to display specific shortcuts, so I can send the focused window to a new empty workspace on the display of my choosing.

I also don’t use a set number of workspaces, I have 4 primary, one for each display. And I have shortcuts to switch to the primary on a given display, and then other shortcuts to cycle the active workspace on that display. So the above may not work for everyone, but it is another option. I assume it would require only minor modification to work with Sway.

Edit: I suppose I should provide an explanation. That first line just gets the highest workspace number currently in use and adds 1 to it. Then the rest is pretty straightforward. It moves the container to the the workspace with the new number (i3 creates a new workspace for this) and then brings it into focus, and finally moves it to the display requested.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • swaywm@lemmy.ml
  • DreamBathrooms
  • mdbf
  • ethstaker
  • magazineikmin
  • GTA5RPClips
  • rosin
  • thenastyranch
  • Youngstown
  • osvaldo12
  • slotface
  • khanakhh
  • kavyap
  • InstantRegret
  • Durango
  • provamag3
  • everett
  • cisconetworking
  • Leos
  • normalnudes
  • cubers
  • modclub
  • ngwrru68w68
  • tacticalgear
  • megavids
  • anitta
  • tester
  • JUstTest
  • lostlight
  • All magazines