Switching between layouts in i3

Following my previous post on how to define, save and restore layouts, in this post I’ll show you a way to quickly switch between layouts.

As shown in the last post not only is it possible to load a desired workspace layout, but also you can automatically populate the placeholders with applications of your choice. Imagine you’ve created two layouts: workspace_1-basic.json and workspace_1-generic.json.

You can create two short scripts to load the layouts and populate them with programs. Please note that we need to kill all the existing windows in the workspace. Otherwise, the loaded layout will append itself next to the existing windows on the workspace. It’s not the most graceful way of killing windows but I couldn’t find any other way of doing it. Furthermore, do not forget to add the trailing & to indicate that the job needs to be executed in the background. Otherwise, the first program will block loading any subsequent ones.

~/.i3/i3-load.basic.sh

# Kill all the existing containers
# Add more instances of the 'focus parent' command if you ever have more containers
i3-msg 'workspace 1; focus parent; focus parent; focus parent; focus parent; focus parent; kill'
i3-msg 'workspace 1; append_layout /home/sycamorex/.i3/workspace_1-basic.json'

# Start programs
xterm -name topleft -e 'ncmpcpp' &
xterm -name bottomleft &
xterm -name topright -e 'emacs -nw' &
xterm -name bottomright &

~/.i3/i3-load.generic.sh

# Kill all the existing containers
# Add more instances of the 'focus parent' command if you ever have more containers
i3-msg 'workspace 1; focus parent; focus parent; focus parent; focus parent; focus parent; focus parent; kill'
i3-msg 'workspace 1; append_layout /home/sycamorex/.i3/workspace_1-generic.json'

# start programs
xterm -name topleft -e 'ncmpcpp' &
xterm -name middleleft -e 'irssi' &
xterm -name bottomleft -e 'mc' &
xterm -name topright -e 'htop' &
xterm -name bottomright -e 'emacs -nw'

We can bind them to keys in i3 main config file:
~/.i3/config

bindsym $mod+g exec /home/sycamorex/.i3/i3-load-generic.sh
bindsym $mod+b exec /home/sycamorex/.i3/i3-load-basic.sh

Please note that the layout feature is still fresh. It hasn’t even reached the stable branch of i3 yet. As far as I know, it’ll be part of version 4.8 of i3 (whenever it’s released). See my previous post on how to install the development branch of i3 that will let utilise this feature.

Happy tiling!!!

Leave a Reply

Your email address will not be published. Required fields are marked *