434

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

D A Vincent
  • 364
  • 2
  • 21
yazz.com
  • 57,320
  • 66
  • 234
  • 385
  • 23
    I know emacs is often considered an OS, but...s/reboot/restart/? – William Pursell Feb 17 '10 at 15:21
  • 8
    I'm not a developer, so alot of the answers that make sense immediately to developers don't mean that much to me – yazz.com Apr 05 '10 at 19:50
  • 9
    @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 http://stackoverflow.com/faq – chollida Apr 05 '10 at 19:51
  • 5
    possible duplicate of http://stackoverflow.com/questions/2281593/how-can-i-load-changes-to-my-emacs-without-rebooting-emacs – Trey Jackson Apr 05 '10 at 19:52
  • Yes, this is a duplicate, thanks. I didn't see the other one when I posted – yazz.com Apr 05 '10 at 19:56
  • 2
    I did not intend to sound so condescending. I realize that even basic questions are ask-able here, but I was serious that a google search was quicker than waiting for an answer. My apologies. – MJB Apr 05 '10 at 20:01
  • No worries MJB. I'm learning to google stuff more and more all the time, but usually I get much more "understandable" (for me at least) answers from stack overflow – yazz.com Apr 11 '10 at 08:14
  • possible duplicate of [Reload configurations without restarting Emacs](http://stackoverflow.com/questions/167705/reload-configurations-without-restarting-emacs) – wap26 Sep 11 '15 at 10:06
  • 14
    haha now five years later the suggested "Google search first" leads here! nice – JimLohse Jan 08 '16 at 17:49
  • 4
    @JimLohse and another 5 years, and most of the first-page search results on "emacs load .emacs" are here or stack exchange. i sometimes wonder if people realize that for google to deliver good answers, somebody has to write them. – Peter S Magnusson Jan 29 '21 at 19:16

18 Answers18

406

You can use the command load-file (M-x load-file, and 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-x, C-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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 23
    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. – chollida Apr 05 '10 at 19:54
  • 4
    Any side-effects in general are likely to break the desired behavior: loading files, etc. – deprecated Jul 30 '12 at 14:55
  • When I do this, I get the message: load-with-code-conversion: Symbol's value as variable is void: n – Chris Marie Jan 19 '15 at 20:40
  • 5
    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. – Thomas May 12 '15 at 07:30
  • What about `M-x load-file` then `~/.emacs` in place of default. – young_souvlaki Oct 04 '21 at 21:04
182

There is the very convenient

M-x eval-buffer

It immediately evaluates all code in the buffer. It's the quickest method if your .emacs file is idempotent.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter
  • 47,963
  • 46
  • 132
  • 181
  • 4
    Who says that `.emacs` is (or has to be) idempotent? – mike May 18 '15 at 16:48
  • 2
    @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. – Peter May 18 '15 at 16:52
  • 8
    @mike: When would it make sense for `.emacs` not to be idempotent? – Zaz May 18 '15 at 23:14
  • I would have thought this to be the first suggestion. – Stryker Feb 22 '17 at 14:14
  • 2
    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. – Kaligule Jun 23 '17 at 11:55
  • 1
    The useful functions are `eval-buffer` and `eval-defun`. I have these bound to C-c C-k and C-c C-c, respectively (same keys as SLIME). I rarely actually eval the whole buffer, usually only evaling the function or definitions I'm actually working on. – George Kettleborough Mar 13 '18 at 10:40
  • @GeorgeKettleborough doesn't `C-c C-c` collide with the escapes you use when hosting a shell? All emacs commands starting with `C-x ...` are mapped to `C-c ...`, when using the bash through `term`. – mju Sep 10 '22 at 11:05
  • @Zaz I don't use emacs heavily enough to have run into this use-case, and I would strive to make things idempotent precisely to have this kind of stuff never matter, but I've definitely had situations in life where it was just simpler to code something in a not-idempotent way. For example, in my `zsh` config, for several years I had a piece of code that added a prefix to `PS1` - if that file was sourced on its own, it wasn't idempotent (though it was idempotent if the whole config was sourced). I imagine there exist more than zero situations for `.emacs` where it's easier to skip idempotency. – mtraceur Mar 30 '23 at 04:28
48

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.

Dale Hagglund
  • 16,074
  • 4
  • 30
  • 37
  • 2
    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 – Erik Apr 11 '12 at 20:39
  • 2
    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. – Dale Hagglund Apr 19 '12 at 06:44
  • 1
    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. – Dale Hagglund Apr 19 '12 at 06:45
  • 1
    That being said, wouldn't it be possible to write .emacs in an idempotent way? Global state is icky. – Ehtesh Choudhury Nov 06 '12 at 05:17
47

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

M-x eval-buffer
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
  • 13
    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). – luapyad Feb 17 '10 at 19:25
32

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    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 == – Robert Houghton Feb 10 '20 at 06:16
30
M-x load-file
~/.emacs
digitaldreamer
  • 52,552
  • 5
  • 33
  • 28
15

Others already answered your question as stated, but I find that I usually want to execute the lines that I just wrote.

For that, Ctrl + Alt + X in the Elisp part works just fine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bahbar
  • 17,760
  • 43
  • 62
  • 3
    See also `eval-lastsexp`, at `C-x C-e` – Derek Slager Feb 17 '10 at 17:11
  • 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. – Thomas May 12 '15 at 07:32
  • 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! – Bahbar May 13 '15 at 07:07
13

The following should do it...

M-x load-file
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
Pace
  • 41,875
  • 13
  • 113
  • 156
12

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 reload. 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe Casadonte
  • 15,888
  • 11
  • 45
  • 57
10

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
Resigned June 2023
  • 4,638
  • 3
  • 38
  • 49
Matt Kneiser
  • 1,982
  • 18
  • 23
8
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
WisdomFusion
  • 440
  • 6
  • 12
  • An explanation would be in order. E.g., what is the idea/gist? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/11913712/edit), not here in comments (****************** ***without*** ****************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 18 '23 at 21:13
5

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))
jacekmigacz
  • 789
  • 12
  • 22
  • 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. – Dalker Jun 22 '20 at 06:31
  • Best answer, as it also works if your config lives in `~/.config/emacs/` instead of `~/.emacs.d/` – Kaligule Apr 20 '21 at 04:29
4

I'm currently on Ubuntu 15.04 (Vivid Vervet); I like to define a key for this.

[M-insert] translates to Alt + Ins on my keyboard.

Put this in your .emacs file:

(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AAAfarmclub
  • 2,202
  • 1
  • 19
  • 13
3

Besides commands like M-x eval-buffer or M-x load-file, you can restart a fresh Emacs instance from the command line:

emacs -q --load "init.el"

Usage example: Company backends in GNU Emacs

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
  • The question asks how to do this without restarting Emacs. Launching a new copy, while [technically](https://www.youtube.com/watch?v=hou0lU8WMgo) answering the question, doesn't seem to fit the spirit of the question. – chwarr May 14 '20 at 23:08
2

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.

XY L
  • 25,431
  • 14
  • 84
  • 143
2

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 what’s new in your .emacs file (or even scratch buffer if you're just messing around) and then M-x eval-region.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

You can set a 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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vinh Trieu
  • 993
  • 8
  • 12
-2

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

. ~/.emacs

It may save a few key strokes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
benjaminz
  • 3,118
  • 3
  • 35
  • 47