How do you recompile an .el source file [...]
In Elisp file-visiting buffers, you can run the command M-x
emacs-lisp-byte-compileRET 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-x
byte-compile-fileRET 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-x
emacs-lisp-byte-compile-and-loadRET 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-x
byte-compile-fileRET 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-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.
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:
defcustom forms do not reset their
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 (
defface forms in a similar way to
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.