Swap every windows between workspaces in awesome wm

awesome is the tiling windows manager I use daily and I just wrote a very useful (at least for me) function for it. Basically it swaps every client (similar to windows) between the current tag (similar to workspace) and the target tag. Normally I put terminal clients for my current task at tag#3, right next to my Emacs-only tag#2 so I can quickly switch between browser (#1), editor and terminals. Now if I want to switch to a different task I just need to swap in terminals from another tag while swap out current terminals to that tag.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-- i is the index of target tag in variable `tags'
function ()
local screen = mouse.screen
local from = client.focus:tags()[1]
local to = tags[screen][i]
if to then
t = to:clients()
for i, c in ipairs(from:clients()) do
awful.client.movetotag(to, c)
end
for i, c in ipairs(t) do
awful.client.movetotag(from, c)
end
end
end

I put this under

1
2
3
4
5
6
7
8
9
10
11
12
13
-- Bind all key numbers to tags.
-- Be careful: we use keycodes to make it works on any keyboard layout.
-- This should map on the top row of your keyboard, usually 1 to 9.
for i = 1, keynumber do
globalkeys = awful.util.table.join(globalkeys,
awful.key({ modkey }, "#" .. i + 9,
function ()
local screen = mouse.screen
if tags[screen][i] then
awful.tag.viewonly(tags[screen][i])
end
end),
awful.key({ modkey, "Control" }, "#" .. i + 9,

in my copy of default rc.lua.