Skip to content

Instantly share code, notes, and snippets.

@raz0229
Created August 14, 2022 07:35
Show Gist options
  • Select an option

  • Save raz0229/14a88fc5d5d54d81145b7a2f9cc7bcb9 to your computer and use it in GitHub Desktop.

Select an option

Save raz0229/14a88fc5d5d54d81145b7a2f9cc7bcb9 to your computer and use it in GitHub Desktop.
Creating a systemd service for i3wm to automatically split workspaces in 2 columns so windows are stacked on top of each other
Save the script in /usr/bin/spliti3 and make sure you make it executable using:
sudo chmod +x /usr/bin/spliti3
(Replace socket path on line 34 of script)
Create a systemd service using:
sudo nano /etc/systemd/system/spliti3.service
(Replace your username on line 7 of service)
Then, simply:
sudo systemctl enable spliti3 --now
sudo systemctl status spliti3
(See status for any errors)
#!/usr/bin/python3
# Save it in /usr/bin as sudo and make sure you make it executable using:
# sudo chmod +x /usr/bin/spliti3
from i3ipc import Connection, Event
import subprocess
COLUMNS = 2 # Desired columns
def move_container (con1, con2):
con2.command("mark __column-layout");
con1.command("move window to mark __column-layout")
con2.command("unmark __column-layout");
def layout (i3, event):
if event.change == "close":
for reply in i3.get_workspaces():
if reply.focused:
workspace = i3.get_tree().find_by_id(reply.ipc_data["id"]).workspace()
if len(workspace.nodes) == 1 and len(workspace.nodes[0].nodes) == 1:
child = workspace.nodes[0].nodes[0]
move_container(child, workspace)
else:
window = i3.get_tree().find_by_id(event.container.id)
if window is not None:
workspace = window.workspace()
if workspace is not None and len(workspace.nodes) >= COLUMNS:
for node in workspace.nodes:
if node.layout != "splitv":
node.command("splitv")
i3 = Connection('/run/user/1000/i3/ipc-socket.662') # output of "i3 --get-socketpath"
# Or simply uncomment the line below and it will automatically find the socket path
# i3 = Connection((subprocess.check_output(['i3','--get-socketpath'])).decode().strip())
i3.on(Event.WINDOW_NEW, layout)
i3.on(Event.WINDOW_CLOSE, layout)
i3.on(Event.WINDOW_MOVE, layout)
i3.main()
[Unit]
Description=i3 workspace manager
After=network.target
[Service]
Type=simple
User=<whoami>
ExecStart=/usr/bin/spliti3
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment