How can I reload .emacs after changing it?

Emacs

Emacs Problem Overview


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

Emacs Solutions


Solution 1 - Emacs

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.

Solution 2 - Emacs

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.

Solution 3 - Emacs

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.

Solution 4 - Emacs

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

M-x eval-buffer

Solution 5 - Emacs

M-x load-file
~/.emacs

Solution 6 - Emacs

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

Solution 7 - Emacs

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.

Solution 8 - Emacs

The following should do it...

M-x load-file

Solution 9 - Emacs

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.

Solution 10 - Emacs

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

Solution 11 - Emacs

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

Solution 12 - Emacs

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))

Solution 13 - Emacs

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")))

Solution 14 - Emacs

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/

Solution 15 - Emacs

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.

Solution 16 - Emacs

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.

Solution 17 - Emacs

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!

Solution 18 - Emacs

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

. ~/.emacs

May save a few key strokes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionyazz.comView Question on Stackoverflow
Solution 1 - EmacsBryan OakleyView Answer on Stackoverflow
Solution 2 - EmacsPeterView Answer on Stackoverflow
Solution 3 - EmacsDale HagglundView Answer on Stackoverflow
Solution 4 - EmacsDominic RodgerView Answer on Stackoverflow
Solution 5 - EmacsdigitaldreamerView Answer on Stackoverflow
Solution 6 - Emacsuser4104817View Answer on Stackoverflow
Solution 7 - EmacsBahbarView Answer on Stackoverflow
Solution 8 - EmacsPaceView Answer on Stackoverflow
Solution 9 - EmacsJoe CasadonteView Answer on Stackoverflow
Solution 10 - EmacsMatt KneiserView Answer on Stackoverflow
Solution 11 - EmacsWisdomFusionView Answer on Stackoverflow
Solution 12 - EmacsjacekmigaczView Answer on Stackoverflow
Solution 13 - EmacsAAAfarmclubView Answer on Stackoverflow
Solution 14 - EmacsPicaud VincentView Answer on Stackoverflow
Solution 15 - Emacsuser1026139View Answer on Stackoverflow
Solution 16 - EmacsX.CreatesView Answer on Stackoverflow
Solution 17 - EmacsVinh TrieuView Answer on Stackoverflow
Solution 18 - EmacsbenjaminzView Answer on Stackoverflow