Dmenu – What It Is and How I Use It

Dmenu enables us to create something like right-click menu in Windows OS. You can use it in your shell scripts. In these scripts, you just need to supply your list of menus to dmenu, and then you can choose any one of them. Now, based on the chosen menu, you can take any action.

Right-Click Menu in Win
Figure: Right-click menu in Windows. Source: docs.microsoft.com
dmenu as right click menu used with sxiv
Figure: Dmenu like right-click menu used with sxiv

And it also comes with additional benefits:

  1. It is based on keyboard so no hassel of going back and forth between mouse and keyboards.
  2. You can use arrow keys or emacs key bindings – one of the widely used key bindings. Therefore, you don’t have to memorize any new key bindings.
  3. For minimal dekstop environments such as those using tiling window managers, it is a gold mine. You will need it to create required features like power-menu, and application launcher such as i3-dmenu-desktop.
  4. Despite Being a suckless utility in nature, it works out of the box. Just supply your menu items using echo or printf and you are done.
  5. It is a modular tool so it can be used with many other modular tools like sxiv.

Modularity brings many advantages. You can combine it with many modular packages like sxiv (an image viewer), systemctl suspend/poweroff/reboot, etc., and create your own package! This is one of the features I like about Linux. You don’t have to stay dependent on big corporates. They first create an application/software which we want. At the start, they sell it for free or at a low cost to lure us in. And when we get dependent on these applications, they keep increasing the price. On top of that, you will have to wait for these big corporates to bring additional features you like. For example, you need to buy a whole new copy of Windows 11 for a feature called Tiling Windows available in Linux for decades.

In this article, you will learn about installation, available features, patches, modern-day useful examples, and how dmenu can replace many packages.

Table of Contents

1. Installation

Debian/Ubuntu/Raspbian/Kali Linux

~$ sudo apt-get install suckless-tools

Archlinux:

~$ sudo pacman -S dmenu

Alpine

~$ sudo apk add dmenu

Fedora

~$ sudo dnf install dmenu

Now that we have installed the dmenu on our system, we will familiarize ourselves with some terms in the dmenu.

2. Getting Familar with Dmenu

Dmenu reads a list of newline-separated items from standard input. For the stdin, use either pipe (|) or <.

Example Syntax:

~$ echo -e "poweroff\nreboot\nsuspend" | dmenu 

Output:

Search Bar and Menus in Dmenu
Figure: Search Bar and Menus in dmenu

At the top left corner, you will get a search bar. Right next to it, you will get all of your menu items. In the above example, I have three menu items – poweroff, reboot, and suspend. You can choose one of the items using either arrow keys or by typing in the Search Bar. Then hit Enter key. The chosen item will be printed out on the terminal. You can take further action on this choice using another pipe. To continue with the above example, if I add xargs systemctl:

~$ echo -e "poweroff\nreboot\nsuspend" | dmenu | xargs systemctl

Here, the xargs just takes your choice and adds it next to the systemctl, and then executes it. So, if you choose the ‘suspend’ menu, xargs will execute a command ‘systemctl suspend’. And then your system will be suspended.

So in just one line, we created a power-menu which, like any desktop OS, can power off, reboot and suspend your system.

3. Options Available

Using options, you can set dmenu’s fonts, colors, vertical/horizontal placement, and top/bottom placement, etc.

As I told you earlier, in the dmenu you can type out in the search bar to select your menu. By default, it is case-sensitive. So, for example, if you type ‘test’ in the search bar, it will match only those menus which have ‘test’ in them. If you want to include ‘Test’ as well, use case-insensitive matching. For this, you need to use the -i flag.

Example:

~$ echo -e "poweroff\nreboot\nsuspend" | dmenu -i

3.2. Bottom Placement of Dmenu

By default, the dmenu places itself on top of the screen. You can use the -b flag to put it at the bottom.

Dmenu at bottom of the screen
Figure: Dmenu at bottom of the screen

3.3. Vertical Placement of Menus

By default, dmenu places menus in a horizontal bar. To place them vertically, use -l flag:

~$ ps -e | dmenu -l 10

Here, in the above command, ps -e lists the processes running on your computer. And the -l 10 flag places the dmenu vertically with a maximum of 10 items in it. This way you will not lose sight of your existing window.

Vertical Dmenu
Figure: Vertical dmenu

3.4. Font Configuration in Dmenu

As you have been seeing, the default size of the font is very small. You need to increase the font size. At the same time, you can also change the font. For this purpose we use the -fn flag:

~$ echo -e "poweroff\nreboot\nsuspend" | dmenu -fn "Ubuntu-18"

As you might have guessed, 18 is font size and Ubuntu is the name of the font.

Changing font size and fonts in dmenu
Figure: Changing font size and fonts in dmenu

Note: To find font names, you can use fc-list command.

3.5. Dmenu No More “Black and Blue”

The default color is black and blue which is a little off-putting. You can change it using color flags -nb, -nf, -sb, and -sf.

FlagMeaning
-nbnormal background-color
-nfnormal foreground-color
-sbselected background-color
-sfselected foreground-color

These flags accept HTML color codes as is shown in the example given below:

~$ echo -e "poweroff\nreboot\nsuspend" | dmenu -nb "#EBCB8B" -nf "#2E3440" -sb "#2E3440" -sf "#EBCB8B"

Output:

Color Configuration in Dmenu
Figure: Color configuration in dmenu

3.6 Dmenu in Multi-Monitor Setup

To display it over a given monitor use -m flag. Monitor numbers start from 0.

~$ echo -e "poweroff\nreboot\nsuspend" | dmenu -m 0

3.7. Prompt in Dmenu

To set up a prompt, we use -p "Your_PROMPT". Example:

~$ echo -e "poweroff\nreboot\nsuspend" | dmenu -p "Power Options"
Prompt in dmenu
Figure: Prompt in dmenu

4. Setting Default Colors, Fonts, and Other Options

Using -nb, -nf, -sb, -sf, and -fn flags each time we want to use the dmenu is kind of repetitive and hence boring. What we need to do is a wrapper script called mydmenu with the following contents in it:

#!/bin/sh

dmenu -nb "#EBCB8B" -nf "#2E3440" -sb "#2E3440" -sf "#EBCB8B" -fn "Ubuntu-18" "$@"

Now, put it in your script directory and set appropriate execution bits using chmod.

Now, you don’t need to apply these flags anymore. Just set the colors according to your computer’s themes and you are done. Use the mydmenu instead like the following command:

~$ echo -e "poweroff\nreboot\nsuspend" | mydmenu -l 30

Similarly, you can also add your own flags like -i, and -m 0 in your wrapper script.

5. Shortcut Keys Available in Dmenu

You can use Enter button to select one of the menus and get out of the dmenu. Arrow keys, Ctrl-Left Arrow, Ctrl-Right Arrow, Home, End, Backspace, and Delete keys work as expected.

Moreover, many of the emacs key bindings are also available here. You might already be using these key bindings in your bash, zsh, ranger command line, fzf, and other packages. Personally, I prefer the emacs key bindings over the traditional key bindings (arrow keys mentioned above) because they are easy on my fingers. Some of the key bindings I use very often are:

Shortcut Action
Ctrl-nGo to the next menu
Ctrl-pGo to the previous menu
Ctrl-mEquivalent to Enter button
Ctrl-aEquivalent to Home button
Ctrl-eEquivalent to End button
Ctrl-bEquivalent to Left Arrow
Ctrl-fEquivalent to Right Arrow
Ctrl-dEquivalent to Delete button
Ctrl-hEquivalent to Backspace button
source: man dmenu; for more, see the man page.

That is another feature I love about Linux. In Windows and other Operating Systems, developers find it hard to include other packages’ key bindings given license issues but the Linux community is an open community. There are no such restrictions.

All the features and options mentioned above are sufficient for me. They are available in the default installation of dmenu in most of the distributions. But just for the sake of completion, I will also talk about how we can have more features and options using patches.

6. Patches in Dmenu

As dmenu is a suckless tool, you can bring additional features and options to it. You can use patches to bring mouse support, transparency, fuzzy match, multiselection, Xresources based configurations, etc. Just head over to this suckless.org link and download the required patches.

Now that I have covered all the basics we need, we will learn more about it using my own commands/scripts in the next heading.

7. How I Find Dmenu Useful

I find it highly useful to list the available man pages and open one of them using my favorite pdf viewer. [Courtesy: Luke Smith’s blog]

~$ man --apropos . | dmenu -l 30 | awk '{print $1}' | xargs man --troff-device=pdf | zathura -

Explanation of the above command:

  1. man --apropos . lists out all the available man pages and their short descriptions into dmenu.
  2. awk '{print $1} cuts out only the first word from the chosen option. So, for example, if I choose the first item shown in the below figure, the awk command will cut out only the anki part out of it.
  3. xargs man --troff-device=pdf: xargs, as mentioned above, is used to build and execute the command from the standard input. So, for the anki example, xargs executes the command man --troff-device=pdf anki.
  4. The output of the man --troff-device=pdf anki is in pdf format which can be read only by a pdf reader like zathura.
Dmenu listing man pages available in your system
Figure: Dmenu listing man pages available in your system

Further, I use it to kill unresponsive programs – a handy replacement command to top and htop.

~$ ps -e -o comm | dmenu -l 30 | xargs kill -9

Explanation of the above command:

  1. ps -e -o comm prints out all the running program.
  2. kill -9 <programme> is used to kill the program
Dmenu used as htop replacement
Figure: Dmenu used as htop replacement

Moreover, My favorite clipboard manager clipmenu (cdown/clipmenu on github) uses dmenu. This clipboard manager is just a shell script and therefore you can modify the script to fulfill your own needs.

Furthermore, One user created a whole file manager called dmenufm using dmenu (huijunchen9260/dmenufm on github).

Figure: Dmenufm using dmenu as file manager

Similarly, dmenu can be used as a launcher. For this purpose, the dmenu available in many distributions already includes a script called dmenu_run. Just execute it from your terminal and you can launch any program.

~$ dmenu_run

At the same time, dmenu can be used as a task manager. For this, I use todo. Just select a task to remove it after its completion, or type a new one to add that.

Figure: dmenu as task manager
Figure: dmenu as the task manager

Besides, many such scripts (password manager, clipboard manager, file manager, bookmark manager, music player, and more) are also hosted on suckless.org.

8. Way Ahead

Dmenu is a very small package with tremendous potential. Through this article, I tried to give you a small glimpse of these potential applications. To explore more, go through the man pages, patches, and scripts hosted on suckless.org.

And also, I tried my best to be as clear as possible without committing any mistakes. But I am also a human. And because of that, some mistakes are bound to occur. Therefore, I humbly request you to notify me about these mistakes in the comment section below. If you have any questions, feel free to ask in the comment section. I will be pleased to answer such questions.

2 thoughts on “Dmenu – What It Is and How I Use It”

  1. Pingback: How to send notifications in linux using dunstify/notify-send | SmartTech101

Leave a Comment

Your email address will not be published. Required fields are marked *