Skip to main content
graphwiz.aigraphwiz.ai
← Back to Cheatsheets

Emacs Cheatsheet

DevOps
emacstext-editorterminallisporg-modesysadmin

Emacs Cheatsheet

GNU Emacs is a Lisp-based, infinitely extensible editor. It's not just a text editor — it's a complete computing environment. Key convention: `C-` = Ctrl, `M-` = Alt (Meta).


Emacs

GNU Emacs is a Lisp-based, infinitely extensible editor. It's not just a text editor — it's a complete computing environment. Key convention: C- = Ctrl, M- = Alt (Meta).

Basic Navigation

KeyActionMnemonic
C-fForward one characterForward
C-bBack one characterBack
C-nNext lineNext
C-pPrevious linePrevious
C-aBeginning of lineBeginning of alphabet
C-eEnd of lineEnd
M-fForward one wordMeta Forward
M-bBack one wordMeta Back
M-aBeginning of sentenceMeta Beginning
M-eEnd of sentenceMeta End
C-vPage downView
M-vPage upMeta View
C-lRe-center cursor on screenLine
M-<Beginning of bufferMeta Less-than
M->End of bufferMeta Greater-than
C-gCancel/quit current commandGo away

File & Buffer Management

# Files
C-x C-f       # find (open) file
C-x C-s       # save file
C-x C-w       # write as (save as)
C-x C-c       # exit Emacs
C-x k         # kill (close) current buffer
C-x C-j       # open Dired (file manager)

# Buffers
C-x b         # switch buffer (with auto-complete)
C-x C-b       # list all buffers
C-x <left>    # previous buffer
C-x <right>   # next buffer
C-x k         # kill buffer

Editing

KeyAction
BackspaceDelete previous character
C-dDelete next character
M-BackspaceDelete previous word
M-dDelete next word
C-kKill to end of line (cut)
M-kKill to end of sentence
C-wKill region (cut selection)
M-wCopy region
C-yYank (paste)
M-yCycle through kill ring after yank
C-/ or C-x uUndo
C-Shift-/ or C-x C-Shift-uRedo (Emacs 28+)
C-SpaceSet mark (begin selection)
C-x hSelect entire buffer
C-tTranspose two characters
M-tTranspose two words
C-x C-tTranspose two lines
M-cCapitalize word
M-uUppercase word
M-lLowercase word
M-qFill (reflow) paragraph

Search & Replace

# Incremental search
C-s           # search forward (incremental)
C-r           # search backward (incremental)
# While searching:
#   C-s again = jump to next match
#   C-r again = jump to previous match
#   C-g = cancel search

# Non-incremental search
M-C-s         # search forward with regex
M-C-r         # search backward with regex

# Replace
M-%           # query-replace (prompted replace)
#   y = replace, n = skip, ! = replace all, q = quit

# Replace with regex
C-M-%         # query-replace-regexp

Kill & Yank Ring

# The kill ring is Emacs's multi-item clipboard
C-k           # kill (cut) line from cursor to end
C-k C-k       # kill entire line (including newline)
C-w           # kill region (cut selection)
M-w           # copy region (without killing)
C-y           # yank (paste most recent kill)
M-y           # cycle kill ring — press repeatedly after C-y

# Example: paste something from 3 cuts ago
C-y M-y M-y M-y

Window Management

# Split windows
C-x 2         # split horizontally
C-x 3         # split vertically
C-x 1         # close all other windows
C-x 0         # close current window

# Navigate between windows
C-x o         # switch to next window

# Resize windows
C-x ^         # grow window height
C-x }         # grow window width
C-x {         # shrink window width
C-x +         # balance all windows

Org-Mode Basics

Org-Mode is Emacs's killer app — an outline-based document and task management system built in.

# Open an Org file
C-x C-f todo.org

# Outline navigation
Tab           # cycle visibility (fold/unfold subtree)
S-Tab         # cycle global visibility
C-c C-n       # next heading
C-c C-p       # previous heading
C-c C-f       # forward heading (same level)
C-c C-b       # backward heading (same level)
C-c C-u       # go to parent heading

# Structure editing
M-Enter       # new heading/item below
M-S-Enter     # new heading (same level as parent)
M-Left/Right  # promote/demote subtree
M-S-Left/Right # promote/demote subtree with children
M-Up/Down     # move subtree up/down

# Todo lists
C-c C-t       # cycle todo state (TODO → DONE → TODO)
S-Right/Left  # cycle through todo keywords

# Tables (built-in spreadsheet)
C-c |         # create or convert to table
Tab           # move to next cell / re-align
S-Tab         # move to previous cell
M-Left/Right  # move column
M-Up/Down     # move row
C-c -         # insert horizontal line
C-c Enter     # insert row below
C-c C-c       # recalculate table formulas

# Links
C-c C-l       # insert/edit link
C-c C-o       # open link at point

# Export
C-c C-e       # export dispatcher
#   h h = export to HTML
#   l p = export to PDF (via LaTeX)
#   l o = export to ODT
#   m m = export to Markdown

Practical Org-Mode Example

# Create a task list
* Project Alpha
** TODO Research competitor features
   SCHEDULED: <2026-04-25>
** TODO Design API schema
   SCHEDULED: <2026-04-28>
** DONE Write initial documentation
   CLOSED: [2026-04-18 Sat]

# A simple table with formula
| Item     | Qty | Price | Total |
|----------+-----+-------+-------|
| Widget   |   3 |    10 |    30 |
| Gadget   |   2 |    25 |    50 |
| Doohickey|   1 |    15 |    15 |
|----------+-----+-------+-------|
| Total    |   6 |       |    95 |
#+TBLFM: @5$4=vsum(@2$4..@4$4)

.emacs / emacs.d Init

# ~/.emacs.d/init.el — modern Emacs configuration

;; Package management
(require 'package)
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
                         ("gnu"   . "https://elpa.gnu.org/packages/")))
(package-initialize)
(unless package-archive-contents
  (package-refresh-contents))

;; Use-package for clean config (built-in since Emacs 29)
(setq use-package-always-ensure t)

;; UI
(tool-bar-mode -1)          ; hide toolbar
(menu-bar-mode -1)          ; hide menubar
(scroll-bar-mode -1)        ; hide scrollbar
(column-number-mode 1)      ; show column number
(global-display-line-numbers-mode 1)  ; show line numbers
(show-paren-mode 1)         ; highlight matching parens
(electric-pair-mode 1)      ; auto-close brackets

;; Theme
(use-package doom-themes
  :config
  (setq doom-themes-enable-bold t
        doom-themes-enable-italic t)
  (load-theme 'doom-one t))

;; Editing
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq-default fill-column 80)
(global-auto-revert-mode t)  ; auto-reload changed files
(setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
(setq auto-save-file-name-transforms '((".*" "~/.emacs.d/auto-save/" t)))

;; Key bindings
(global-set-key (kbd "C-<tab>") 'other-window)
(global-set-key (kbd "M-/") 'company-complete)
(global-set-key (kbd "C-c g") 'magit-status)
(global-set-key (kbd "C-x k") 'kill-current-buffer)

Common Packages

Magit (Git Interface)

# Install
M-x package-install RET magit RET

# Usage
C-x g         # open Magit status buffer
# In Magit status:
#   s = stage file
#   u = unstage file
#   c c = commit (opens commit message buffer)
#   C-c C-c = finish commit
#   P p = push
#   F p = pull
#   l l = log
#   d d = diff
#   z s = stash
#   b b = checkout branch
#   Tab = expand/collapse section
#   q = quit Magit

Company (Auto-Completion)

# Install
M-x package-install RET company RET

# Configuration (add to init.el)
(use-package company
  :hook (prog-mode . company-mode)
  :config
  (setq company-minimum-prefix-length 1
        company-idle-delay 0.0)
  (global-company-mode 1))

# Usage (in prog-mode, completions appear automatically):
M-/           # trigger completion manually
C-n / C-p     # navigate completion candidates
Tab           # accept completion

Flycheck (Syntax Checking)

# Install
M-x package-install RET flycheck RET

# Configuration (add to init.el)
(use-package flycheck
  :init (global-flycheck-mode))

# Usage:
# Flycheck runs automatically in supported modes
C-c ! l       # list errors
C-c ! n       # jump to next error
C-c ! p       # jump to previous error
C-c ! v       # verify Flycheck setup
C-c ! s       # set checker

Key Chord Reference

Essential Survival Keys

KeyAction
C-gCancel any command
C-x C-cQuit Emacs
C-x u or C-/Undo
C-x C-fOpen file
C-x C-sSave file
C-x C-bList buffers
C-x bSwitch buffer

Movement

KeyAction
C-f / C-bChar forward / back
C-n / C-pLine down / up
C-a / C-eLine start / end
M-f / M-bWord forward / back
M-< / M->Buffer start / end
C-v / M-vPage down / up

Killing & Yanking

KeyAction
C-kKill line (cut to end)
C-wKill region (cut selection)
M-wCopy region
C-yYank (paste)
M-yCycle kill ring

Window & Buffer

KeyAction
C-x 2Split horizontal
C-x 3Split vertical
C-x 1Single window
C-x oOther window
C-x bSwitch buffer
C-x kKill buffer

Search & Replace

KeyAction
C-s / C-rSearch forward / backward
M-%Query replace
C-M-%Query replace regex

Help System

C-h f         # describe function
C-h v         # describe variable
C-h k         # describe key binding
C-h m         # describe current mode's bindings
C-h a         # apropos (search all commands by keyword)
C-h t         # built-in Emacs tutorial
C-h i         # Info manual browser


Tips

Install Packages from MELPA

```bash

Add MELPA to init.el (if not already present)

(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)

Then in Emacs:

M-x package-refresh-contents RET M-x package-install RET RET ```


Next Steps