MuttInEmacs

Emacs as your external editor for Mutt

Add the following to your ~/.emacs in order to have all files whose name contains “/mutt” to be in mail-mode.

    (server-start)
    (add-to-list 'auto-mode-alist '("/mutt" . mail-mode))

Change ~/.muttrc:

  set editor=emacsclient
  set pager=emacsclient

You can also use GnuClient instead of EmacsClient, if you installed it.

XEmacs as your external editor for Mutt

Use the same as above, but change your ~/.muttrc to read as follows:

  set editor=gnuclient
  set pager=gnuclient

You need that because XEmacs uses GnuClient instead of EmacsClient.

Run Mutt within Emacs or XEmacs

You can run mutt within an AnsiTerm: M-x ansi-term.

Note that ansi-term remaps your default C-x key to C-c.

See also “Extremely slow screen clearing in Emacs 24” in AnsiTermHints

Further Configuration

Since your Mutt mails are now in mail-mode, you can use mail-mode-hook for further configurations. Here is an example to add AutoFillMode:

    (add-hook 'mail-mode-hook 'turn-on-auto-fill)

Replacing C-x # with C-x k

This turns on auto-fill-mode, abbrev-mode and sets C-x k to server-edit so that you don’t have to change from using C-x k to C-x # depending on the buffer

  (defun my-mail-mode-hook ()
     (auto-fill-mode 1)
     (abbrev-mode 1)
     (local-set-key "\C-Xk" 'server-edit))
   (add-hook 'mail-mode-hook 'my-mail-mode-hook)

Replacing C-x # with C-c C-c

Here’s another way which binds C-c C-c and doesn’t prompt whether you want to save the buffer first:

  ;; Yes, you can do this same trick with the cool "It's All Text" firefox add-on :-)
  (add-to-list 'auto-mode-alist '("/mutt-\\|itsalltext.*mail\\.google" . mail-mode))
  (add-hook 'mail-mode-hook 'turn-on-auto-fill)
  (add-hook
   'mail-mode-hook
   (lambda ()
     (define-key mail-mode-map [(control c) (control c)]
       (lambda ()
         (interactive)
         (save-buffer)
         (server-edit)))))

Using Message mode for Mutt

Following snippet opens any file having name “mutt” in it in message mode, sets mail-header-separator to “” for M-q and auto-fill to work correctly, sets auto-fill-mode default for message-mode and assigns C-c C-c in message mode to close the server buffer and exit quickly.

  (add-to-list 'auto-mode-alist '(".*mutt.*" . message-mode))                                                                   
  (setq mail-header-separator "")                                                                                               
  (add-hook 'message-mode-hook 'auto-fill-mode)                                                                                 
  (define-key message-mode-map (kbd "C-c C-c")  '(lambda ()                                                                     
                                                 "save and exit quickly"                                                      
                                                 (interactive)                                                                
                                                 (save-buffer)                                                                
                                                 (server-edit)))                                                              
                                                                                  

Usage report: Tried code above on debian using emacs 23 but the keybinding doesn’t work. Error is ‘Symbol’s value as variable is void: message-mode-map’. Unfortunately I hardly know any elisp so could anyone please correct it (if it is easily correctable)?

Response: I haven’t tested the below extensively, but I believe it solves your problem (which was mine as well).

  (add-to-list 'auto-mode-alist '(".*mutt.*" . message-mode))
  (setq mail-header-separator "")
  (add-hook 'message-mode-hook 'auto-fill-mode)
  (defvar message-mode-map
    (let ((map (make-sparse-keymap)))
      (define-key map (kbd "C-c C-c")  '(lambda ()
                                        "save and exit quickly"
                                        (interactive)
                                        (save-buffer)
                                        (server-edit)))
      map)
    "Add Mutt C-c C-c command to message-mode keymap (creating it if needed)"
  )

Update: Thank you very much, above code works but you completely replace message-mode’s keymap so all other message-mode-bindings (for adding headers, jumping to body etc.) are gone. I finally managed to correct the first solution by wrapping the keybinding in a hook (problem seemed to be, that the ‘message-mode-map’-variable is only available when in message mode). The complete snippet now reads:

  (add-to-list 'auto-mode-alist '(".*mutt.*" . message-mode))                                                                   
  (setq mail-header-separator "")                                                                                               
  (add-hook 'message-mode-hook
          'turn-on-auto-fill
          (function
           (lambda ()
             (progn
               (local-unset-key "\C-c\C-c")
               (define-key message-mode-map "\C-c\C-c" '(lambda ()
                                                          "save and exit quickly"
                                                          (interactive)
                                                          (save-buffer)
                                                          (server-edit)))))))

I’m sure this can be written more concisely but as I said, I hardly know anything about elisp and don’t want to fiddle any furher… At least everything works as it should now 😊

Switching window focus between mutt and emacs when using emacsclient

See the "Automatically managing window manager focus" section of EmacsClient.


CategoryMail CategoryExternalUtilities