How do you recompile an .el source file [...]
In Elisp file-visiting buffers, you can run the command M-xemacs-lisp-byte-compile
RET to byte-compile the corresponding file.
In Dired buffers, you can type B (dired-do-byte-compile
) to byte-compile all specified Elisp files.
In all buffers, you can run the command M-xbyte-compile-file
RET to byte-compile an arbitrary file prompted with completion.
[...] and make it active in my current session
Similar to the aforementioned emacs-lisp-byte-compile
, the command M-xemacs-lisp-byte-compile-and-load
RET will byte-compile the current file, if necessary, before loading it.
Typing L (dired-do-load
) in a Dired buffer will load the corresponding file.
Passing a prefix argument to C-uM-xbyte-compile-file
RET will prompt for and byte-compile an arbitrary file as usual before additionally loading it.
I have been writing some elisp code, current i have to open the file edit quit and reload emacs seems there must be a better way ?
There are various alternative approaches you could take, depending on your preferred workflow:
Emacs provides several commands for evaluating Elisp at varying degrees of granularity and from various sources, such as in-buffer expressions or expressions read from the minibuffer. See (emacs) Lisp Eval
.
You can load Elisp files/libraries via the commands load-file
and load-library
, as Drew mentions in a comment. See (emacs) Lisp Libraries
.
See also the package auto-compile
for automatically compiling Elisp files on save/load.
Edit
As Tobias correctly warns in a comment, there are many caveats associated with reevaluation of Elisp, which is not always straight-forward. Two common pitfalls are:
Contrary to defun
, defvar
and defcustom
forms do not reset their INITVALUE
and STANDARD
arguments, respectively, when reloaded (i.e. when they have already been defined and set). Only a call to C-M-x (eval-defun
) in their vicinity will reset their value. [Since Emacs 28, C-xC-e (eval-last-sexp
) handles defvar
, defcustom
, and defface
forms in a similar way to eval-defun
.]
Reloading/redefining a macro will cause its new definition to be used henceforth, but all previously expanded call sites will have been expanded according to the old definition. Each call site has to be reevaluated and/or byte-compiled for the expansion to occur using the new macro definition.
M-x load-file RET <file> RET
. See also how toeval
Elisp buffers and theauto-compile
package.load-library
. If the file is in yourload-path
then that will automatically prefer to pick up the byte-compiled file (*.elc
) rather than the source file (*.el
).