$ /buzzqrd/blog/


Default Options Using dmenu

Aug. 11, 2023


I recently was trying to find a way to use Chromium because I hate the way that Firefox scolls in increments. When configuring my laptop, I wanted my trackpad to have smooth scrolling in my web browser, and Chromium was the simplest options I could find (surf can't do yt right, so that's out). The biggest issue that I had with Chromium at the time was that, when searching for it in dmenu, it would not come up very quickly in the search results. The first result would always be "chromedriver", which was very irritating when I just wanted to quickly open up and instance of my browser.

This was a big issue, considering the program I open most often besides a terminal is my web browser. I looked into ways of making "chromium" have a higher priority than "chromedriver". I started by looking at the dmenu patch highpriority. I was having a bit of trouble patching it in for some reason. (I'll probably try again another time because I don't want to loose to something as simple as a patch.)

In the process, I spent quite a long time trying to learn how dmenu works. While looking at the code I thought that it would be more useful to have my browser pinned as the first item in dmenu. This would mean that when I open dmenu, I can just hit enter to open up my browser. It would also be useful to add multiple default programs so I can just arrow over to libreoffice or some other common program.

After looking through a bunch of C code, I finally figured out how dmenu works well enough to find that dmenu is actually called through a script named dmenu_run. By modifying this script, we can put some program names at the top of the listing, making them default entries.

When getting dmenu from the suckless repo, the dmenu_run script looks something like this:

#!/bin/sh
dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} &


Let's quickly break down what this does. First, we see that dmenu_path is run. If we open a new terminal and run this, we can see that it will just output all of the names of the programs that we can run, in alpabetical order, just as we see them listed when there is no text in the search box. This gets piped into dmenu, which will allow you to select an option. The selected option now gets run in a background shell, completing everything that dmenu has to do.

Now, adding default programs at the top of this list of programs initially given to dmenu is quite simple. We just need to inject the names of our default programs at the top of the program list fed to dmenu.

We can inject those lines for the default programs by replacing the second line of the script with this code:

{ echo -e "firefox\nlibreoffice" ; dmenu_path ; } | dmenu "$@" | ${SHELL:-"/bin/sh"} &


Just by changing the first part of the script and modifying what we pipe into dmenu, we are able to set some default programs. Here, we have firefox as the first default program, and libreoffice next to it for easy access.

If you just run the first part:

{ echo -e "firefox\nlibreoffice" ; dmenu_path ; }
You can see that it gives an output with all of the programs, showing your defaults at the top. If you don't want to scroll all the way to the top, you can just read the top with head.

{ echo -e "firefox\nlibreoffice" ; dmenu_path ; } | head

Be careful, bash scripts can often be sensitive to whitespace and changing the spacing might break the program.

You can change around this script by modifying the program names to use the programs that you run most often. You can add more programs to the defaults list, just make sure that there are no extra spaces in the string and that each program name is seperated by a newline \n character.

Hopefully you get some use out of this article. Thanks for reading!

buzzqrd