How do I execute (“evaluate”) a piece of Emacs Lisp code?
There are a number of ways to execute (evaluate, in Lisp lingo) an Emacs Lisp form:
If you want it evaluated every time you run Emacs, put it in a file named
.emacs.d/init.elin your home directory. This is known as “your init file,” and contains all of your personal customizations (see How do I set up an init file properly?).You can type the form in the
*scratch*buffer, and then type LFD (or C-j) after it. The result of evaluating the form will be inserted in the buffer.In
emacs-lisp-mode, typing C-M-x evaluates a top-level form before or around point.Typing C-x C-e in any buffer evaluates the Lisp form immediately before point and prints its value in the echo area.
Typing M-: or M-x eval-expression allows you to type a Lisp form in the minibuffer which will be evaluated once you press RET.
You can use M-x load-file to have Emacs evaluate all the Lisp forms in a file. (To do this from Lisp use the function
loadinstead.)The functions
load-library,eval-region,eval-buffer,require, andautoloadare also useful; see Where can I get documentation on Emacs Lisp?, if you want to learn more about them.