Skip to content

Instantly share code, notes, and snippets.

@nattomi
Last active July 3, 2021 11:11
Show Gist options
  • Save nattomi/bfd8abfc746f35c61b3c2cdb4c0aae4b to your computer and use it in GitHub Desktop.
Save nattomi/bfd8abfc746f35c61b3c2cdb4c0aae4b to your computer and use it in GitHub Desktop.
Extending the dwm workspace switcher

Extending the dwm workspace switcher

Windows managers for Linux usually provide a mean of switching between multiple workspaces. Dwm, a.k.a Dynamic Window Manager is not an exception either - the default configuration offers 9 workspaces to choose from.

default dwm workspace switcher

This may seem a lot, but in practice it's easy to exhaust. Configuring dwm is achieved by editing the configuration file config.h and recompiling the application. By default, the following tags are defined for the available workspaces:

/* tagging */
static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };

Since I happen to use a Hungarian qwertz keyboard, the row with the numberic keys right below the function keys contains there additional keys for accented characters ö, ü, ó. Since on this keybaord type 0 is located on the left of 1, the following configuration provides a natural extension of the default tagging above:

/* tagging */
static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "ö", "ü", "ó" };

This way I can get 3 extra workspaces while making sure that all keys used for switching the workspaces are located next to each other in the same row. However, we must also configure the desired shortkey combinations for the new workspace tags. It can be done by extending the keys array in the configuration file:

static Key keys[] = {
	/* modifier                     key        function        argument */
	{ MODKEY,                       XK_p,      spawn,          {.v = dmenucmd } },
	/* array truncated for compactness */
	{ MODKEY|ShiftMask,             XK_period, tagmon,         {.i = +1 } },
	TAGKEYS(                        XK_1,                      0)
	TAGKEYS(                        XK_2,                      1)
	TAGKEYS(                        XK_3,                      2)
	TAGKEYS(                        XK_4,                      3)
	TAGKEYS(                        XK_5,                      4)
	TAGKEYS(                        XK_6,                      5)
	TAGKEYS(                        XK_7,                      6)
	TAGKEYS(                        XK_8,                      7)
	TAGKEYS(                        XK_9,                      8)
	{ MODKEY|ShiftMask,             XK_q,      quit,           {0} },
};

As it can be seen from the default configuration above, the default workspaces can be visited by MODKEY+1, MODKEY+2, ..., MODKEY+9, respectively. For obvious reasons, we want to assign the combinations MODKEY+ö, MODKEY+ü and MODKEY+ó to the newly defined workspaces. This is achieved by the following extension of the keys array:

static Key keys[] = {
	/* modifier                     key        function        argument */
	{ MODKEY,                       XK_p,      spawn,          {.v = dmenucmd } },
	/* array truncated for compactness */
	{ MODKEY|ShiftMask,             XK_period, tagmon,         {.i = +1 } },
	TAGKEYS(                        XK_1,                      0)
	TAGKEYS(                        XK_2,                      1)
	TAGKEYS(                        XK_3,                      2)
	TAGKEYS(                        XK_4,                      3)
	TAGKEYS(                        XK_5,                      4)
	TAGKEYS(                        XK_6,                      5)
	TAGKEYS(                        XK_7,                      6)
	TAGKEYS(                        XK_8,                      7)
	TAGKEYS(                        XK_9,                      8)
	TAGKEYS(                        0xf6,                      9)
	TAGKEYS(                        0xfc,                      10)
	TAGKEYS(                        0xf3,                      11)
	{ MODKEY|ShiftMask,             XK_q,      quit,           {0} },
};

The keycodes 0xf6, 0xfc and 0xf3 were obtained with the help of the utility xev. For example, for the key ö it gave the following output (look for the keysym keyword in line 3).

KeyPress event, serial 32, synthetic NO, window 0x1a00001,
    root 0x78b, subw 0x0, time 6904838, (664,532), root:(665,552),
    state 0x0, keycode 19 (keysym 0xf6, odiaeresis), same_screen YES,
    XLookupString gives 2 bytes: (c3 b6) "ö"
    XmbLookupString gives 2 bytes: (c3 b6) "ö"
    XFilterEvent returns: False

Now we can enjoy our extended workspace switcher.

default dwm workspace switcher

This gist also contains a patch that you can apply to the default configuration file of dwm-6.2:

patch config.h < dwm_extended_workspaces.diff
--- config.def.h 2021-07-03 12:59:26.087518856 +0200
+++ config.h 2021-07-03 13:01:14.988817508 +0200
@@ -19,7 +19,7 @@
};
/* tagging */
-static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
+static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "ö", "ü", "ó" };
static const Rule rules[] = {
/* xprop(1):
@@ -93,6 +93,9 @@
TAGKEYS( XK_7, 6)
TAGKEYS( XK_8, 7)
TAGKEYS( XK_9, 8)
+ TAGKEYS( XK_0xf6, 9)
+ TAGKEYS( XK_0xfc, 10)
+ TAGKEYS( XK_0xf3, 11)
{ MODKEY|ShiftMask, XK_q, quit, {0} },
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment