Fluxbox quickly add/kill workspaces

by moorray

I like the idea of automatic workspace creation and recycling. AwesomeWM does have it, KDE has activities which I suppose are even more powerful. But I use fluxbox which doesn’t support this feature out of box. Or does it? All you need to do is write a few lines of code in your favourite scripting language. Here is my version in BASH using wmctrl and fluxbox-remote commands:



# Add WS
fluxbox-remote addworkspace

# Switch to it
wmctrl -s $(wmctrl -d | tail -1 | cut -d' ' -f1)

# Rename
fluxbox-remote setworkspacenamedialog

When adding a workspace I need to find out its number, beware of numbering in wmctrl, it’s zero based.



# Find out current WS no.
ws_no=$(wmctrl -d | awk '{ if ($2 == "*") printf $1; }')

# Kill all windows on current WS.
for win_id in $(wmctrl -l | grep "^[0-9a-fx]*  $ws_no" | cut -d' ' -f1)
    wmctrl -i -c $win_id

max_ws=$(wmctrl -d | tail -1 | cut -f1 -d' ')
for ws in `seq $ws_no $((max_ws-1))`
    # Rename later WSes
    wmctrl -s $ws
    next_ws_name=$(wmctrl -d | grep "^$((ws+1))" | sed -e 's/.*  \(.*\)/\1/')
    fluxbox-remote "setworkspacename $next_ws_name"

    # Move windows
    for win in $(wmctrl -l | grep "^[0-9a-fx]*  $((ws+1))" | cut -d' ' -f1)
        wmctrl -i -R $win

wmctrl -s $max_ws
fluxbox-remote removelastworkspace

Removing is a little bit complicated since fluxbox can only remove last workspace and will not close windows on it (which I want) just move them to other workspace instead. I also have to take care of workspace names manually.

Remember that you have to set session.screen0.allowRemoteActions to true to use fluxbox-remote!