How you can manage the i3 window manager on multiple computers

Posted on 2020-03-21.

In this tutorial I'll show you a simple way you can manage the configuration of i3 across multiple computers with different setups on each computer.

One of the minor downsides of the i3 tiling manager is that its configuration file isn't scriptable and if you run it on multiple computers with different setups, such as a desktop with a dual monitor setup and a laptop, you have to maintain multiple configuration files.

However, there is a simple way you can get around that.

I use a shell script that contains all the different configuration options. I never touch the i3 configuration file manually, instead I change the options in the shell script and run that. The shell script generates the i3 config file and it has conditions that determine what options to use based on the hostname.

Below is an example of such a shell script. Rather than echoing out every configuration line to the i3 config file, I concatenate all the options in a variable and add a newline. There are several way you can do that, but I prefer the method below. The shell script strives to be POSIX.

#!/bin/sh # # This script generates the i3 config file (v4) # Settings. gaps_inner=10 gaps_outer=0 monitor1="hdmi1" monitor2="hdmi2" background="#1e1e1e" statusline="#aaaaaa" separator="#555555" focused_border="#008fff" focused_background="#007fff" focused_text="#ffffff" active_border="#333333" active_background="#5f676a" active_text="#ffffff" inactive_border="#333333" inactive_background="#222222" inactive_text="#888888" urgent_border="#aa0000" urgent_background="#990000" urgent_text="#ffffff" status_command="i3blocks -c $HOME/.config/i3blocks/i3blocks.conf" cfg_file="$HOME/.config/i3/config" # Get OS and hostname. unamestr=$(uname) hostname=$(hostname) # Initializing the variable. use_gaps=true # I'm using the OS to determine if we need gaps because you might use # different OSs on the same host (dual boot). If you do end up using a compiled # version of i3 gaps on something like Devuan, then you can use something else. if [ "$unamestr" = "Linux" ]; then # Get the distro. # shellcheck disable=SC1091 . /etc/os-release if [ "$NAME" = "Debian GNU/Linux" ] || [ "$NAME" = "Devuan GNU/Linux" ]; then use_gaps=false fi fi if [ -d "$HOME/.config/i3" ]; then # Let's clean up. rm -f "$HOME/.config/i3/*" else mkdir -pv "$HOME/.config/i3" fi options="# i3 config

" options="${options}set \$mod Mod4

" options="${options}font pango:DejaVu Sans Mono 8

" # Use Mouse+$mod to drag floating windows to their wanted position. options="${options}floating_modifier \$mod

" # Disable focus following the mouse. options="${options}focus_follows_mouse no

" # Kill focused window. options="${options}bindsym \$mod+Shift+q kill

" # Chance focus. options="${options}bindsym \$mod+h focus left

" options="${options}bindsym \$mod+j focus down

" options="${options}bindsym \$mod+k focus up

" options="${options}bindsym \$mod+l focus right

" # Move focused window. options="${options}bindsym \$mod+Shift+h move left

" options="${options}bindsym \$mod+Shift+j move down

" options="${options}bindsym \$mod+Shift+k move up

" options="${options}bindsym \$mod+Shift+l move right

" # Split in horizontal orientation. options="${options}bindsym \$mod+Shift+h split h

" # Split in vertical orientation. options="${options}bindsym \$mod+Shift+v split v

" # Enter fullscreen mode for the focused container. options="${options}bindsym \$mod+Shift+f fullscreen toggle

" # Change container layout (stacked, tabbed, toggle split). options="${options}bindsym \$mod+Shift+s layout stacking

" options="${options}bindsym \$mod+Shift+t layout tabbed

" options="${options}bindsym \$mod+Shift+e layout toggle split

" # Toggle tiling / floating. options="${options}bindsym \$mod+Shift+space floating toggle

" # Change focus between tiling / floating windows. options="${options}bindsym \$mod+space focus mode_toggle

" # Focus the parent container. #bindsym $mod+a focus parent # Focus the child container. #bindsym $mod+d focus child # Switch to workspace. options="${options}bindsym \$mod+1 workspace 1

" options="${options}bindsym \$mod+2 workspace 2

" options="${options}bindsym \$mod+3 workspace 3

" options="${options}bindsym \$mod+4 workspace 4

" options="${options}bindsym \$mod+5 workspace 5

" options="${options}bindsym \$mod+6 workspace 6

" options="${options}bindsym \$mod+7 workspace 7

" options="${options}bindsym \$mod+8 workspace 8

" options="${options}bindsym \$mod+9 workspace 9

" options="${options}bindsym \$mod+0 workspace 10

" # Move focused container to workspace. options="${options}bindsym \$mod+Shift+1 move container to workspace 1

" options="${options}bindsym \$mod+Shift+2 move container to workspace 2

" options="${options}bindsym \$mod+Shift+3 move container to workspace 3

" options="${options}bindsym \$mod+Shift+4 move container to workspace 4

" options="${options}bindsym \$mod+Shift+5 move container to workspace 5

" options="${options}bindsym \$mod+Shift+6 move container to workspace 6

" options="${options}bindsym \$mod+Shift+7 move container to workspace 7

" options="${options}bindsym \$mod+Shift+8 move container to workspace 8

" options="${options}bindsym \$mod+Shift+9 move container to workspace 9

" options="${options}bindsym \$mod+Shift+0 move container to workspace 10

" # Running with i3-gaps. if [ $use_gaps ]; then options="${options}gaps inner $gaps_inner

" options="${options}gaps outer $gaps_outer

" fi # Disable title bars. options="${options}for_window [class=\"^.*\"] border pixel 1

" options="${options}bindsym \$mod+d exec dmenu_run -i -l 20

" options="${options}bindsym \$mod+Return exec xterm

" options="${options}bindsym \$mod+c exec claws-mail

" options="${options}bindsym \$mod+o exec falkon

" options="${options}bindsym \$mod+f exec firefox

" options="${options}bindsym \$mod+g exec gimp

" options="${options}bindsym \$mod+z exec filezilla

" # Assign to floating by default. options="${options}for_window [class=\"mpv\"] floating enable

" options="${options}for_window [class=\"gnome-calculator\"] floating enable

" # Let's try to get most pop-up floating. options="${options}for_window [window_role=\"pop-up\"] floating enable

" options="${options}for_window [window_role=\"bubble\"] floating enable

" options="${options}for_window [window_role=\"task_dialog\"] floating enable

" options="${options}for_window [window_role=\"Preferences\"] floating enable

" options="${options}for_window [window_type=\"dialog\"] floating enable

" options="${options}for_window [window_type=\"menu\"] floating enable

" # Jump to the latest "urgent" window. options="${options}bindsym \$mod+u [urgent=latest] focus

" # Reload the configuration file. options="${options}bindsym \$mod+Shift+w reload

" # Restart i3 inplace (preserves your layout/session, can be used to upgrade i3). options="${options}bindsym \$mod+Shift+Escape restart

" # Resize window (you can also use the mouse for that). options="${options}mode resize {

" options="${options} bindsym h resize shrink width 10 px or 10 ppt

" options="${options} bindsym j resize grow height 10 px or 10 ppt

" options="${options} bindsym k resize shrink height 10 px or 10 ppt

" options="${options} bindsym l resize grow width 10 px or 10 ppt

" # Back to normal: Enter or Escape. options="${options} bindsym Return mode default

" options="${options} bindsym Escape mode default

" options="${options}}

" options="${options}bindsym \$mod+Shift+r mode resize

" if [ "$hostname" = "foo" ]; then # Place workspace. options="${options}workspace 1 output $monitor1

" options="${options}workspace 2 output $monitor2

" options="${options}bar {

" options="${options} output $monitor1

" options="${options} position top

" options="${options} colors {

" options="${options} background $background

" options="${options} statusline $statusline

" options="${options} separator $separator

" options="${options} focused_workspace $focused_border $focused_background $focused_text

" options="${options} active_workspace $active_border $active_background $active_text

" options="${options} inactive_workspace $inactive_border $inactive_background $inactive_text

" options="${options} urgent_workspace $urgent_border $urgent_background $urgent_text

" options="${options} }

" options="${options}}

" options="${options}bar {

" options="${options} output $monitor2

" options="${options} position top

" options="${options} colors {

" options="${options} background $background

" options="${options} statusline $statusline

" options="${options} separator $separator

" options="${options} focused_workspace $focused_border $focused_background $focused_text

" options="${options} active_workspace $active_border $active_background $active_text

" options="${options} inactive_workspace $inactive_border $inactive_background $inactive_text

" options="${options} urgent_workspace $urgent_border $urgent_background $urgent_text

" options="${options} }

" options="${options} status_command $status_command

" options="${options}}

" else options="${options}bar {

" options="${options} position top

" options="${options} colors {

" options="${options} background $background

" options="${options} statusline $statusline

" options="${options} separator $separator

" options="${options} focused_workspace $focused_border $focused_background $focused_text

" options="${options} active_workspace $active_border $active_background $active_text

" options="${options} inactive_workspace $inactive_border $inactive_background $inactive_text

" options="${options} urgent_workspace $urgent_border $urgent_background $urgent_text

" options="${options} }

" options="${options} status_command $status_command

" options="${options}}

" fi printf "$options%s" > "$cfg_file" if [ -f "$cfg_file" ]; then echo "Created i3 config file" else echo "Could not write config file" fi

If you have any comments or corrections please feel free to email them to me. Also, if you found this content useful consider supporting me on Patreon