Emacs Lisp

Emacs is largely written in Emacs Lisp. Use C-h f (describe-function), C-h v (describe-variable), M-x ielm (REPL), C-x C-e (evaluate the expression before point), and M-x edebug-defun (debugger).

Enable ;;; -*- lexical-binding: t -*- at the top of files for lexical scope (the default for new code). Emacs 28+ supports native compilation (--with-native-compilation).

;; ~/.emacs.d/init.el
(setq inhibit-startup-screen t)
(setq-default indent-tabs-mode nil)
(global-display-line-numbers-mode 1)

;; Interactive commands (callable with M-x and keybindings)
(defun my-insert-date ()
  "Insert today's date at point."
  (interactive)
  (insert (format-time-string "%Y-%m-%d")))
(global-set-key (kbd "C-c d") #'my-insert-date)

(defun my-count-words-region (start end)
  "Count words between START and END."
  (interactive "r")                       ; "r" passes the region bounds
  (message "%d words" (count-words start end)))

;; Hooks
(add-hook 'prog-mode-hook #'display-line-numbers-mode)
(add-hook 'before-save-hook #'delete-trailing-whitespace)

;; Buffers and text manipulation
(with-current-buffer (get-buffer-create "*demo*")
  (erase-buffer)
  (insert "Hello from Emacs Lisp\n")
  (goto-char (point-min))
  (while (re-search-forward "Hello" nil t)
    (replace-match "Hi")))

;; Advice: modify existing functions
(advice-add 'save-buffer :before (lambda (&rest _) (message "Saving...")))

;; Packages
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(use-package magit :ensure t :bind ("C-x g" . magit-status))

;;
…