EmacsLispMode

Emacs Lisp Mode is one of the best ProgrammingModes that comes with Emacs for working with EmacsLisp.

Emacs Lisp Mode is enabled for any files ending in .el or when editing the Emacs InitFile.

Emacs Lisp Mode is used for EvaluatingExpressions. Indentation is available with ‘TAB’. ‘C-M-q’ indents the Sexp in front of point. Indent region is `C-M-\'. Newline and indent is ‘C-j’.

Completion is very well configured. Simply use ‘C-M-i’ or ‘M-TAB’. Because Emacs’s SelfDocumentation is really good its possible to read up on a function or variable in HelpMode based on its DocString.

ByteCompilation is possible with ‘M-x byte-compile-file’. Unfortunately, not all of the Emacs Lisp mode features are available in the LispInteractionMode. See InferiorEmacsLispMode for an Emacs Lisp REPL.

Starting a new Emacs Lisp file with ‘M-x auto-insert’ will use SkeletonMode to give the standard format for your source code. Enabling the global minor mode AutoInsertMode can do this for you every time you start a new file.

As an example of customizing Emacs Lisp Mode, with Emacs Lisp, we will enable these features.

Try these customizations by adding the following code it to your InitFile or by EvaluatingExpressions. If you are already visiting a buffer in Emacs Lisp Mode, then after evaluating this code reopen the associated file or restart the mode with ‘M-x normal-mode’.

    (add-hook 'emacs-lisp-mode-hook
              (lambda ()
                ;; Use spaces, not tabs.
                (setq indent-tabs-mode nil)
                ;; Keep M-TAB for `completion-at-point'
                (define-key flyspell-mode-map "\M-\t" nil)
                ;; Pretty-print eval'd expressions.
                (define-key emacs-lisp-mode-map
                            "\C-x\C-e" 'pp-eval-last-sexp)
                ;; Recompile if .elc exists.
                (add-hook (make-local-variable 'after-save-hook)
                          (lambda ()
                            (byte-force-recompile default-directory)))
                (define-key emacs-lisp-mode-map
                            "\r" 'reindent-then-newline-and-indent)))
    (add-hook 'emacs-lisp-mode-hook 'eldoc-mode)
    (add-hook 'emacs-lisp-mode-hook 'flyspell-prog-mode) ;; Requires Ispell

Use ‘byte-compile-file’ instead of ‘byte-force-recompile’ above to compile regardless if the file is already compiled.

Related EmacsLisp major and minor modes:


CategoryCode CategoryModes CategoryProgramming