Moving/Closing All Windows In Current Workspace

I would like to set a keybind that moves all windows contained in the current workspace to another workspace. The following example is wrong, but that is what I want to achieve.

function allWindowsToWorkspace(destination)
    local currentWorkspaceID = hl.get_active_workspace().id
    local windowsInCurrentWorkspace = hl.get_workspace_windows(currentWorkspaceID)

    for window, info in pairs(windowsInCurrentWorkspace) do
        return hl.dsp.window.move({ workspace = destination, window = info.id })
    end
end

for i = 1, 10 do
    local i = i % 10
    hl.bind( "SUPER + ALT + " .. i, allWindowsToWorkspace(i))
end

I remember having some keybinds for this in Sway:

bindsym $mod+shift+c [workspace=__focused__] kill

bindsym $mod+ctrl+alt+1 [workspace=__focused__] move to workspace number 1
bindsym $mod+ctrl+alt+2 [workspace=__focused__] move to workspace number 2

Just to be clear, I do not want to swap workspaces.

Any hints would be appreciated, thanks in advance.

The biggest issue will be to preserve positions, afaik hyprland doesn’t natively have a solution for that

I understand you want a one key shortcut but personally if I want to move my windows from one workspace to another I just press a few times SUPER+SHIFT+new_workspace_number and since it auto-select leftover windows then whole ordeal is done in around 0.69 seconds

I don’t mind the positions too much, but I do the same as you but only take 0.420 secs.

Thanks for replying :)

american-psycho-impressive

3 Likes

Here goes my solution., forgot to upload it once I figure it out.

local function closeAllWindows()
	local windows = hl.get_windows()

	for _,w in pairs(windows) do
		hl.dispatch(hl.dsp.window.close({ window = w }))
	end
end

local function closeAllWindowsCurrentWorkspace()
	local cws = hl.get_active_workspace()
	local windows = hl.get_windows({ workspace = cws })

	for _,w in pairs(windows) do
		hl.dispatch(hl.dsp.window.close({ window = w }))
	end
end

-- Probably add another mod key for safety reasons.
hl.bind("SUPER+BackSpace", closeAllWindowsCurrentWorkspace )
hl.bind("SUPER+Delete", closeAllWindows )

And for moving the windows:

local function moveWindowsCurrentWorkspace(ws, f)
	local cws = hl.get_active_workspace()
	local windows = hl.get_windows({ workspace = cws })

	for _,w in pairs(windows) do
		hl.dispatch(
			hl.dsp.window.move({
				window = w ,
				workspace = ws ,
				follow = f
			})
		)
	end
end

for i = 1, 10 do
	local key = i % 10
	hl.bind("SUPER+SHIFT+ALT+" .. key, function()
		moveWindowsCurrentWorkspace(i, true)
	end)
end

for i = 1, 10 do
	local key = i % 10
	hl.bind("SUPER+CTRL+ALT+" .. key, function()
		moveWindowsCurrentWorkspace(i, false)
	end)
end

You could probably go for a more "compact approach for the “close all windows on current workspace”.

-- Close all windows on current workspace
hl.bind("SUPER+BackSpace", function()
	local wins = hl.get_workspace_windows(hl.get_active_workspace())
	for _, win in ipairs(wins) do
		hl.dispatch(hl.dsp.window.close({ window = win }))
	end
end)

I don’t use “move all” but the logic is more or less the same.

Thanks! I missed that function, that makes it easier.