0

I'm trying to tidy up my init.el file which is currently an absolute mess. I've looked at other people's files online so as to get some inspiration but none of these examples feature this particular block:


(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.
 '(ispell-dictionary nil)
 '(package-selected-packages
   '(sonic-pi browse-kill-ring sclang-extensions async magit avy csv-mode company ein conda perspective buffer-move pdf-tools which-key helpful use-package sudo-edit))
 '(send-mail-function 'mailclient-send-it))
(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.
 )

So I've no idea where to start from as a result of this and there isn't much info on the web. I was interested in re organizing my file with use-package as it seems like quite a tidy way of doing it but I've no idea as to where this bit of code should go??

d m
  • 1

3 Answers3

0

The block you are displaying is, as the text points out, added by the customize interface (see also this answer). To point Emacs to any other initialization file, see the documentation.

If you are looking to tidy up your Emacs configuration in general, I would highly suggest a literate programming approach. This means that you write your Emacs configuration in ~/.emacs.d/config.org and include blocks that are tangled by org-babel-tangle:

    #+begin_src emacs-lisp :tangle yes
      (all-your-config-stuff ...)
    #+end_src

Basic recipe

  • Install org-babel. It should already be installed when using a version of Emacs >=24
  • Replace the contents of your ~/.emacs.d/init.el with one line (org-babel-load-file "~/.emacs.d/config.org")
  • Enjoy Emacs

Updating your config

Everytime you restart Emacs the newest version of config.org will be 'tangled' into config.el and the init file will be properly loaded. When you don't want to restart Emacs while messing around with your config, simply tangle the file using M-x org-babel-tangle-file and evaluate your config.el by opening a buffer that loads config.el and performing M-x eval-buffer.

Example

Here is an example of ~/.emacs.d/config.org of my personal configuration. It is the part that takes care of use-package initialization. Note the additional explanation that is added in Org-mode. As this explanation is outside of a (to be) tangled source block, it will not appear in your config.el.

First we need to use the built-in package manager to actually download
=use-package= when it is not available.
#+begin_src emacs-lisp :tangle yes
  ;; Use the built-in package manager and specify archive
  (require 'package)
  (add-to-list 'package-archives
               '("melpa" . "https://melpa.org/packages/") t)
  (add-to-list 'package-archives
               '("melpa-stable" . "https://stable.melpa.org/packages/") t)
  ;; Initialize built-in package management
  (package-initialize)
  ;; Install use-package if not available
  (unless (package-installed-p 'use-package)
    (package-refresh-contents)
    (package-install 'use-package))
#+end_src

Then we can actually load =use-package=
#+begin_src emacs-lisp :tangle yes
  ;; Load use-package by requiring it, to ensure it has been loaded
  (eval-when-compile
    (require 'use-package))
#+end_src
  • Thank you for your extensive answer. I looked previously at the links you are referring to but unfortunately there isn't one which really answers my question, hence my post. What I'm trying to figure out is that I did not customize anything with the `customize` interface, there are references somewhere that this block might be added when installing packages via `M-x package-install` but the references are quite vague Your suggestion of writing a config with org-babel is interesting but for the time being I think I might be better off following the online tutorials such as System Crafters etc.. – d m Mar 21 '23 at 15:32
0

So I can answer my own question after having found the explanation on stack exchange

To quote emacs' documentation "This variable is fed automatically by Emacs when installing a new package.". Yet I've yet to see someone's init file with this block in it, or a line such as (setq custom-file (make-temp-file "emacs-custom")).

I'm mystified

d m
  • 1
0

As stated, those forms are written by the customize interface. You can control where customize stores this information by setting the variable custom-file. See its documentation with C-h v custom-file RET. Paraphrasing those docs, you can put something like the following in your ~/.emacs.d/init.el file:

;; don't let customize add stuff to my init.el
(setq custom-file "~/.emacs.d/custom.el")
(load custom-file)

Where you put it is up to you. Personally I set custom-file near the beginning of my init file, and then load it at the end.

nega
  • 2,526
  • 20
  • 25