Set Mutt's default save folder depending on the current folder

I would like to configure Mutt so that the default save folder changes depending on the current folder:

  • When reading messages in the folder =account1/Unsorted or =account1/Important or any other sub-folder under =account1/, I’d like the default save folder (the one suggested when pressing s in the message index) to be =account1/INBOX.

  • Likewise, for sub-folders of =account2/, I’d want the default save folder to be =account2/INBOX, etc.

How may I configure Mutt to do this?

  • It appears save-hook would have been useful, if it had a way of matching against the folder name (I don’t think it has).
  • It appears folder-hook would have been useful, if it had a way of setting the save folder, but there is no save folder setting to set with the hook (there’s record, but that’s for outgoing messages).
Asked By: Kusalananda


Update: Reportedly, the following two lines were used

folder-hook +account1 unhook save-hook
folder-hook +account1 save-hook . +account1/INBOX

repeated for each account.

Mutt, as you know, is configured by .muttrc file.
Look at man muttrc, the patterns at all others is there. I also do not see a folder pattern there but searching for save I found elsewhere

 Note that these expandos are supported in "save-hook", "fcc-hook" and "fcc-save-hook", too.

and above that

              %b     filename of the original message folder (think mailbox)
              %B     the list to which the letter was sent, or else the folder name (%b).
              %O     original  save  folder where mutt would formerly have stashed the message: list name or
                     recipient name if not sent to a list

Some of them could potentially be used, but I do not see in patterns any way how to match against a fixed string and not against the message.

Use can make procmail (or whatever is messing with your incoming mail) to add a custom header line and use -h to test for it.

I think your solution will be using

     folder-hook [!]regexp command
              When  mutt enters a folder which matches regexp (or, when regexp is preceded by an exclamation
              mark, does not match regexp), the given command is executed.

              When several folder-hooks match a given mail folder, they are executed in the order  given  in
              the configuration file.

May be something like

folder-hook account1 unhook *
folder-hook account1 save-hook ~A   =account1/INBOX
folder-hook account1      save-hook  ".*"  =account1/INBOX
folder-hook ^=account1.*  save-hook  ........... ???

UPDATE: Reportedly, the following two lines were used as solution (and seem to work)
folder-hook +account1 unhook save-hook
folder-hook +account1 save-hook . +account1/INBOX
# repeated for each separate account
# Note: We know the documentation for folder-hook says "regexp", but using +account (for some given account) seems to work too for whatever reason

It is unlikely that the most recent save-hook take precedence, so
I added unhook (change it to unhook save-hook or whatever you test to work)

    unhook [ *  | hook-type ]
              This  command will remove all hooks of a given type, or all hooks when "*" is used as an argu-
              ment.  hook-type can be any of the -hook commands documented above.

The following info might be useful for other users who have simpler needs and somehow came to this page:

              Type: boolean
              Default: no

              If set, mutt will take the sender's full address when choosing a default folder for  saving  a
              mail. If $save_name or $force_name is set too, the selection of the Fcc folder will be changed
              as well.

              Type: boolean
              Default: no

              This  variable  controls how copies of outgoing messages are saved.  When set, a check is made
              to see if a mailbox specified by the recipient address exists (this is done by searching for a
              mailbox  in  the  $folder  directory with the username part of the recipient address).  If the
              mailbox exists, the outgoing message will be saved to that mailbox, otherwise the  message  is
              saved to the $record mailbox.

              Also see the $force_name variable.

An example:
set save_address=yes

Please tell me and others how you solved the problem.

Answered By: minorChaos
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.