Space Panda

Wag the Dog

The mutt has a ranger.

This article describes how to select attachments in mutt and neomutt through the command line file manager ranger.

Selecting files to attach to my emails in (neo)mutt is not quite as convenient as it could be. Especially since I'm quite pampered using ranger for my usual file management needs in the command line.

As it turns out, combining the two is much easier than anticipated.

First things first, ranger provides the command line option --choosefile=output. With it, ranger will start up and once you selected a file, it writes the filename into the file output.

To pull the content from that output file and use it as an attachment is something that doesn't work out of the box in mutt or neomutt. Especially since that output file might as well be empty if you changed your mind and just quit ranger without selecting a file.

However you can utilise source to generate a mutt configuration file from the output of a shell script, like this:

# (neo)mutt.rc

macro compose a "<enter-command>source ~/.config/neomutt/|<return>" "Attach a file"

Upon pressing a the script will be executed and its output will be interpreted as a configuration file.

And here's the downright boring script.


if [ -z "$(which ranger)" ]; then
    echo "push <attach-file>"
    tmpout=$(mktemp -p /run/user/`id -u`/)
    ranger --choosefile=$tmpout
    attachment=$(cat $tmpout)
    if [ -n "$attachment" ]; then
        echo "push <attach-file>\"$attachment\"<return>
        echo "cd ."
    rm -f $tmpout

The first branch of the if is just there in case you forgot that you don't actually have ranger installed. The fallback is to simply push the attach-file function into mutt and have the user enter the file location as usual.

The second branch is much more interesting, albeit simple and boring: create a temporary filename, have ranger use that to write the user selected file into, pull out the filename and finally push attach-file into mutt. This time though we add the filename into the buffer, too, and press return.

The obscure cd . in the second branch is there to provide mutt with something, otherwise it will complain that you made it read and "empty" configuration file.

Of course, sending attachments is a questionable activity and especially for bigger files it's just bad manners. However, thanks to ranger it can be much easier in mutt and neomutt to select the (small, I hope) file to attach.

Tagged as attachment, mutt, neomutt, ranger