Steve Mitchell
Steve Mitchell

Reputation: 1

Loading an Emacs init file

In Emacs, I am putting on the menu an item to load the init.el file, since I am in there almost daily. menu code is working fine, but the file isn't loading. So in a buffer, for troubleshooting, I enter:

(load user-init-file)

and use C-x C-e to execute it.

Turns out if fails because it needs double backslashes in the path. user-init-file resolves to "c:\steve\emacs\init.el" But should be `

"c:\\steve\\emacs\\init.el"

is there a function already to convert to the double backslashes? Or how do I do that with a search/replace?

This is similar to other search questions I found except this is for replacing within a string instead of within a buffer.

Upvotes: 0

Views: 1051

Answers (1)

Drew
Drew

Reputation: 30718

I think you probably just want (load-file user-init-file). load-file does not use load-path, and it does not try to append .elc or .el.

(If you use MS Windows notation for a file name then you can see what Emacs really thinks the file name is, by calling file-truename on it.)


If you really want to use load, try (load user-init-file nil nil t).

load tries to expand its FILE arg, automatically adding .elc and .el. The 4th argument is NOSUFFIX, which if non-nil prevents that behavior.

C-h f load:

**load-** is a built-in function inC source code`.

(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)

Execute a file of Lisp code named FILE.

First try FILE with .elc appended, then try with .el, then try FILE unmodified (the exact suffixes in the exact order are determined by load-suffixes). Environment variable references in FILE are replaced with their values by calling substitute-in-file-name. This function searches the directories in load-path.

If optional second arg NOERROR is non-nil, report no error if FILE doesn't exist. Print messages at start and end of loading unless optional third arg NOMESSAGE is non-nil (but force-load-messages overrides that).

If optional fourth arg NOSUFFIX is non-nil, don't try adding suffixes .elc or .el to the specified name FILE.

If optional fifth arg MUST-SUFFIX is non-nil, insist on the suffix .elc or .el; don't accept just FILE unless it ends in one of those suffixes or includes a directory name.

If NOSUFFIX is nil, then if a file could not be found, try looking for a different representation of the file by adding non-empty suffixes to its name, before trying another file. Emacs uses this feature to find compressed versions of files when Auto Compression mode is enabled.

If NOSUFFIX is non-nil, disable this feature.

The suffixes that this function tries out, when NOSUFFIX is nil, are given by the return value of get-load-suffixes and the values listed in load-file-rep-suffixes. If MUST-SUFFIX is non-nil, only the return value of get-load-suffixes is used, i.e. the file name is required to have a non-empty suffix.

When searching suffixes, this function normally stops at the first one that exists. If the option load-prefer-newer is non-nil, however, it tries all suffixes, and uses whichever file is the newest.

Loading a file records its definitions, and its provide and require calls, in an element of load-history whose car is the file name loaded. See load-history.

While the file is in the process of being loaded, the variable load-in-progress is non-nil and the variable load-file-name is bound to the file's name.

Return t if the file exists and loads successfully.

Upvotes: 2

Related Questions