Use alternative file selector in mutt

mutt

In mutt, is it possible to use a file browser such as vifm or ranger (possibly in a tmux split) instead of the default file browser to select a file to attach to an email?

Best Answer

You can open ranger in a tmux split (a possibility you suggested), select the file to be attached, copy the file name with its path using yp and then return to the mutt attach window, press a and paste the attachment path and file name in the line that opens at the bottom of the attach window and press Enter.

Regarding the issue of spaces in the file name (in response to the comment of @Rastapopolous), I realized I have strong habit of not using them, but re-testing the above method of pasting the path from ranger, I find that when the file name has spaces, for some reason, the attachment name pasted in mutt has a repetition of part of the name appended! One can backspace to remove the "appendage" and the attachment is readily sent even with the spaces not escaped. I tested the path copy from ranger in vim and it pastes correctly, but not in mutt.

Since I habitually use tmux, I find this a relatively simple method.

Alternatively and independently of tmux, I also have the following script in my .bashrc:

# email an attachment
function mail_attach ()
{
    mutt -F ~/.muttrc -a "$@" -- myemail@gmail.com;
}

export -f mail_attach

This is useful when you know you will attach before composing an email. So, from within ranger, one simply selects a file, types s to open a shell command line and indicates the selected file with %f, that is:

:shell mail_attach %f

Pressing Enter will open the mutt editor for composing an email which already has the selected file attached attached. However, one must then change the recipient email address to the desired one. Perhaps there is a way to change the function to open the email editor with a blank "To" field, but I don't know enough. However, I don't find this a big deal.

For multiple files, if they are in the same folder, I select them all and then press @ which is a ranger shortcut to open the shell command line like this where %s is a reference to the selected files:

:shell %s

Then one simply inserts mail_attach before %s and pressing enter will open mutt for composing the email which already has all the selected files attached. If you want to attach files from different folders, I don't know how to do that apart from gathering them into the same folder.

I would like to give credit to whoever created the function, but I no longer know where it came from. I know it wasn't me.

Related Question