Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swap i3 displays / workspaces between displays
#!/bin/sh
# requires jq
stick_to="name" && cmd="workspace" # stick to the workspace
# stick_to="output" && cmd="focus output" # stick to the screen
initial=$(
i3-msg -t get_workspaces |
jq -r ".[] | select(.focused == true) | .$stick_to"
)
py_script=$(cat <<EOF
import sys
from numpy import roll
s, w = [], []
for l in sys.stdin.readlines():
screen, workspace = l.strip().split(':', 1)
s.append(screen)
w.append(workspace)
for line in zip(s, roll(s,1), w, roll(w, -1)):
print(' '.join(line))
EOF
)
i3-msg -t get_outputs |
jq -r '.[] | select(.active == true) | "\(.name):\(.current_workspace)"' |
python3 -c "$py_script" |
while read -r screen_src screen_dst workspace_tgt workspace_final
do
i3-msg -- workspace --no-auto-back-and-forth "$workspace_tgt"
i3-msg move workspace to output "$screen_dst"
i3-msg focus output "$screen_src"
i3-msg -- workspace --no-auto-back-and-forth "$workspace_final"
echo "moved $workspace_tgt to $screen_dst"
echo "and restored focus to $workspace_final"
done
i3-msg $cmd $initial
@Zackhardtoname
Copy link

Zackhardtoname commented Nov 14, 2020

It doesn't work for me. Does it still work for you?

@TitouanT
Copy link
Author

TitouanT commented Nov 14, 2020

@Zackhardtoname I don't use multiple screens very often, what is the issue ? I can test it again.

@TitouanT
Copy link
Author

TitouanT commented Nov 14, 2020

@Zackhardtoname I made a revision to make the code clearer, maybe it will work for you now.
I tested it before and after and both are working for me.

@Zackhardtoname
Copy link

Zackhardtoname commented Nov 14, 2020

I got

Traceback (most recent call last):
File "", line 5, in
ValueError: too many values to unpack (expected 2)

I have tried calling it both inside and outside i3 config
the latter:
bindsym $mod+m exec --no-startup-id /home/zack/Downloads/test.sh 2> /home/zack/Downloads/error.txt&

My output of l.strip().split(':'):

Traceback (most recent call last):
File "", line 7, in
ValueError: too many values to unpack (expected 2)
ERROR: Your command: workspace --no-auto-back-and-forth
ERROR:
ERROR: Expected one of these tokens: '--no-auto-back-and-forth', 'next_on_output', 'prev_on_output', 'next', 'prev', 'back_and_forth', 'number',
[{"success":false,"parse_error":true,"error":"Expected one of these tokens: '--no-auto-back-and-forth', 'next_on_output', 'prev_on_output', 'next', 'prev', 'back_and_forth', 'number', ","input":"workspace --no-auto-back-and-forth ","errorposition":" "}]
ERROR: Your command: move workspace to output
ERROR:
ERROR: Expected one of these tokens: 'output',
[{"success":false,"parse_error":true,"error":"Expected one of these tokens: 'output', ","input":"move workspace to output ","errorposition":" "}]
ERROR: Output ['DP-2' not found.
ERROR: Your command: focus output ['DP-2', '1', '1', '\uf268']
ERROR: ^^^^^^^^^^^^^^^^^^^
ERROR: Expected one of these tokens: , '[', 'move', 'exec', 'exit', 'restart', 'reload', 'shmlog', 'debuglog', 'border', 'layout', 'append_layout', 'workspace', 'focus', 'kill', 'open', 'fullscreen', 'sticky', 'split', 'floating', 'mark', 'unmark', 'resize', 'rename', 'nop', 'scratchpad', 'swap', 'title_format', 'mode', 'bar', 'gaps'
[{"success":false,"error":"Output ['DP-2' not found."},{"success":false,"parse_error":true,"error":"Expected one of these tokens: , '[', 'move', 'exec', 'exit', 'restart', 'reload', 'shmlog', 'debuglog', 'border', 'layout', 'append_layout', 'workspace', 'focus', 'kill', 'open', 'fullscreen', 'sticky', 'split', 'floating', 'mark', 'unmark', 'resize', 'rename', 'nop', 'scratchpad', 'swap', 'title_format', 'mode', 'bar', 'gaps'","input":"focus output ['DP-2', '1', '1', '\uf268']","errorposition":" ^^^^^^^^^^^^^^^^^^^"}]
ERROR: Your command: workspace --no-auto-back-and-forth
ERROR:
ERROR: Expected one of these tokens: '--no-auto-back-and-forth', 'next_on_output', 'prev_on_output', 'next', 'prev', 'back_and_forth', 'number',
[{"success":false,"parse_error":true,"error":"Expected one of these tokens: '--no-auto-back-and-forth', 'next_on_output', 'prev_on_output', 'next', 'prev', 'back_and_forth', 'number', ","input":"workspace --no-auto-back-and-forth ","errorposition":" "}]
moved to
and restored focus to
[{"success":true}]

Thanks!

@TitouanT
Copy link
Author

TitouanT commented Nov 15, 2020

mmmh it looks like it's an issue with IFS=:
Now that I think about it, it's not even needed to set it to : since it was for the final read and since I use a python script I can just use a space as separator.
I don't understand why it's not working for you though, what shell do you use ? I have tried with both dash and bash.

Incase that's not it, give me the output of

i3-msg -t get_outputs |
jq -r '.[] | select(.active == true) | "\(.name):\(.current_workspace)"' 

@Zackhardtoname
Copy link

Zackhardtoname commented Nov 15, 2020

I normally use zsh but I tried it both for zsh and bash and got the same error...

@Zackhardtoname
Copy link

Zackhardtoname commented Nov 15, 2020

The output I got:

DP-2:1:1:
DP-4:3:3:

I used icons in my ws names, which is not displaying on GitHub, bu shows like this for me.

image

Maybe that's the issue? Also somehow my only two monitors are named DP-2 and DP-4...

@TitouanT
Copy link
Author

TitouanT commented Nov 15, 2020

I meant what shell /bin/sh points to you can see which it is with realpath /bin/sh

@TitouanT
Copy link
Author

TitouanT commented Nov 15, 2020

yeah thats the issue

@TitouanT
Copy link
Author

TitouanT commented Nov 15, 2020

Can you reference them by there number, for example does this put focus on your chrome workspace ?
i3-msg -- workspace --no-auto-back-and-forth "1"

If yes then just change screen, workspace = l.strip().split(':') to screen, workspace = l.strip().split(':')[:2]

If it works then I will add it to the gist.

@Zackhardtoname
Copy link

Zackhardtoname commented Nov 15, 2020

Let me see. The original script this is forked from did not give me that error.

@TitouanT
Copy link
Author

TitouanT commented Nov 15, 2020

@Zackhardtoname it should work now ! thanks for your feedback, I keep my workspaces named with numbers so I never ran into this issue.
The problem here was that you have : in your workspace name and that's the symbol I use to split.

@Zackhardtoname
Copy link

Zackhardtoname commented Nov 15, 2020

That wouldn't and didn't work because somehow i3 treats ws "3" and "3:" differently.
This works: screen, workspace = l.strip().split(':', maxsplit=1)

Thank you so much! I should have dug this more myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment