# Emacs Quick Reference

*Navigation, editing, buffers, search, org mode, packages*

> Source: GNU Emacs Manual (gnu.org/software/emacs) · MIT

## Navigation

### Cursor Movement

| Command | Description |
|---------|-------------|
| `C-f / C-b` | Forward / backward one character |
| `M-f / M-b` | Forward / backward one word |
| `C-n / C-p` | Next / previous line |
| `C-a / C-e` | Beginning / end of line |
| `M-a / M-e` | Beginning / end of sentence |
| `C-v / M-v` | Scroll down / up one page |
| `M-< / M->` | Beginning / end of buffer |
| `M-g g` | Go to line number |

### Screen Positioning

| Command | Description |
|---------|-------------|
| `C-l` | Recenter screen (cycle top/center/bottom) |
| `C-M-l` | Reposition window heuristically |

## Editing

### Kill & Yank (Cut & Paste)

| Command | Description |
|---------|-------------|
| `C-k` | Kill to end of line |
| `C-w` | Kill region (cut) |
| `M-w` | Copy region |
| `C-y` | Yank (paste) |
| `M-y` | Cycle through kill ring after yank |
| `C-d` | Delete character forward |
| `M-d` | Kill word forward |
| `M-DEL` | Kill word backward |

### Undo & Transpose

| Command | Description |
|---------|-------------|
| `C-/ or C-_` | Undo |
| `C-g C-/` | Redo (undo the undo) |
| `C-t` | Transpose characters |
| `M-t` | Transpose words |
| `C-x C-t` | Transpose lines |

### Selection

| Command | Description |
|---------|-------------|
| `C-SPC` | Set mark (start selection) |
| `C-x h` | Select entire buffer |
| `M-h` | Select paragraph |
| `C-x C-x` | Exchange point and mark |

## Buffers & Windows

### Buffers

| Command | Description |
|---------|-------------|
| `C-x b` | Switch to buffer |
| `C-x C-b` | List all buffers |
| `C-x k` | Kill (close) buffer |
| `C-x LEFT / RIGHT` | Previous / next buffer |

### Windows

| Command | Description |
|---------|-------------|
| `C-x 2` | Split window horizontally |
| `C-x 3` | Split window vertically |
| `C-x 0` | Close current window |
| `C-x 1` | Close all other windows |
| `C-x o` | Switch to other window |
| `C-x ^` | Grow window vertically |
| `C-x {` | Shrink window horizontally |

### Frames

| Command | Description |
|---------|-------------|
| `C-x 5 2` | Create new frame |
| `C-x 5 0` | Delete current frame |
| `C-x 5 o` | Switch to other frame |

## Search & Replace

### Incremental Search

| Command | Description |
|---------|-------------|
| `C-s` | Search forward (incremental) |
| `C-r` | Search backward (incremental) |
| `C-s C-s` | Repeat last search forward |
| `C-M-s` | Regex search forward |
| `C-M-r` | Regex search backward |
| `RET` | Exit search at current position |
| `C-g` | Cancel search, return to start |

### Replace

| Command | Description |
|---------|-------------|
| `M-%` | Query replace |
| `C-M-%` | Query replace regexp |
| `y` | Replace this match |
| `n` | Skip this match |
| `!` | Replace all remaining |
| `q` | Quit replacing |

## Files

### File Operations

| Command | Description |
|---------|-------------|
| `C-x C-f` | Find (open) file |
| `C-x C-s` | Save current buffer |
| `C-x C-w` | Save as (write to new file) |
| `C-x s` | Save all modified buffers |
| `C-x C-r` | Open file read-only |
| `C-x i` | Insert file contents at point |

### Dired (Directory Editor)

| Command | Description |
|---------|-------------|
| `C-x d` | Open directory in Dired |
| `d / x` | Flag for deletion / execute deletions |
| `R` | Rename / move file |
| `C` | Copy file |
| `+` | Create directory |
| `g` | Refresh listing |

## Modes

### Major Modes

| Command | Description |
|---------|-------------|
| `M-x fundamental-mode` | Plain text editing |
| `M-x text-mode` | Text with fill and spelling |
| `M-x prog-mode` | Base for programming modes |
| `M-x python-mode` | Python editing and indentation |
| `M-x shell-mode` | Interactive shell in buffer |

### Useful Minor Modes

| Command | Description |
|---------|-------------|
| `M-x display-line-numbers-mode` | Toggle line numbers |
| `M-x auto-fill-mode` | Auto-wrap at fill column |
| `M-x flyspell-mode` | On-the-fly spell checking |
| `M-x column-number-mode` | Show column in mode line |
| `M-x whitespace-mode` | Visualize whitespace |
| `M-x electric-pair-mode` | Auto-close brackets and quotes |

## Org Mode Basics

### Structure

| Command | Description |
|---------|-------------|
| `* / ** / ***` | Heading levels 1 / 2 / 3 |
| `TAB` | Cycle heading visibility |
| `S-TAB` | Cycle global visibility |
| `M-RET` | Insert heading at same level |
| `M-LEFT / M-RIGHT` | Promote / demote heading |
| `M-UP / M-DOWN` | Move subtree up / down |

### TODO & Checkboxes

| Command | Description |
|---------|-------------|
| `C-c C-t` | Cycle TODO state |
| `C-c C-d` | Set deadline |
| `C-c C-s` | Schedule item |
| `C-c C-c` | Toggle checkbox [ ] / [X] |
| `C-c a` | Open agenda view |

### Links & Export

| Command | Description |
|---------|-------------|
| `C-c C-l` | Insert / edit link |
| `C-c C-o` | Open link at point |
| `C-c C-e` | Export dispatcher (HTML, PDF, etc.) |

## Package Management

### package.el

| Command | Description |
|---------|-------------|
| `M-x list-packages` | Browse available packages |
| `i / d / x` | Mark install / delete / execute |
| `U` | Mark all upgradable |
| `M-x package-refresh-contents` | Refresh package list |

### use-package (init.el)

```
(use-package magit
  :ensure t
  :bind ("C-x g" . magit-status))
(use-package company
  :ensure t
  :hook (prog-mode . company-mode))
```

## Configuration

### Key Config Files

| Command | Description |
|---------|-------------|
| `~/.emacs.d/init.el` | Main configuration file |
| `~/.emacs.d/early-init.el` | Early init (before GUI, Emacs 27+) |
| `~/.emacs.d/custom.el` | Customize-written settings |

### Common Settings

```
(setq inhibit-startup-screen t)
(tool-bar-mode -1)
(set-face-attribute 'default nil :height 120)
(global-display-line-numbers-mode 1)
(setq make-backup-files nil)
```

### Help System

| Command | Description |
|---------|-------------|
| `C-h k` | Describe key binding |
| `C-h f` | Describe function |
| `C-h v` | Describe variable |
| `C-h m` | Describe current modes |
| `C-h t` | Open Emacs tutorial |
| `C-h i` | Open Info documentation |

## Common Patterns

### Rectangle Editing

| Command | Description |
|---------|-------------|
| `C-x SPC` | Start rectangle mark mode |
| `C-x r k` | Kill rectangle |
| `C-x r y` | Yank rectangle |
| `C-x r t` | Replace rectangle with string |
| `C-x r o` | Insert blank rectangle |

### Macros

| Command | Description |
|---------|-------------|
| `C-x (` | Start recording macro |
| `C-x )` | Stop recording macro |
| `C-x e` | Execute last macro |
| `C-u 10 C-x e` | Execute macro 10 times |
| `M-x name-last-kbd-macro` | Name macro for reuse |

### Shell & Eval

| Command | Description |
|---------|-------------|
| `M-x shell` | Open shell in buffer |
| `M-x eshell` | Emacs Lisp shell |
| `M-!` | Run shell command |
| `M-x eval-buffer` | Evaluate entire buffer as Elisp |
| `C-x C-e` | Evaluate Elisp expression before point |
