50.1.4 Saving Customizations

In the customization buffer, you can save a customization setting by choosing the ‘Save for Future Sessions’ choice from its ‘[State]’ button. The C-x C-s (Custom-save) command, or the ‘[Apply and Save]’ button at the top of the customization buffer, saves all applicable settings in the buffer.

Saving works by writing code to a file, usually your initialization file (see The Emacs Initialization File). Future Emacs sessions automatically read this file at startup, which sets up the customizations again.

You can choose to save customizations somewhere other than your initialization file. To make this work, you must add a couple of lines of code to your initialization file, to set the variable custom-file to the name of the desired file, and to load that file. For example:

(setq custom-file "~/.config/emacs-custom.el")
(load custom-file)

You can even specify different customization files for different Emacs versions, like this:

(cond ((< emacs-major-version 28)
       ;; Emacs 27 customization.
       (setq custom-file "~/.config/custom-27.el"))
      ((and (= emacs-major-version 26)
            (< emacs-minor-version 3))
       ;; Emacs 26 customization, before version 26.3.
       (setq custom-file "~/.config/custom-26.el"))
      (t
       ;; Emacs version 28.1 or later.
       (setq custom-file "~/.config/emacs-custom.el")))

(load custom-file)

If Emacs was invoked with the -q or --no-init-file options (see Initial Options), it will not let you save your customizations in your initialization file. This is because saving customizations from such a session would wipe out all the other customizations you might have on your initialization file.

Please note that any customizations you have not chosen to save for future sessions will be lost when you terminate Emacs. If you’d like to be prompted about unsaved customizations at termination time, add the following to your initialization file:

(add-hook 'kill-emacs-query-functions
          'custom-prompt-customize-unsaved-options)