index

Article posted on: 2021-01-16 22:26
Last edited on: 2021-01-17 12:24
Written by: Sylvain Gauthier

Minimal tips 1: using dmenu for literally (almost) everything

I have been using dwm as my only window manager for quite a while now, and it happens to ship with a very convenient utility called dmenu. The purpose of this utility might seem trivial and useless to people not familiar with the UNIX philosophy, but as with most UNIX things, it’s precisely its simplicity that makes its strength.

dwm takes a bunch of lines in stdin, display them separately on a menu bar (by default on top of your screen) and will filter them as the user type characters into the bar. Once the user press enter, the selected entry gets printed into stdout.

Nothing fancy, you might think, but here are a few of its applications that I either use (or even made) personally or are worth mentioning.

Application launcher

That’s the most obvious one and the default use of it on dwm. You simply print all possible commands into dmenu’s stdin and you execute the output. You’ve got yourself a task launcher that’s infinitely lighter and faster that all the nonsense usually found in graphical user interfaces, all the shiny search bars and start menus.

Bookmark manager

If, like me, you like using a simple browser without tab support and you just want to load one web page per instance (at least most of the time), you’ll love this one.

Just put all of your favorite URLs into a text file, one per line, optionally with a description or title separated by a space. You feed that file to dmenu, you cut -d' ' -f1 the output to keep only the URL and you open the browser on it.

Code snippet to do that assuming you are using surf, plus activate tor if the line in your bookmarks starts with T:

#!/bin/bash

SEL="$(dmenu < "$HOME/.bookmarks")"

OPT="$(printf "$SEL" | grep -o '^[A-Z] ' | cut -d ' ' -f1)"
URL="$(printf "$SEL" | grep -o '[^ ]*$')"

case "$OPT" in
    T)
        PROXY="socks://127.0.0.1:9050"
        ;;
    *)
        ;;
esac

test -z "$URL" && exit

http_proxy="$PROXY" surf "$URL"
shred ~/.surf/cookies.txt
rm ~/.surf/cookies.txt
rm -r ~/.surf/cache

Not only is this ridiculously light and simple but with that system, any script or program can easily add bookmarks to your file, by just appending the new one at the end. Endless possibilities.

Password manager

If you use pass to store securely your passwords, you may have heard of passmenu, which will just feed all passwords paths into dmenu, let you select the one you want, will decipher it and store it in the clipboard so that you can paste it wherever you want.

The great thing with this solution, compared to most integrated password managers in mainstream web browser, is that it’s completely universal. Whether you need a password in a terminal, in a clicky-click GUI, in a website or pipe to a raw device file (don’t do that), it will work.

“B-but the clipboard is not secure, what about malicious software that might snatch your password from it?” You might ask.

Well, if you think running malicious code on your CPU is an acceptable threat model that just need to be dealt with, congratulation, you can be a web browser developer. Feel free to impose your shitty standards and horrendous designs on everybody and hey, make sure that NO ONE can live a decent life without having to use a 10M+ SLOC bloated software whose only purpose is to download code from untrusted sources to run it on your CPU.

I digress, I’ll save my kind words on the modern web for another post.

Universal unicode input system

This one I kinda stole from a Luke Smith video, where he did roughly the same for emojis.

The way it works is ridiculously simple. It’s pretty much the same as the bookmark system described above, except instead of an URL you have an emoji, with a description. So you just filter emojis by typing the description and it will insert the selected emoji in the clipboard. You can then paste it anywhere you want. A simple trick that makes all emoji selection menu developers for all individual apps seethe with anger as they see their work deprecated by a 5 line script.

My modest improvement consists in using this for simplified Chinese input. Instead of an emoji file, I built a dictionary from wiktionary with thousands of Chinese characters with their pronunciation and translation. So the same technique as above lets you filter and select the character you want, not only by pronunciation but also by the English meaning. Makes learning mandarin even easier than it already is.

The only difference with the emoji system is that here, the script loops on the character selection and will append the next one to the previous one until the user presses Ctrl+C, to make it more efficient to write full words and small sentences.

Here is the code to do that:

#!/bin/sh

DICT="$HOME/.local/share/dict/mandarin"

char=""
sent=""
while true ; do
    char="$(dmenu -l 30 < "$DICT" | cut -d' ' -f1 | tr -d $'\n')"
    test -n "$char" || exit 0
    sent="$sent$char"
    echo "$sent" | xclip -r -selection c
done

And here is the Chinese dictionary to save you the trouble of formatting it.

Conclusion

Those are just a few, humble uses I found to dmenu but the possibilities are endless. I will edit this article with more uses as they arise.

Next post on minimal tips series will be our homemade choice CLI menu selector.