Emacs Lisp
Emacs Lisp Q&A (Beginner to Advanced)
A comprehensive question-and-answer guide for Emacs Lisp.
Designed for:
- Learning Emacs Lisp from scratch
- Understanding Emacs internals
- Building packages and extensions
- Becoming an advanced Emacs developer
Study progressively from fundamentals to advanced package development.
Beginner
Q: What is Emacs Lisp (Elisp)?
Emacs Lisp is the Lisp dialect used by GNU Emacs.
Almost all of Emacs itself is written in Elisp.
It allows you to:
- Configure Emacs
- Extend editor functionality
- Create packages
- Automate repetitive tasks
Q: Why learn Elisp?
Learning Elisp allows you to:
- Understand how Emacs works internally
- Build custom commands
- Create major modes and minor modes
- Automate workflows
- Build complete editor extensions
Q: What are S-expressions?
Everything in Lisp is an expression.
Types:
- Atoms
- Lists
42 "hello" (+ 1 2) (message "Hello")
Q: How does evaluation work?
Expressions are evaluated immediately.
(+ 1 2) ;; => 3
Quoted expressions are treated as data.
'(+ 1 2) ;; => (+ 1 2)
Q: What is quoting?
Quote prevents evaluation.
(setq x 10) x ;; => 10 'x ;; => x
Q: What are symbols?
Symbols store:
- value
- function definition
- property list
(symbolp 'car)
Q: How do variables work?
Global:
(setq x 10)
Local:
(let ((x 5))
(+ x 1))
Constants:
(defconst pi 3.14)
Declared variables:
(defvar debug-mode nil)
Q: How do functions work?
(defun square (x) (* x x))
Call:
(square 5)
Q: What is an interactive function?
Interactive functions can be executed with M-x.
(defun hello () (interactive) (message "Hello"))
Q: How does interactive input work?
Prompt user input.
(defun greet (name) (interactive "sName: ") (message "Hello %s" name))
Common specifiers:
- s → string
- n → number
- f → file
- b → buffer
- r → region
Q: How do conditionals work?
(if (> 5 2) "yes" "no")
(cond ((= x 1) "one") ((= x 2) "two") (t "other"))
Q: What are nil and t?
nil
- false
- empty list
t
- true
Everything except nil is truthy.
Q: How do lists work?
(setq nums '(1 2 3))
(car nums)
(cdr nums)
(cons 0 nums)
(length nums)
Q: How do loops work?
(dolist (x '(1 2 3))
(print x))
(dotimes (i 5)
(print i))
(while condition
...)
Intermediate
Q: What are buffers?
Buffers are text containers.
Examples:
- files
- temporary buffers
- internal buffers
(current-buffer) (buffer-name)
Q: How does with-current-buffer work?
Execute code inside another buffer.
(with-current-buffer "*scratch*" (insert "Hello"))
Useful for:
- temporary buffers
- package development
- asynchronous processes
Q: What is save-excursion?
Preserves point and cursor location.
(save-excursion
(goto-char (point-min)))
Q: What are point, mark, and region?
- point → cursor position
- mark → secondary position
- region → selected text
(point) (mark) (region-beginning) (region-end)
Q: How do keymaps work?
(global-set-key
(kbd "C-c h")
#'hello)
Q: What are hooks?
Hooks execute functions when events occur.
(add-hook 'prog-mode-hook #'display-line-numbers-mode)
Q: What is defcustom?
User configurable variables.
(defcustom my-option t "Option." :type 'boolean)
Q: What is buffer-local state?
(setq-local fill-column 100)
Each buffer can hold different values.
Q: How does error handling work?
(condition-case err (/ 1 0) (arith-error (message "error")))
Q: How do files work?
Read:
(insert-file-contents "file.txt")
Write:
(with-temp-file "out.txt" (insert "hello"))
Q: What are alists, plists, and hash tables?
Alist:
((name . "Bob"))
Plist:
(:name "Bob")
Hash table:
(make-hash-table)
Advanced
Q: What are lambda functions?
(lambda (x)
(* x x))
Anonymous functions.
Q: What are closures?
(let ((x 10)) (lambda () x))
Closures capture variables.
Q: What is lexical binding?
Enable modern lexical scope.
;;; -*- lexical-binding: t; -*-
Required for closures.
Q: What are macros?
Macros generate code.
(defmacro my-when (cond &rest body) `(if ,cond (progn ,@body)))
Q: How do macros differ from functions?
Functions evaluate arguments.
Macros receive raw code.
(macroexpand-1 ...)
Q: What is backquote syntax?
`(...) ,x ,@list
Q: How do major modes work?
(define-derived-mode my-mode text-mode "MyMode")
Q: How do minor modes work?
(define-minor-mode my-mode "Toggle mode")
Q: What are syntax tables?
Syntax tables define parsing behavior.
They control:
- comments
- strings
- symbols
- words
(with-syntax-table ...)
Q: How do regular expressions work?
Search:
(re-search-forward "[0-9]+")
Replace:
(replace-match "number")
Used heavily in text manipulation.
Q: What are windows and frames?
Frame:
Entire Emacs window.
Window:
Buffer pane.
(selected-frame) (selected-window)
Q: What are markers?
(copy-marker (point))
Markers follow buffer changes.
Expert
Q: What are overlays?
Temporary visual decorations.
(make-overlay ...)
Q: What are text properties?
Metadata attached directly to text.
(propertize
"Warning"
'face 'warning)
Q: Hooks vs Advice?
Hooks:
(add-hook ...)
Advice:
(advice-add ...)
Prefer hooks when possible.
Q: What is advice-add?
Modify existing function behavior.
(advice-add
'find-file
:before
#'my-function)
Q: How do asynchronous processes work?
(make-process ...)
Non-blocking execution.
Q: How do process sentinels work?
(make-process
:sentinel ...)
Sentinel runs when process state changes.
Q: How do timers work?
(run-at-time ...) (run-with-idle-timer ...)
Q: How do shell commands work?
(call-process ...) (shell-command ...)
Prefer call-process for security.
Q: How do HTTP requests work?
(url-retrieve ...)
Built-in HTTP client.
Q: How do JSON APIs work?
(json-parse-string ...)
Q: How does testing work?
ERT testing framework.
(require 'ert) (ert-deftest my-test () ...)
Q: What is byte compilation?
(byte-compile-file "my.el")
Benefits:
- faster loading
- compiler warnings
Q: What is native compilation?
Produces:
- .eln files
- faster execution
Q: How do you debug Elisp?
Useful tools:
- debug-on-error
- edebug-defun
- debug-on-entry
- trace-function
- check-parens
- macroexpand-1
Example:
(setq debug-on-error t)
Q: Performance optimization?
Avoid:
- append in loops
- repeated regex scans
- unnecessary allocations
Prefer:
(push item list)
(nreverse list)
Q: Common beginner mistakes?
Common mistakes:
- forgetting interactive
- forgetting lexical-binding
- using setq instead of let
- using shell-command unsafely
- misunderstanding quote
- modifying buffers without save-excursion
Package Development
Q: Why use provide and require?
(provide 'mypackage) (require 'mypackage)
Q: How do package dependencies work?
;; Package-Requires: ;; ((emacs "29.1") ;; (dash "2.19"))
Q: How should package structure look?
;;; my.el --- package -*- lexical-binding:t -*- ;;; Commentary: ;;; Code: (provide 'my) ;;; my.el ends here
Q: How do you check package quality?
Tools:
- checkdoc
- package-lint
- byte compilation
Master Level
Q: What is EIEIO?
Object system for Emacs Lisp.
Q: What is cl-lib?
Common Lisp extensions.
Useful:
- cl-loop
- cl-defstruct
- cl-defmethod
- cl-reduce
Q: What are generators?
(require 'generator)
Functions:
- iter-defun
- iter-yield
Q: What is garbage collection tuning?
gc-cons-threshold
Controls GC frequency.
Q: How do you become advanced at Elisp?
Study source code from:
- GNU Emacs
- Org Mode
- Magit
- Projectile
- Vertico
- Consult
- Corfu
Build:
- major modes
- minor modes
- package managers
- syntax highlighters
- completion frameworks
- LSP integrations
- IDE extensions