Skip to content

Instantly share code, notes, and snippets.

@tadly tadly/swapper.sh
Last active Mar 27, 2018

Embed
What would you like to do?
Allows to quickly swap two workspaces with one another.
#!/usr/bin/env bash
#
# Allows to quickly swap two workspaces with one another.
#
# Requirements:
# i3, jq
#
# i3 config example:
# set $exec_nsi exec --no-startup-id
#
# set $swapper path/to/swapper.sh
# set $swapper_mode Workspace swapper
# bindsym $mod+s mode "$swapper"
# mode "$swapper_mode" {
# bindsym 1 $exec_nsi $swapper $ws1; mode "default"
# bindsym 2 $exec_nsi $swapper $ws2; mode "default"
# bindsym 3 $exec_nsi $swapper $ws3; mode "default"
# bindsym 4 $exec_nsi $swapper $ws4; mode "default"
# bindsym 5 $exec_nsi $swapper $ws5; mode "default"
# bindsym 6 $exec_nsi $swapper $ws6; mode "default"
# bindsym 7 $exec_nsi $swapper $ws7; mode "default"
# bindsym 8 $exec_nsi $swapper $ws8; mode "default"
# bindsym 9 $exec_nsi $swapper $ws9; mode "default"
# bindsym 0 $exec_nsi $swapper $ws0; mode "default"
#
# bindsym Return mode "default"
# bindsym Escape mode "default"
# }
# Path to jq binary. Can be obtained from:
# https://stedolan.github.io/jq/
JQ_BIN='./jq'
# Name used for a temporary workspace while swapping.
WS_TMP="_tmp_"
cd "$(dirname "$0")"
ws_target="$1"
if [ -z "$ws_target" ]; then
echo "usage: $0 [target]"
exit 1
fi
# Get all workspaces
workspaces="$(i3-msg -t get_workspaces)"
# Get current workspace
ws_current=$(echo "$workspaces" | \
$JQ_BIN -r '.[] | select(.focused == true).name')
# Check if moving to same workspace
if [ "$ws_target" == "$ws_current" ]; then
exit 0
fi
# Check if the target workspace already exists
to_node=$(echo "$workspaces" | \
$JQ_BIN -r ".[] | select(.name == \"$ws_target\")")
# Either swap current with target or simply rename current
if [ -z "$to_node" ]; then
i3-msg "rename workspace to \"$ws_target\""
else
i3-msg "rename workspace \"$ws_current\" to \"$WS_TMP\""
i3-msg "rename workspace \"$ws_target\" to \"$ws_current\""
i3-msg "rename workspace \"$WS_TMP\" to \"$ws_target\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.