I want to debug my init file, by exiting from loading the file in the middle so if something is wrong after that place it will not be loaded. How can I exit from .emacs file?
1 Answer
You can use this to exit from your init file:
(with-current-buffer " *load*"
(goto-char (point-max)))
Emacs uses the load
function to execute lisp file through a temporary buffer, that's " *load*"
, if the name is not already taken. The above code moves the point to the end of the buffer, thus the read
function will not read further code.
For example, the last sexp of foo.el
is unbalanced, but there is no error when running it, since Emacs can't go that far, it exits early:
~$ cat foo.el
(message "Loading %s..." load-file-name)
(with-current-buffer " *load*"
(goto-char (point-max)))
(this line does not matter
~$ emacs --batch --load foo.el
Loading /home/xcy/foo.el...
~$
-
Awesome, thanks. why you have space in the begining of the name of the buffer is it a typo?– jcubicCommented Nov 26, 2016 at 17:25
-
1@jcubic No, it isn't. Buffer name starting with a space has a special meaning (see gnu.org/software/emacs/manual/html_node/elisp/Buffer-Names.html if you are interested). Commented Nov 26, 2016 at 17:37
-
It seems like emacs should have a simple
(exit-load)
or(return)
for this. What if the file is byte-compiled?– NetMageCommented Nov 2, 2020 at 23:06 -
@NetMage When loading a byte-compiled file, I get
(error "no buffer named *load*")
. See pastebin.com/yQ3PDdJr.– kdbCommented Feb 4, 2021 at 9:06 -
For compilation the equivalent buffer would be
" *Compiler Input*"
.– kdbCommented Feb 4, 2021 at 9:08
(1 + 1)
to the beginning of your init file, the rest will not be loaded at all. is this what you want? BTW, I don't think this method will help you to debug your init file at all.--debug-init
option you can add when starting Emacs.