Emacs: Byte Compile Elisp Files

By Xah Lee. Date: . Last updated: .

Emacs lisp files can be byte compiled.

Byte compiled elisp file has “.elc” suffix (aka extension). Normal elisp file has “.el” suffix.

Advantage of Byte Compiled Elisp File

Byte compiled elisp files will load faster, and also run faster. (by a simple test of a loop, it seems to run about 4 times faster.)

Another advantage is that byte compiling will often tell you errors or warning in your elisp code that you normally wouldn't know.

elisp byte compile warning 2016-01-29
elisp byte compile warning

When you use ELPA to install packages, they are automatically byte compiled. [see Emacs: How to Install Packages Using ELPA, MELPA]

As of today (), for init files and light-weight packages, byte compile doesn't make any noticeable speed difference. In general, you should byte-compile lisp packages. (example of heavy weight packages that need or must be byte-compiled: js2-mode, nxml-mode, auto-complete-mode.)

Loading Byte Compiled File

In your init file, when you use load, if you want emacs to load the byte compiled file if it exists, you should not include the “.el” suffix. For example, do it like this

;; load elisp file, use byte compiled version (.elc) if exist
(load "my_emacs_keybinding") ; no file name extension here

[see Elisp: require, load, load-file, autoload, feature]

How to Byte Compile

There are several ways to byte compile elisp files. The simplest and most useful are:

Batch Byte Compile

Alt+x byte-recompile-directory to batch byte compile all elisp files in current dir and sub-directory, if a “.elc” for the file exists, and has a file timestamp older than the “.el” file.

Evaluate (byte-recompile-directory directory_path 0) to recompile every “.el” file. (regardless whether “.elc” exists.), but still compare timestamp.

Evaluate (byte-recompile-directory directory_path 0 t) to recompile every “.el” file. (regardless whether “.elc” exists and regardless of timestamp.)

Evaluate (byte-recompile-directory directory_path nil t) to byte compile all “.el” file that has a existing “.elc” file. (regardless of timestamp.)

[see How to Evaluate Emacs Lisp Code]

Recompile When Upgrade

When you upgrade to a new emcas version, or upgrade packages, or bring over your byte compiled elisp directory from one machine to another, you should recompile your elisp files, because often, emacs has some incompatible elisp changes, and big packages may fail without recompile.


(info "(elisp) Byte Compilation")

thanks to Adolfo Benedetti.

Emacs Customization

  1. How to Set Emacs's User Interface
  2. Emacs: How to Install Packages Using ELPA, MELPA
  3. How to Install Emacs Package Manually
  4. Emacs: How to Define Keys
  5. Emacs: M-x customize Tutorial
  6. Emacs: Set File to Open in a Major Mode
  7. Emacs: Organize Init File
  8. Emacs: Byte Compile Elisp Files
  9. Emacs: What's Hook?
  10. Emacs: Set Environment Variables within Emacs
  11. Elisp: Determine OS, Emacs Version, Machine Host Name
  12. Elisp: Check If a {function, variable, feature} is Defined/Loaded
  13. Emacs: Set Default Window (frame) Size
  14. Emacs: Font Setup
  15. Emacs: Set Color Theme
  16. Emacs: Save Cursor Position
  17. Emacs: Turn Off Auto Backup; Set Backups into a Directory; How to Delete Backup Files
  18. Emacs: Stop Cursor Going into Minibuffer Prompt
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon