Switching to the i3 window manager

Vincent Bernat

I have been us­ing the awe­some win­dow man­ager for 10 years. It is a tiling win­dow man­ager, con­fig­urable and ex­tend­able with the Lua lan­guage. Us­ing a gen­eral-pur­pose pro­gram­ming lan­guage to con­fig­ure ev­ery as­pect is a dou­ble-edged sword. Due to lazi­ness and the ap­par­ent dif­fi­culty of adapt­ing my con­fig­u­ra­tion—about 3000 lines—to newer re­leases, I was stuck with the 3.4 ver­sion, whose last re­lease is from 2013.

It was time for a re­write. In­stead, I have switched to the i3 win­dow man­ager, lured by the pos­si­bil­ity to mi­grate to Way­land and Sway later with min­i­mal pain. Us­ing an em­bed­ded in­ter­preter for con­fig­u­ra­tion is not as im­por­tant to me as it was in the past: it brings both com­plex­ity and brit­tleness.

i3 dual screen setup
Dual screen desktop running i3, Emacs, some terminals, including a Quake console, Firefox, Polybar as the status bar, and Dunst as the notification daemon.

The win­dow man­ager is only one part of a desk­top en­vi­ron­ment. There are sev­eral op­tions for the other com­po­nents. I am also in­tro­duc­ing them in this post.

i3: the window manager#

i3 aims to be a min­i­mal tiling win­dow man­ager. Its doc­u­men­ta­tion can be read from top to bot­tom in less than an hour. i3 or­ga­nizes win­dows in a tree. Each non-leaf node con­tains one or sev­eral win­dows and has an ori­en­ta­tion and a lay­out. This in­for­ma­tion ar­bi­trates the win­dow po­si­tions. i3 fea­tures three lay­outs: split, stack­ing, and tabbed. They are demon­strated in the be­low screenshot:

Example of layouts
Demonstration of the layouts available in i3. The main container is split horizontally. The first child is split vertically. The second one is tabbed. The last one is stacking.
Tree representation of the previous
screenshot
Tree representation of the previous screenshot.

Most of the other tiling win­dow man­agers, in­clud­ing the awe­some win­dow man­ager, use pre­de­fined lay­outs. They usu­ally fea­ture a large area for the main win­dow and an­other area di­vided among the re­main­ing win­dows. These lay­outs can be tuned a bit, but you mostly stick to a cou­ple of them. When a new win­dow is added, the be­hav­ior is quite pre­dictable. More­over, you can cy­cle through the var­i­ous win­dows with­out think­ing too much as they are ordered.

i3 is more flex­i­ble with its abil­ity to build any lay­out on the fly, but it can feel quite over­whelm­ing as you need to vi­su­al­ize the tree in your head. At first, it is not un­usual to find your­self with a com­plex tree with many use­less nested con­tain­ers. More­over, you have to nav­i­gate win­dows us­ing di­rec­tions. It takes some time to get used to.

I set up a split lay­out for Emacs and a few ter­mi­nals, but most of the other workspaces are us­ing a tabbed lay­out. I do not use the stack­ing lay­out. You can find many scripts try­ing to em­u­late other tiling win­dow man­agers, but I did try to keep my setup free of these at­tempts and get a chance to fa­mil­iar­ize my­self. i3 can also save and re­store lay­outs, which is quite a pow­er­ful feature.

My con­fig­u­ra­tion is quite sim­i­lar to the de­fault one and has less than 200 lines.

i3 companion: the missing bits#

i3 phi­los­o­phy is to keep a min­i­mal core and let the user im­ple­ment miss­ing fea­tures us­ing the IPC protocol:

Do not add fur­ther com­plex­ity when it can be avoided. We are gen­er­ally happy with the fea­ture set of i3 and in­stead fo­cus on fix­ing bugs and main­tain­ing it for sta­bil­ity. New fea­tures will there­fore only be con­sid­ered if the ben­e­fit out­weighs the ad­di­tional com­plex­ity, and we en­cour­age users to im­ple­ment fea­tures us­ing the IPC when­ever possible.

— In­tro­duc­tion to the i3 win­dow manager

While this is not as pow­er­ful as an em­bed­ded lan­guage, it is enough for many cases. More­over, as high-level fea­tures may be opin­ion­ated, del­e­gat­ing them to small, loosely cou­pled pieces of code keeps them more main­tain­able. Li­braries ex­ist for this pur­pose in sev­eral lan­guages. Users have pub­lished many scripts to ex­tend i3: au­to­matic lay­out and win­dow pro­mo­tion to mimic the be­hav­ior of other tiling win­dow man­agers, win­dow swal­low­ing to put a new app on top of the ter­mi­nal launch­ing it, and cy­cling be­tween win­dows with Alt+Tab.

In­stead of main­tain­ing a script for each fea­ture, I have cen­tral­ized ev­ery­thing into a sin­gle Python process, i3-companion us­ing asyn­cio and the i3ipc-python li­brary. Each fea­ture is self-con­tained into a func­tion. It im­ple­ments the fol­low­ing com­ponents:

Make a workspace ex­clu­sive to an ap­pli­ca­tion
When a workspace con­tains Emacs or Fire­fox, I would like other ap­pli­ca­tions to move to an­other workspace, ex­cept for the ter­mi­nal which is al­lowed to “in­trude” into any workspace. The workspace_​exclusive() func­tion mon­i­tors new win­dows and moves them if needed to an empty workspace or to one with the same ap­pli­ca­tion al­ready running.
Im­ple­ment a Quake con­sole
The quake_​console() func­tion im­ple­ments a drop-down con­sole avail­able from any workspace. It can be tog­gled with Mod+`. This is im­ple­mented as a scratch­pad window.
Back and forth workspace switch­ing on the same out­put
With the workspace back_​and_​forth com­mand, we can ask i3 to switch to the pre­vi­ous workspace. How­ever, this fea­ture is not re­stricted to the cur­rent out­put. I pre­fer to have one key­bind­ing to switch to the workspace on the next out­put and one key­bind­ing to switch to the pre­vi­ous workspace on the same out­put. This be­hav­ior is im­ple­mented in the previous_​workspace() func­tion by keep­ing a per-out­put his­tory of the fo­cused workspaces.
Cre­ate a new empty workspace or move a win­dow to an empty workspace
To cre­ate a new empty workspace or move a win­dow to an empty workspace, you have to lo­cate a free slot and use workspace number 4 or move container to workspace number 4. The new_​workspace() func­tion finds a free num­ber and uses it as the tar­get workspace.
Restart some ser­vices on out­put change
When adding or re­mov­ing an out­put, some ac­tions need to be ex­e­cuted: re­fresh the wall­pa­per, restart some com­po­nents un­able to adapt their con­fig­u­ra­tion on their own, etc. i3 trig­gers an event for this pur­pose. The output_​update() func­tion also takes an ex­tra step to co­a­lesce mul­ti­ple con­sec­u­tive events and to check if there is a real change with the low-level li­brary xcffib.

I will de­tail the other fea­tures as this post goes on. On the tech­ni­cal side, each func­tion is dec­o­rated with the events it should react to:

@on(CommandEvent("previous-workspace"), I3Event.WORKSPACE_FOCUS)
async def previous_workspace(i3, event):
    """Go to previous workspace on the same output."""

The CommandEvent() event class is my way to send a com­mand to the com­pan­ion, us­ing ei­ther i3-msg -t send_​tick or bind­ing a key to a nop com­mand. The lat­ter is used to avoid spawn­ing a shell and a i3-msg process just to send a mes­sage. The com­pan­ion lis­tens to bind­ing events and checks if this is a nop command.

bindsym $mod+Tab nop "previous-workspace"

There are other dec­o­ra­tors to avoid code du­pli­ca­tion: @debounce() to co­a­lesce mul­ti­ple con­sec­u­tive calls, @static() to de­fine a static vari­able, and @retry() to retry a func­tion on fail­ure. The whole script is a bit more than 1000 lines. I think this is worth a read as I am quite happy with the result. 🦚

Up­date (2022-07)

Daniel Pereira wrote wm­com­pan­ion, a desk­top event lis­tener for min­i­mal win­dow man­ager users, in­spired by i3-com­pan­ion. It is a generic en­gine to build your own com­pan­ion. It does not have built-in sup­port for i3 events, but its mod­u­lar de­sign al­lows a mo­ti­vated user to add it with a few lines of code.

dunst: the notification daemon#

Un­like the awe­some win­dow man­ager, i3 does not come with a built-in no­ti­fi­ca­tion sys­tem. Dunst is a light­weight no­ti­fi­ca­tion dae­mon. I am run­ning a mod­i­fied ver­sion with HiDPI sup­port for X11 and re­cur­sive icon lookup. The i3 com­pan­ion has a helper func­tion, notify(), to send no­ti­fi­ca­tions us­ing DBus. container_​info() and workspace_​info() use it to dis­play in­for­ma­tion about the con­tainer or the tree for a workspace.

Notification showing i3 tree for a
workspace
Notification showing i3’s tree for a workspace

polybar: the status bar#

i3 bun­dles i3bar, a ver­sa­tile sta­tus bar, but I have opted for Poly­bar. A wrap­per script runs one in­stance for each monitor.

The first mod­ule is the built-in sup­port for i3 workspaces. To not have to re­mem­ber which ap­pli­ca­tion is run­ning in a workspace, the i3 com­pan­ion re­names workspaces to in­clude an icon for each ap­pli­ca­tion. This is done in the workspace_​rename() func­tion. The icons are from the Font Awe­some project. I main­tain a map­ping be­tween ap­pli­ca­tions and icons. This is a bit cum­ber­some but it looks great.

i3 workspaces in Polybar
i3 workspaces in Polybar

For CPU, mem­ory, bright­ness, bat­tery, disk, and au­dio vol­ume, I am re­ly­ing on the built-in mod­ules. Poly­bar’s wrap­per script gen­er­ates the list of filesys­tems to mon­i­tor and they get only dis­played when avail­able space is low. The bat­tery wid­get turns red and blinks slowly when run­ning out of power. Check my Poly­bar con­fig­u­ra­tion for more details.

Various modules for Polybar
Polybar displaying various information: CPU usage, memory usage, screen brightness, battery status, Bluetooth status (with a connected headset), network status (connected to a wireless network and to a VPN), notification status, and speaker volume.

For Blue­tooth, net­work, and no­ti­fi­ca­tion sta­tuses, I am us­ing Poly­bar’s ipc mod­ule: the next ver­sion of Poly­bar can re­ceive an ar­bi­trary text on an IPC socket. The mod­ule is de­fined with a sin­gle hook to be ex­e­cuted at the start to re­store the lat­est status.

[module/network]
type = custom/ipc
hook-0 = cat $XDG_RUNTIME_DIR/i3/network.txt 2> /dev/null
initial = 1

It can be up­dated with polybar-msg action "#network.​send.​XXXX". In the i3 com­pan­ion, the @polybar() dec­o­ra­tor takes the string re­turned by a func­tion and pushes the up­date through the IPC socket.

The i3 com­pan­ion re­acts to DBus sig­nals to up­date the Blue­tooth and net­work icons. The @on() dec­o­ra­tor ac­cepts a DBusSignal() object:

@on(
    StartEvent,
    DBusSignal(
        path="/org/bluez",
        interface="org.freedesktop.DBus.Properties",
        member="PropertiesChanged",
        signature="sa{sv}as",
        onlyif=lambda args: (
            args[0] == "org.bluez.Device1"
            and "Connected" in args[1]
            or args[0] == "org.bluez.Adapter1"
            and "Powered" in args[1]
        ),
    ),
)
@retry(2)
@debounce(0.2)
@polybar("bluetooth")
async def bluetooth_status(i3, event, *args):
    """Update bluetooth status for Polybar."""

The mid­dle of the bar is oc­cu­pied by the date and a weather fore­cast. The lat­ter also uses the IPC mech­a­nism, but the source is a Python script trig­gered by a timer.

Date and weather in Polybar
Current date and weather forecast for the day in Polybar. The data is retrieved with the OpenWeather API.

I do not use the sys­tem tray in­te­grated with Poly­bar. The em­bed­ded icons usu­ally look hor­ri­ble and they all be­have dif­fer­ently. A few years back, GNOME has re­moved the sys­tem tray. Most of the prob­lems are fixed by the DBus-based Sta­tus No­ti­fier Item pro­to­col—also known as Ap­pli­ca­tion In­di­ca­tors or Ay­atana In­di­ca­tors for GNOME. How­ever, Poly­bar does not sup­port this pro­to­col. In the i3 com­pan­ion, the im­ple­men­ta­tion of Blue­tooth and net­work icons, in­clud­ing the no­ti­fi­ca­tions on change, takes about 200 lines. I got to learn a bit about how DBus works and I get ex­actly the info I want.

picom: the compositor#

I like hav­ing slightly trans­par­ent back­grounds for ter­mi­nals and to re­duce the opac­ity of un­fo­cused win­dows. This re­quires a com­pos­i­tor.1 pi­com is a light­weight com­pos­i­tor. It works well for me, but it may need some tweak­ing de­pend­ing on your graphic card.2 Un­like the awe­some win­dow man­ager, i3 does not han­dle trans­parency, so the com­pos­i­tor needs to de­cide by it­self the opac­ity of each win­dow. Check my con­fig­u­ra­tion for details.

systemd: the service manager#

I use sys­temd to start i3 and the var­i­ous ser­vices around it. My xses­sion script only sets some en­vi­ron­ment vari­ables and lets sys­temd han­dle ev­ery­thing else. Have a look at this ar­ti­cle from Michał Góral for the ra­tio­nale. No­tably, each com­po­nent can be eas­ily restarted and their logs are not man­gled in­side the ~/.​xsession-errors file.3

I am us­ing a two-stage setup: i3.​service de­pends on xsession.​target to start ser­vices before i3:

[Unit]
Description=X session
BindsTo=graphical-session.target
Wants=autorandr.service
Wants=dunst.socket
Wants=inputplug.service
Wants=picom.service
Wants=pulseaudio.socket
Wants=policykit-agent.service
Wants=redshift.service
Wants=spotify-clean.timer
Wants=ssh-agent.service
Wants=weather.service
Wants=weather.timer
Wants=xiccd.service
Wants=xsettingsd.service
Wants=xss-lock.service

Then, i3 ex­e­cutes the sec­ond stage by in­vok­ing the i3-session.target:

[Unit]
Description=i3 session
BindsTo=graphical-session.target
Wants=wallpaper.service
Wants=wallpaper.timer
Wants=polybar.service
Wants=i3-companion.service
Wants=misc-x.service

Have a look at my con­fig­u­ra­tion files for more details.

rofi: the application launcher#

Rofi is an ap­pli­ca­tion launcher. Its ap­pear­ance can be cus­tom­ized through a CSS-like lan­guage and it comes with sev­eral themes. Have a look at my con­fig­u­ra­tion for mine.

Rofi as an application launcher
Rofi as an application launcher

It can also act as a generic menu ap­pli­ca­tion. I have a script to con­trol a me­dia player and an­other one to se­lect the wifi net­work. It is quite a flex­i­ble ap­plication.

Rofi as a wifi network selector
Rofi to select a wireless network

xss-lock and i3lock: the screen locker#

i3lock is a sim­ple screen locker. xss-lock in­vokes it re­li­ably on in­ac­tiv­ity or be­fore a sys­tem sus­pend. For in­ac­tiv­ity, it uses the XScreenSaver events. The de­lay is con­fig­ured us­ing the xset s com­mand. The locker can be in­voked im­me­di­ately with xset s activate. X11 ap­pli­ca­tions know how to pre­vent the screen saver from run­ning. I have also de­vel­oped a small dim­mer ap­pli­ca­tion that is ex­e­cuted 20 sec­onds be­fore the locker to give me a chance to move the mouse if I am not away.4 Have a look at my con­fig­u­ra­tion script.

Demonstration of xss-lock, xss-dimmer and i3lock with a 4× speedup.

Up­date (2021-12)

I am now us­ing XSecureLock with a Python script to cus­tom­ize the screen saver. See “Cus­tom screen saver with XSecureLock” for more details.

The remaining components#

  • au­torandr is a tool to de­tect the con­nected dis­play, match them against a set of pro­files, and con­fig­ure them with xrandr.

  • in­put­plug ex­e­cutes a script for each new mouse and key­board plugged. This is quite use­ful to load the ap­pro­pri­ate key­board map. See my con­fig­uration.

  • xset­tingsd pro­vides set­tings to X11 ap­pli­ca­tions, not un­like xrdb but it no­ti­fies ap­pli­ca­tions for changes. The main use is to con­fig­ure the Gtk and DPI set­tings. See my ar­ti­cle on HiDPI sup­port on Linux with X11.

  • Red­shift ad­justs the color tem­per­a­ture of the screen ac­cord­ing to the time of day.

  • maim is a util­ity to take screen­shots. I use Prt Scn to trig­ger a screen­shot of a win­dow or a spe­cific area and Mod+Prt Scn to cap­ture the whole desk­top to a file. Check the helper script for details.

  • I have a col­lec­tion of wall­pa­pers I ro­tate ev­ery hour. A script se­lects them us­ing ad­vanced ma­chine learn­ing al­go­rithms and stitches them to­gether on multi-screen set­ups. The se­lected wall­pa­per is reused by i3lock.

Up­date (2026-10)

This post was heav­ily dis­cussed on Hacker News and Lobsters.