Mon 06 Jul 2020 08:44:24 AM UTC

Using Xnest for Xorg Window Manager Testing

When playing around with a window manager, sometimes it’s nice to be able to test your changes without having to log out. Especially when you screwed something up and need to open up a TTY and kill x to fix it.

Luckily there’s a nice tool that allows us to start up a new X Display and nest it in a window in our current X instance: Xnest(1)

To use it for testing a WM, dwm for instance, we first need to start up Xnest and give it an unused display to use:

Xnest :1

Since the defualt is usually :0, using :1 should work in most cases.

Then, in a second terminal, you can start any X application on that display by either using the -display option if the program recognizes it, or by setting the $DISPLAY environment variable:

DISPLAY=:1 dwm

Of course opening up two window managers and a third window for the Xnest instance can be a bit much, so in order to start Xnest and run your WM you can rap it up in a script:

#!/bin/sh
Xnest :1 &
sleep 1
DISPLAY=:1 dwm

Or just a one-liner:

Xnest :1 & sleep 1; DISPLAY=:1 dwm

Note that we added a sleep(1) in there. Since we’re backgrounding Xnest, the next commands will run immediately before it’s fully started up and the sleep gives it a second to start before we run dwm.

Of course, in some situations, the next available Display may not be :1. We can check for the used Display ID’s by looking in the /tmp/.X11-unix directory:

$> ls /tmp/.X11-unix
X0
X1

That’s my out put with my default display running along with Xnest. We could easily create a helper script or shell function to read the directory and find the next available Display ID, but it seems overkill to me.