How you can manage the i3 window manager on multiple computers

Published 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\n"

options="${options}set \$mod Mod4\n"
options="${options}font pango:DejaVu Sans Mono 8\n"

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

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

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

# Chance focus.
options="${options}bindsym \$mod+h focus left\n"
options="${options}bindsym \$mod+j focus down\n"
options="${options}bindsym \$mod+k focus up\n"
options="${options}bindsym \$mod+l focus right\n"

# Move focused window.
options="${options}bindsym \$mod+Shift+h move left\n"
options="${options}bindsym \$mod+Shift+j move down\n"
options="${options}bindsym \$mod+Shift+k move up\n"
options="${options}bindsym \$mod+Shift+l move right\n"

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

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

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

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

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

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

# 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\n"
options="${options}bindsym \$mod+2 workspace 2\n"
options="${options}bindsym \$mod+3 workspace 3\n"
options="${options}bindsym \$mod+4 workspace 4\n"
options="${options}bindsym \$mod+5 workspace 5\n"
options="${options}bindsym \$mod+6 workspace 6\n"
options="${options}bindsym \$mod+7 workspace 7\n"
options="${options}bindsym \$mod+8 workspace 8\n"
options="${options}bindsym \$mod+9 workspace 9\n"
options="${options}bindsym \$mod+0 workspace 10\n"

# Move focused container to workspace.
options="${options}bindsym \$mod+Shift+1 move container to workspace 1\n"
options="${options}bindsym \$mod+Shift+2 move container to workspace 2\n"
options="${options}bindsym \$mod+Shift+3 move container to workspace 3\n"
options="${options}bindsym \$mod+Shift+4 move container to workspace 4\n"
options="${options}bindsym \$mod+Shift+5 move container to workspace 5\n"
options="${options}bindsym \$mod+Shift+6 move container to workspace 6\n"
options="${options}bindsym \$mod+Shift+7 move container to workspace 7\n"
options="${options}bindsym \$mod+Shift+8 move container to workspace 8\n"
options="${options}bindsym \$mod+Shift+9 move container to workspace 9\n"
options="${options}bindsym \$mod+Shift+0 move container to workspace 10\n"

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

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

options="${options}bindsym \$mod+d exec dmenu_run -i -l 20\n"
options="${options}bindsym \$mod+Return exec xterm\n"
options="${options}bindsym \$mod+c exec claws-mail\n"
options="${options}bindsym \$mod+o exec falkon\n"
options="${options}bindsym \$mod+f exec firefox\n"
options="${options}bindsym \$mod+g exec gimp\n"
options="${options}bindsym \$mod+z exec filezilla\n"

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

# Let's try to get most pop-up floating.
options="${options}for_window [window_role=\"pop-up\"] floating enable\n"
options="${options}for_window [window_role=\"bubble\"] floating enable\n"
options="${options}for_window [window_role=\"task_dialog\"] floating enable\n"
options="${options}for_window [window_role=\"Preferences\"] floating enable\n"
options="${options}for_window [window_type=\"dialog\"] floating enable\n"
options="${options}for_window [window_type=\"menu\"] floating enable\n"

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

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

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

# Resize window (you can also use the mouse for that).
options="${options}mode resize {\n"
options="${options}    bindsym h resize shrink width 10 px or 10 ppt\n"
options="${options}    bindsym j resize grow height 10 px or 10 ppt\n"
options="${options}    bindsym k resize shrink height 10 px or 10 ppt\n"
options="${options}    bindsym l resize grow width 10 px or 10 ppt\n"

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

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

if [ "$hostname" = "foo" ]; then
    # Place workspace.
    options="${options}workspace 1 output $monitor1\n"
    options="${options}workspace 2 output $monitor2\n"
    options="${options}bar {\n"
    options="${options}    output $monitor1\n"
    options="${options}    position top\n"
    options="${options}    colors {\n"
    options="${options}        background $background\n"
    options="${options}        statusline $statusline\n"
    options="${options}        separator  $separator\n"
    options="${options}        focused_workspace  $focused_border $focused_background $focused_text\n"
    options="${options}        active_workspace   $active_border $active_background $active_text\n"
    options="${options}        inactive_workspace $inactive_border $inactive_background $inactive_text\n"
    options="${options}        urgent_workspace   $urgent_border $urgent_background $urgent_text\n"
    options="${options}    }\n"
    options="${options}}\n"

    options="${options}bar {\n"
    options="${options}    output $monitor2\n"
    options="${options}    position top\n"
    options="${options}    colors {\n"
    options="${options}        background $background\n"
    options="${options}        statusline $statusline\n"
    options="${options}        separator  $separator\n"
    options="${options}        focused_workspace  $focused_border $focused_background $focused_text\n"
    options="${options}        active_workspace   $active_border $active_background $active_text\n"
    options="${options}        inactive_workspace $inactive_border $inactive_background $inactive_text\n"
    options="${options}        urgent_workspace   $urgent_border $urgent_background $urgent_text\n"
    options="${options}    }\n"
    options="${options}    status_command $status_command\n"
    options="${options}}\n"
else
    options="${options}bar {\n"
    options="${options}    position top\n"
    options="${options}    colors {\n"
    options="${options}        background $background\n"
    options="${options}        statusline $statusline\n"
    options="${options}        separator  $separator\n"
    options="${options}        focused_workspace  $focused_border $focused_background $focused_text\n"
    options="${options}        active_workspace   $active_border $active_background $active_text\n"
    options="${options}        inactive_workspace $inactive_border $inactive_background $inactive_text\n"
    options="${options}        urgent_workspace   $urgent_border $urgent_background $urgent_text\n"
    options="${options}    }\n"
    options="${options}    status_command $status_command\n"
    options="${options}}\n"
fi

printf "$options%s" > "$cfg_file"

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