reddit / emacs

Using init.el as a wrapper to a primary config

derive-mor · 2022-06-19 22:40
My `.emacs.d` has the current files: `init.el`, `early-init.el`, and `custom.el`. How would I go about loading the `custom.el` file in `init.el`? Are there any advantages of having a wrapper of this sort?
thetemp_ · 2022-06-19 23:05
(setq custom-file "~/.emacs.d/custom.el") (load custom-file) See [Saving-Customizations](https://www.gnu.org/software/emacs/manual/html_node/emacs/Saving-Customizations.html) from the manual. The advantage is that settings you make in the Customization interface won't cause Emacs to make changes to your init file. You might not want that, because maybe your init file is under version control, or you just don't like the idea of it being touched except by you when directly editing it. I find it advantageous, because it provides an easy way to divide my settings between those that are more or less permanent (that I always want) and those that are transitory or that I might just be testing out.
onetom · 2022-06-20 02:00
have a look at how https://github.com/plexus/chemacs2 does this sort of things. chemacs won't be necessary in the future probably, because Emacs 29 has introduced an option for choosing an init file. btw, the "modern" default location for the init file is ~/.config/emacs/init.el I think it's worth transitioning to this convention, so address all your configs via the ~/.config/ directory.
flyin1501 · 2022-06-20 04:23
Doesn't Emacs still use ~/.emacs.d/ by default if no init file exists?
onetom · 2022-06-20 04:27
https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html#:~:text=When%20Emacs%20is%20started%2C%20it,Traditionally%2C%20file%20~%2F. > When Emacs is started, it normally tries to load a Lisp program from an initialization file, or init file for short. This file, if it exists, specifies how to initialize Emacs for you. Traditionally, file ~/.emacs is used as the init file, although Emacs also looks at ~/.emacs.el, ~/.emacs.d/init.el, ~/.config/emacs/init.el, or other locations. See How Emacs Finds Your Init File. > > You may find it convenient to have all your Emacs configuration in one directory, in which case you should use ~/.emacs.d/init.el or the XDG-compatible ~/.config/emacs/init.el.
flyin1501 · 2022-06-20 05:06
I know, it supports `~/.config`; that unfortunately doesn't mean it's used as the default: - `emacs -Q` or `env HOME=/tmp emacs` (start Emacs without an init file) - `C-h user-emacs-directory` → `~/.emacs.d` Or will this change in 29?
onetom · 2022-06-20 05:35
there is no reason for changing that default, but that doesn't imply, that it's the preferred user-emacs-directory. it has priority, for the sake of backwards compatibility. why is it an important case for you, when there is no init.el? btw, `locate-user-emacs-file` is a useful function, I've just learnt about recently. it allows referring to files relative to `user-emacs-home`.
flyin1501 · 2022-06-20 06:23
The case without an init file is the default case: it is `~/.emacs.d` that gets created for a new user (by `locate-user-emacs-file`). So it's not helpful to call `~/.config/emacs` a default when it isn't, as much as I hope it is.
ws-ilazki · 2022-06-20 06:28
> The advantage is that settings you make in the Customization interface won't cause Emacs to make changes to your init file. You might not want that, because maybe your init file is under version control, or you just don't like the idea of it being touched except by you when directly editing it. This is why I did it, at least initially. However, as my init file became larger I took it a step further by breaking out pieces of it into different files for better organisation. Face configurations go in face-config.el, keybindings are in keybind-config.el, my rather large tabbar customisation is in tabbar-config.el, etc. and then my custom init gets the stuff that isn't big enough to get its own file yet. I've considered changing the layout so it's based more on what modes I'm adjusting for things, so for example keybinds related to a specific mode would go in a file with other setup for that mode, and then only using the catch-all files like keybind-config.el for stuff that doesn't warrant a dedicated config file. Regardless of how you set it up, it makes finding some things a lot easier as the init stuff gets bigger.
onetom · 2022-06-20 10:43
well, maybe we should talk in full sentences :) when I said > there is no reason for changing that default I was referring to your question about the default value for `user-emacs-directory`, when NO init file is found. when I said > the "modern" default location for the init file is `~/.config/emacs/init.el` I meant, that out of the 5 default init file paths, this one is the latest addition, the one looked for the last, hence I called it "the modern default". if u have a `~/.config/emacs/init.el`, and only that one, then Emacs will load it, without any special configuration or command line options, so I think it's fair to call it *a* default init file location. I didn't want to say anything about the `user-emacs-directory`; that's something you brought up.
onetom · 2022-06-20 10:45
btw, I'm using the https://github.com/emacscollective/no-littering package to determine the location of the `custom.el`: https://github.com/onetom/onetomacs/blob/main/lib/use-no-littering.el
lastnamebird · 2022-06-20 12:41
`~/.config/emacs/init.el` is not looked for last. More generally, can you have *multiple* defaults for a single parameter/setting in a program? I’d think a default is the value chosen without any action taken by the user, right?
onetom · 2022-06-20 18:05
good question... according to your definition (that the user did nothing emacs specific yet), indeed we can say `~/.emacs.d/init.el` is the default init file. but what would you call `~/.emacs.el`, `~/.emacs` and `~/.config/emacs/init.el` in that case? in my reading, this documentation suggest that the `~/.config/emacs/init.el` is being looked at last: https://www.gnu.org/software/emacs/manual/html_node/emacs/Find-Init.html the init file being chosen is not a setting, nor a parameter. (yet. it's going to be a command-line arg in Emacs 29) so we can not talk about a simple default value for it. I haven't read the source code yet, but there seems to be 2 defaults, baked into Emacs, which affects the chosen/effective value of the init file path: 1. *list* of init file paths to be checked for existence. this list is not completely hard-wired, because it depends on the `HOME`, `LOGNAME`, `USER`, `XDG_CONFIG_HOME` env vars 2. IF NONE of the init files from the default init file list exists, then the algorithm defaults to the not yet existing `~/.emacs.d/init.el` and creates it that's my mental model of this topic.
lastnamebird · 2022-06-20 21:26
> but what would you call ~/.emacs.el, ~/.emacs and ~/.config/emacs/init.el in that case? I'm unsure...perhaps just options/values. Since there is a "finite" set (aside from unlimited options for the xdg var) of options `~/.emacs.el`, `~/.emacs`, `~/.config/emacs/init.el` and `~/XDG_CONFIG_HOME/emacs/init.el` there is no real reason they have to be called anything. > the init file being chosen is not a setting, nor a parameter. (yet. it's going to be a command-line arg in Emacs 29) so we can not talk about a simple default value for it. I would argue this is a setting since the user can change it user can change it with the creation of a file. However, you can change just about anything in emacs so perhaps it isn't that descriptive. >in my reading, this documentation suggest that the ~/.config/emacs/init.el is being looked at last: I think this is the relevant function in `startup.el`: ;; Return the name of the init file directory for Emacs, assuming ;; XDG-DIR is the XDG location and USER-NAME is the user name. If ;; USER-NAME is nil or "", use the current user. Prefer the XDG ;; location only if the .emacs.d location does not exist. (defun startup--xdg-or-homedot (xdg-dir user-name) (let ((emacs-d-dir (concat "~" user-name (if (eq system-type 'ms-dos) "/_emacs.d/" "/.emacs.d/")))) (cond ((or (file-exists-p emacs-d-dir) (if (eq system-type 'windows-nt) (if (file-directory-p (concat "~" user-name)) (directory-files (concat "~" user-name) nil "\\`[._]emacs\\(\\.elc?\\)?\\'")) (file-exists-p (concat "~" init-file-user (if (eq system-type 'ms-dos) "/_emacs" "/.emacs"))))) emacs-d-dir) ((file-exists-p xdg-dir) xdg-dir) (t emacs-d-dir)))) regardless, I think we both have an understanding of the docs. I just wanted to be sure if anyone comes across this they dont expect, at this time, emacs to default to the xdg location without any action on their part.