2

When I install packages with M-x package-install, it edits the ~/.emacs file and adds the following content:

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
  '(package-selected-packages '(paredit slime)))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
)

Is there a way to prevent package-install from editing my ~/.emacs file. I write ~/.emacs by hand and keep it neat, so it is quite disconcerting when package-install inserts this big blob of code in between. If package-install really needs to maintain this code somewhere, can I not make it put this code in another file that I can load into ~/.emacs?

Drew
  • 75,699
  • 9
  • 109
  • 225
Lone Learner
  • 138
  • 7

3 Answers3

3

I think you are looking that:

(setq custom-file (concat user-emacs-directory "custom.el"))
(when (file-exists-p custom-file)
  (load custom-file))

Another way to get rid of the customization discussed here: https://www.reddit.com/r/emacs/comments/9rrhy8/emacsers_with_beautiful_initel_files_what_about/

1

If you have a file named custom-file, which you load from your init file, then Customize will save such customizations in that file.

I recommend doing that, to keep your hand-written Emacs-Lisp code separate (in your init file) from the file that Customize uses to write the code it uses to save your customizations.

I'm assuming that package-install does the same as Customize - it uses the file that's the value of variable custom-file, not your init file. But I don't know that for a fact. I don't see that mentioned in the doc string or in the manual. If it doesn't then maybe it would be good to send an enhancement request for that: M-x report-emacs-bug.

Really, there's no good reason that either the package system or Customize should fiddle with your init file. That's what custom-file is for: to tell Customize "Hands off my init file!"

Drew
  • 75,699
  • 9
  • 109
  • 225
0

custom-file is all you need, as the other answers say, but you might also be interested in a more general approach. You can "split" your customizations into as many files as you like, not just the one named by custom-file, if you use the package initsplit, available on MELPA.

From the package description:

This file allows you to split Emacs customizations (set via M-x customize) into different files, based on the names of the variables. It uses a regexp to match against each face and variable name, and associates with a file that the variable should be stored in.

It is then up to you to load each file, as in the answer @kadircancetin gave.

While the description mentions only M-x customize, the splitting takes place automatically whenever any customization is saved, including when package-install saves package-selected-packages.

Phil Hudson
  • 1,651
  • 10
  • 13