15

Is there a way to open the next unread message in mutt with a single key? I can move to the next unread with next-new-then-unread, which is bound to Tab by default. However, if there are no unread messages in the current mailbox, then I'd have to use next-unread-mailbox instead (unbound by default). This is sub-optimal anyway, because if I have a new message, quit mutt, then open mutt again, this won't move me to the mailbox containing the "new" messages. (Presumably the mailbox is not unread any more.)

In addition, both of these move to the next message in index view, and I have to manually open the message in pager view (with Enter). Is there a way to open the next unread message, no matter which mailbox it's in?

I'm using neomutt, so one partial workaround I've found is to use the sidebar commands.

macro index,pager , '<sidebar-next-new><sidebar-open><enter>'

The problem is that this automatically opens the next unread mailbox (from the sidebar). Hence if there are unread messages in the current mailbox and another one, this command will open the message in the other mailbox instead of the current.

2
+50

For starters, you can use a macro like this to automatically jump to the new message:

macro index     .n      "<next-unread-mailbox><enter><next-new-then-unread><enter>" "Go to new mail"

Note however that if there are no new messages only Enter key will be pressed and the current message will be opened.

As an alternative if Maildir is used we could use a ~/bin/mutt-new.sh script that would check if there's new mail:

#!/usr/bin/env sh

if [ "$(find "$HOME"/.muttmail/box1/new -type f -printf '\n' | wc -l)" -eq 0 ]
then
    printf "I think there's no new mail\n" >&2
    printf "Press [ENTER] to continue\n" >&2
    read -r _
    exit 1
fi

echo 'push <next-unread-mailbox><enter><next-new-then-unread><enter>'

Add this to ~/.muttrc:

macro index     .n        "!~/bin/mutt-new.sh" "Go to new"

EDIT:

How about this: the following script will first check if there is new mail in the current mailbox:

#!/usr/bin/env sh

cur_mbox=${1/=/}

echo "$1" >> /tmp/PAR
echo "$cur_mbox" >> /tmp/PAR

if [ ! "$(find "$HOME"/.muttmail/"$cur_mbox"/new -type f -printf '\n' | wc -l)" -eq 0 ]
then
    printf "There is new mail in this mailbox\n" >&2
    printf "Press [ENTER] to continue\n" >&2
    read -r _
    echo 'push <next-new-then-unread><enter>'
elif [ ! "$(find "$HOME"/.muttmail/ -type d -name new -exec ls {} \; | wc -l)" -eq 0 ]
then
    printf "There is new mail in other mailboxes\n" >&2
    printf "Press [ENTER] to continue\n" >&2
    read -r _
    echo 'push <next-unread-mailbox><enter><next-new-then-unread><enter>'
else
    printf "I think there's no new mail\n" >&2
    printf "Press [ENTER] to continue\n" >&2
    read -r _
    exit 1
fi

Add this to ~/.muttrc:

folder-hook . 'set my_oldrecord=$record; set record=^; set my_folder=$record; set record=$my_oldrecord'
folder-hook . 'macro index .n "<enter-command>source \"~/bin/mutt-new.sh $my_folder |\"<return>" "Go to new"'

EDIT:

You said:

This is sub-optimal anyway, because if I have a new message, quit mutt, then open mutt again, this won't move me to the mailbox containing the "new" messages. (Presumably the mailbox is not unread any more.)

This can be fixed by:

set mark_old=no
  • Thanks for the answer. A major problem with this approach is that it runs <next-unread-mailbox> regardless of whether there are unread messages in the current mailbox. In this case, it would switch to the other mailbox, and not open the next unread message in the current mailbox. Another problem (as per my question) is that <next-unread-mailbox> doesn't look for unread/new messages but unread mailboxes. – Sparhawk Dec 19 '17 at 3:04
  • @Sparhawk: see edit. That's an interesting question. Unluckily mutt is not fully scriptable and it's a shame. – Arkadiusz Drabczyk Dec 19 '17 at 14:57
  • @Sparhawk: see yet another edit. The macro I suggested automatically opens a new e-mail in the current mailbox if there is any before moving on to other maliboxes. I also suggested how to disable marking messages as O after leaving mutt. – Arkadiusz Drabczyk Dec 21 '17 at 14:49
  • Thanks. I'm on vacation at the moment, but I'll test it when I get back. It looks promising. – Sparhawk Dec 22 '17 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.