dmenu for snippet insertion

2026-01-20

My favorite use cases for dmenu are:

  1. system command execution (dmenu_run)
  2. clipboard manager (greenclip + dmenu)
  3. custom command menus
  4. snippet insertion

1 is shipped with dmenu and for 2 just follow the greenclip link above. For 3 see dmenu-custom-command-menus.

This article covers 4.

Snippet insertion

screenshot of custom command menu

To me it's important that a snippet can be inserted with a single key stroke. That's why I have added some patches to dmenu.

Prerequisites

All you need is xclip, xdotool and dmenu with these patches applied:

Or simply use the custom branch of my fork: ~maxgyver83/dmenu - sourcehut git

These patches allow a quick selection of a snippet. Usually, you use one key combo to show the menu and then a single key stroke to select an entry.

Shell scripts

First, add this little helper script to your $PATH:

snippets-dmenu:

#!/bin/sh
snippets=${SNIPPETS:-~/.snippets}
linecount=$(cat "$snippets" | wc -l)
# dmenu with center patch and instant_first_char patch
cmd="dmenu -1 -c -l $linecount -fn monospace:size=14"

# select line from ~/.snippets with dmenu, replace '\n's by newlines,
# trim the final newline, and write to both primary selection and clipboard
# (because Firefox pastes the clipboard on Shift-Insert)
$cmd $@ < "$snippets" \
    | sed 's/^. //' \
    | sed 's/\\n/\n/g' \
    | head -c -1 \
    | xclip -i -sel p -f \
    | xclip -i -sel c
sleep 0.1
xdotool key shift+Insert

Then, create your snippet file like this example:

main-menu:

e me@example.com
m Max
s Schillinger
n Max Schillinger
w Willy-Brandt-Straße 1
p 10557
b Berlin
a Willy-Brandt-Straße 1\n10557 Berlin
h Hi all,\n\n
t To whom it may concern,\n\n
k Kind regards,\nMax

Important:

As a final step, you could bind snippets-dmenu to a key combo in your ~/.config/sxhkd/sxhkdrc or whatever you use for global key bindings:

super + alt + i
    ~/bin/snippets-dmenu