ChatGPT解决这个技术问题 Extra ChatGPT

How can I reload .emacs after changing it?

How can I get Emacs to reload all my definitions that I have updated in .emacs without restarting Emacs?

I know emacs is often considered an OS, but...s/reboot/restart/?
I'm not a developer, so alot of the answers that make sense immediately to developers don't mean that much to me
@MJB the purpose of Stack Overflow is to be a knowledge repository for even the most basic of questions. This question fits in perfectly with the purpose of Stack Overflow. Please see stackoverflow.com/faq
haha now five years later the suggested "Google search first" leads here! nice

p
programking

You can use the command load-file (M-x load-file, then press return twice to accept the default filename, which is the current file being edited).

You can also just move the point to the end of any sexp and press C-xC-e to execute just that sexp. Usually it's not necessary to reload the whole file if you're just changing a line or two.


Keep in mind that this may not always do exactly what you think it does. If you have variables whose contents are flopped( ie setting a boolean to it's opposite) then the behavior won't be the same as loading the file.
Any side-effects in general are likely to break the desired behavior: loading files, etc.
When I do this, I get the message: load-with-code-conversion: Symbol's value as variable is void: n
Instead of moving point behind a defun or defvar, you can also leave it inside the declaration body and type C-M-x. Very handy.
What about M-x load-file then ~/.emacs in place of default.
P
Peter

Very strange that the very convenient

M-x eval-buffer

is not mentioned here.

It immediately evaluates all code in the buffer, its the quickest method, if your .emacs is idempotent.


Who says that .emacs is (or has to be) idempotent?
@mike : not me. "Given a, b is true" meant for me, "if a, b is true" , but I can see what you mean. I change given to if for more clarity.
@mike: When would it make sense for .emacs not to be idempotent?
I would have thought this to be the first suggestion.
I wouldn't say it is good style but it might happen. For example you could choose not to redefine a function if it is already defined (from the first startup). You might not see a change in its definition when evaluating your init file.
D
Dale Hagglund

You can usually just re-evaluate the changed region. Mark the region of ~/.emacs that you've changed, and then use M-x eval-region RET. This is often safer than re-evaluating the entire file since it's easy to write a .emacs file that doesn't work quite right after being loaded twice.


Interesting. I hadn't considered the idea of a .emacs file that wasn't idempotent. Something to watch out for as I start with ELisp
I would guess it's rare to find an .emacs file that is idempotent, at least in the strong sense of two executions resulting in exactly the same state as one execution. At the very least, it's quite common to append to various lists, often in ways that won't suppress duplicate entries when executed a second time.
One other thought: using the approach I suggest above, it's possible that the emacs state you've got isn't what your .emacs actually does. Ie, since you haven't executed the entire .emacs file from a clean slate, you don't actually know it does what you want.
That being said, wouldn't it be possible to write .emacs in an idempotent way? Global state is icky.
D
Dominic Rodger

If you've got your .emacs file open in the currently active buffer:

M-x eval-buffer

One thing to be aware of here is that doing this may not leave you in a state identical to a restart approach. (Contrived example: if you had a "toggle" affect in your .emacs).
d
digitaldreamer
M-x load-file
~/.emacs

Doing this I learned that on my Windows 7 installation, the .emacs.d folder was located in the %appdata% folder instead of the users home folder.
佚名

solution

M-: (load user-init-file)

notes

you type it in Eval: prompt (including the parentheses)

user-init-file is a variable holding the ~/.emacs value (pointing to the configuration file path) by default

(load) is shorter, older, and non-interactive version of (load-file); it is not an emacs command (to be typed in M-x) but a mere elisp function

conclusion

M-: > M-x


is M-: > Ctrl Alt x? based on your logic I imagine M-: < C-M-x when only one line in the init file is changed? Maybe it's just ==
B
Bahbar

Others already answered your question as stated, but I find that I usually want to execute the lines that I just wrote. for that, CtrlAltx in the lisp works just fine.


See also eval-lastsexp, at C-x C-e
With C-M-x, you don't have to be "at the end of the lisp" -- are you thinking of C-x C-e perhaps? The nice thing about C-M-x is that you can invoke it even when point is in the middle of a function/variable declaration.
I probably got into the habit of going to end of lisp at the time I only used C-x C-e. Thanks for the catch!
D
Dominic Rodger

The following should do it...

M-x load-file

J
Joe Casadonte

I suggest that you don't do this, initially. Instead, start a new emacs session and test whatever changes you made to see if they work correctly. The reason to do it this way is to avoid leaving you in a state where you have an inoperable .emacs file, which fails to load or fails to load cleanly. If you do all of your editing in the original session, and all of your testing in a new session, you'll always have something reliable to comment out offending code.

When you are finally happy with your changes, then go ahead and use one of the other answers to re-load. My personal preference is to eval just the section you've added/changed, and to do that just highlight the region of added/changed code and call M-x eval-region. Doing that minimizes the code that's evaluated, minimizing any unintentional side-effects, as luapyad points out.


R
Radon Rosborough

Keyboard shortcut:

(defun reload-init-file ()
  (interactive)
  (load-file user-init-file))

(global-set-key (kbd "C-c C-l") 'reload-init-file)    ; Reload .emacs file

W
WisdomFusion
C-x C-e ;; current line
M-x eval-region ;; region
M-x eval-buffer ;; whole buffer
M-x load-file ~/.emacs.d/init.el

j
jacekmigacz

Define it in your init file and call by M-x reload-user-init-file

(defun reload-user-init-file()
  (interactive)
  (load-file user-init-file))

This is actually my preferred answer: take something that exists, and make it accessible by M-x (rather than M-: and extra typing). M-x is indeed the normal way to access useful commands that are not common enough to deserve their own keybinding.
Best answer, as it also works if your config lives in ~/.config/emacs/ instead of ~/.emacs.d/
A
AAAfarmclub

I'm currently on Ubuntu 15.04; I like to define a key for this. [M-insert] translates to alt-insert on my keyboard. Put this in your .emacs file:

(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))

P
Picaud Vincent

Beside commands like M-x eval-buffer or M-x load-file you can restart a fresh emacs from the command line:

emacs -q --load "init.el"

Usage example stackoverflow.com/questions/44959535/company-backends-in-gnu-emacs/


The question asks how to do this without restarting Emacs. Launching a new copy, while technically answering the question, doesn't seem to fit the spirit of the question. 😄
u
user1026139

Although M-x eval-buffer will work you may run into problems with toggles and other similar things. A better approach might be to "mark" or highlight whats new in your .emacs (or even scratch buffer if your just messing around) and then M-x eval-region. Hope this helps.


X
X.Creates

Here is a quick and easy way to quick test your config. You can also use C-x C-e at the end of specific lisp to execute certain function individually.

C-x C-e runs the command eval-last-sexp (found in global-map), which is an interactive compiled Lisp function. It is bound to C-x C-e. (eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL) Evaluate sexp before point; print value in the echo area. Interactively, with prefix argument, print output into current buffer. Normally, this function truncates long output according to the value of the variables ‘eval-expression-print-length’ and ‘eval-expression-print-level’. With a prefix argument of zero, however, there is no such truncation. Such a prefix argument also causes integers to be printed in several additional formats (octal, hexadecimal, and character). If ‘eval-expression-debug-on-error’ is non-nil, which is the default, this command arranges for all errors to enter the debugger.


V
Vinh Trieu

You can set key-binding for emacs like this

;; reload emacs configuration
(defun reload-init-file ()
  (interactive)
  (load-file "~/.emacs"))

(global-set-key (kbd "C-c r") 'reload-init-file) 

Hope this will help!


b
benjaminz

If you happen to have a shell opened inside Emacs, you can also do:

. ~/.emacs

May save a few key strokes.