6

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?

4
  • 2
    Emacs will exit from your init file immediately once it finds something is wrong, so if you put (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.
    – xuchunyang
    Commented Jan 9, 2016 at 11:23
  • @xuchunyang that help, but I think I used a function that was exiting from the emacs file some time ago when I was debugging init file but I don't remember what function it was.
    – jcubic
    Commented Jan 9, 2016 at 11:34
  • 2
    You'd be better served by --debug-init option you can add when starting Emacs.
    – wvxvw
    Commented Jan 9, 2016 at 14:29
  • @wvxvw I always have that enabled.
    – jcubic
    Commented Jan 9, 2016 at 15:55

1 Answer 1

11

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...
~$
5
  • Awesome, thanks. why you have space in the begining of the name of the buffer is it a typo?
    – jcubic
    Commented 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).
    – xuchunyang
    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?
    – NetMage
    Commented Nov 2, 2020 at 23:06
  • @NetMage When loading a byte-compiled file, I get (error "no buffer named *load*"). See pastebin.com/yQ3PDdJr.
    – kdb
    Commented Feb 4, 2021 at 9:06
  • For compilation the equivalent buffer would be " *Compiler Input*".
    – kdb
    Commented Feb 4, 2021 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.