I'm interested to know if it is possible to assign and use local variables in muttrc. For instance, let's say I have credentials in some file. I'll read this file as is, e.g.
set creds=`cat file`
and, then I would like to use $creds within my muttrc file for further processing, e.g.
set my_var=`head -1 $creds`
set my_foo=`tail -1 $creds`
Is it possible, and, if so, what is correct syntax (the one I shown above does not work for me)?
You were close.
mutt is able to "source" files that contain configuration commands. It's similar to when you make a change to a file like
.bashrcand want to read the changes into your current login withsource ~/.bashrc. Basically, you're telling mutt to load an additional configuration file and process it before continuing on with processing your primarymuttrc.The insecure version of what you want to do is to make a file called
credsthat looks something like this:Then source that file in your main
.muttrc:source ~/.mutt/creds # or wherever you put itWhen mutt is loading and gets to that line, it'll read that file and process it as if it were part of a
muttrcfile, then it'll continue on to the next line in your.muttrc.However, this approach means your passwords are sitting around in plaintext, which is not a best practice. So you can stop there and live with the risks (accidentally checking into version control, for instance) or you can improve the situation with gpg:
First, encrypt your
credsfile with gpg, e.g.gpg -r [email protected] --encrypt creds. That will give you an encrypted file calledcreds.gpg.Second, change your
.muttrcto decrypt that file when you first run mutt by replacing the line where you sourcedcredswith this one:source "gpg -d ~/.mutt/creds.gpg |"When you first run mutt, you'll get a password prompt to authenticate in gpg key, the file will be decrypted, and mutt will set the
imap_userandimap_passconfiguration variables before moving on to process the rest ofmuttrc.(Once you have this working, don't forget to delete the unencrypted
credsfile.)You could put just your password in there, but using
sourceis useful for keeping yourmuttrcsimple if you use mutt with multiple accounts, so you can make acreds.work,creds.personal, etc. It's also great for storing discrete account configs. I tend to follow the pattern:The
foo.profilefiles contain all the configuration directives for a given account, such asset signature = ~/.mutt/personal.sig,set realname = "John Doe"` server variables, mailboxes, etc.Macros can switch from one account to the other:
So if I'm in the index of my personal account and want to switch to my work account, I tap
.cw(for "change to work") and that sources mywork.profileaccount, which in turn sources the signature and decrypts my work credentials. When I want to switch back, I tap.cp(for "change to personal") and it sources mypersonal.profilewhich then sources all the supporting files for that account.Using
sourceis a good way to break a longmuttrcinto logical pieces, such as macros, scores, colors, etc. and it makes it easier to make variants for multiple profiles/accounts by just tacking on the.workor.personalextension and sourcing them from the mainfoo.profilefile.My main
muttrcjust sets the things all the accounts should have in common, such as the cache directory, general configuration (date_format,index_format,attribution), and it pulls in my default profile withsource ~/.mutt/personal.profile.