Emacs-for-Goodness-Sake

Chapter 3: Movement and Key Bindings

Your Fingers Will Learn to Dance

There’s a moment in every Emacs user’s journey when the key bindings stop feeling like alien hieroglyphics and start feeling like music. Your fingers develop muscle memory, and suddenly you’re navigating text at the speed of thought.

This chapter is about reaching that moment faster, and maybe having fewer hand cramps along the way.

The Home Row Philosophy

Emacs was designed when keyboards looked different. The keyboard had the Control key where Caps Lock is now. This is important because it explains why Emacs seems determined to give you repetitive strain injury.

First thing you should do: Swap Caps Lock and Control. Seriously. Do it now.

🚸 IDE Refugee Note: “But I use Caps Lock!” No, you don’t. Nobody does. And if you actually do, you can still access it—it’s just moved to where Control was. This one change will save your pinky finger from years of torture.

Basic Movement: Beyond the Arrow Keys

Yes, arrow keys work. But they’re so far away! Learn these and your hands never leave home row:

    C-p (previous line)
         ↑
C-b ← C-f → (back/forward)
         ↓
    C-n (next line)

Think of it as: back, forward, next, previous.

🤔 Why Though? These keys were chosen because they’re on the home row (well, close to it). Once you learn them, you can navigate without moving your hands. It’s like touch typing, but for navigation.

Word and Paragraph Movement

Characters are small. Words are better. Paragraphs are best:

Notice the pattern? Control for small movements, Meta for bigger movements.

Line Movement: Your Most-Used Commands

These become so natural you’ll try to use them everywhere else and get frustrated when they don’t work.

Screen Movement: When You Need to Jump

🎯 Pro Tip: C-l cycles through center → top → bottom. It’s incredibly useful for positioning code exactly where you want it on screen.

The Mighty Search: Your Navigation Superpower

Forget about carefully navigating to where you want to go. Just search for it:

While searching:

🚸 IDE Refugee Note: This isn’t like Ctrl+F in other editors. It’s incremental—it searches as you type. And when you’re done, just start typing to edit. No need to “close” the search.

The Kill Ring Dance: Cut, Copy, and Paste Evolved

Remember: Kill = Cut, Yank = Paste. Here’s the full dance:

But here’s where it gets beautiful:

Everything you kill goes on the kill ring. It’s like having clipboard history built into your muscle memory.

Undo/Redo: The Tree of Possibilities

But here’s the advanced move: undo-tree (install it in Chapter 5). It turns your linear undo into a branching tree of possibilities. You can literally see alternate timelines of your document.

Multiple Cursors: When One Isn’t Enough

This isn’t built-in, but it’s so useful we’ll cheat and mention it here:

(We’ll install this package in Chapter 5, but I wanted you to know it exists.)

Registers: Your Bookmark System

Registers are like variables for your editor. Store text, positions, or window configurations:

🎯 Pro Tip: Use registers for boilerplate text. Store your commonly-used code snippets and insert them instantly.

Window Navigation: Dancing Through Panes

Remember, windows are the panes within your Emacs frame:

Advanced window-fu:

The Prefix Key Pattern

Emacs uses prefix keys to group related commands:

This is why Emacs can have thousands of commands without running out of keys.

Rectangle Editing: The Hidden Superpower

This will blow your mind. You can select and edit rectangular regions:

  1. Set mark at corner: C-SPC
  2. Move to opposite corner
  3. C-x r k - Kill rectangle
  4. C-x r y - Yank rectangle
  5. C-x r t - Replace rectangle with text
  6. C-x r N - Number lines in rectangle

🎮 Try This:

  1. Create a list of items
  2. Select a rectangle at the beginning of all lines
  3. C-x r N to number them
  4. Watch minds blow (including your own)

The Modal Alternative: Evil Mode

If you’re coming from Vim, there’s Evil mode. It’s Vim emulation so good that Vim users switch to Emacs just to use it. We’ll cover this in the appendix, but know it exists.

Keyboard Macros: Your Personal Robot

This deserves its own chapter (and it’ll get one), but here’s a taste:

  1. F3 or C-x ( - Start recording macro
  2. Do stuff
  3. F4 or C-x ) - Stop recording
  4. F4 or C-x e - Execute macro
  5. C-u 100 C-x e - Execute macro 100 times

Example: Add semicolons to the end of 100 lines:

  1. F3 - Start recording
  2. C-e ; - Go to end of line, add semicolon
  3. C-n C-a - Next line, beginning
  4. F4 - Stop recording
  5. C-u 99 F4 - Do it 99 more times

God Mode and Other Insanity

There are packages that completely reimagine Emacs keybindings:

We’ll explore these in the packages chapter, but they show how flexible Emacs really is.

Your Personal Keybinding Manifesto

Here’s how to create your own keybindings:

;; Simple binding
(global-set-key (kbd "C-c t") 'toggle-truncate-lines)

;; Unbind something annoying
(global-unset-key (kbd "C-z"))  ; Suspend frame

;; Bind function key
(global-set-key (kbd "<f5>") 'revert-buffer)

;; Create a prefix key
(define-prefix-command 'my-prefix-map)
(global-set-key (kbd "C-c m") 'my-prefix-map)
(define-key my-prefix-map (kbd "t") 'toggle-truncate-lines)

🚸 IDE Refugee Note: Yes, you can make Emacs use CUA keys (Ctrl+C for copy, etc.). Add (cua-mode t) to your config. But try the Emacs way first—it’s actually more powerful once you get used to it.

The Learning Curve Strategy

Don’t try to learn everything at once. Here’s a progression:

Week 1: Basic movement

Week 2: Killing and yanking

Week 3: Search and replace

Week 4: Windows and buffers

Month 2: Advanced movement

Exercises to Build Muscle Memory

🎮 Daily Drills:

  1. The No-Arrow Challenge: Disable arrow keys for a day
    (global-unset-key (kbd "<left>"))
    (global-unset-key (kbd "<right>"))
    (global-unset-key (kbd "<up>"))
    (global-unset-key (kbd "<down>"))
    
  2. The Speed Run:
    • Open a file
    • Navigate to 5 specific words using search
    • Kill 3 lines, yank them elsewhere
    • Time yourself, try to beat it
  3. The Rectangle Puzzle:
    • Create a CSV file
    • Use rectangles to rearrange columns
    • Add line numbers to one column
  4. The Macro Marathon:
    • Find a repetitive task
    • Solve it with a macro
    • Feel like a wizard

The Ergonomics Section

Real talk: Emacs can hurt your hands if you’re not careful.

Prevention:

  1. Swap Caps Lock and Control (seriously, do it)
  2. Use sticky keys if needed (tap Control instead of holding)
  3. Learn to use both Control keys
  4. Take breaks
  5. Consider foot pedals for modifiers (I’m not joking)

Alternative configurations:

The Keybinding Cheat Sheet

Print this out and stick it on your monitor:

MOVEMENT           EDITING             SEARCH
C-f  →            C-k    kill line    C-s    forward
C-b  ←            C-w    kill region  C-r    backward
C-n  ↓            M-w    copy region  C-M-s  regexp
C-p  ↑            C-y    yank         M-%    replace
M-f  word →       M-y    yank pop
M-b  word ←       C-/    undo         FILES
C-a  line start                       C-x C-f  find
C-e  line end     WINDOWS             C-x C-s  save
M-<  buffer start C-x 2  split horiz  C-x C-w  save as
M->  buffer end   C-x 3  split vert   
                  C-x o  other        HELP
                  C-x 1  only         C-h k  key
                  C-x 0  close        C-h f  function

What’s Next?

Now that your fingers are learning to dance, Chapter 4 will show you how to make Emacs truly yours through configuration. We’ll start with simple tweaks and work our way up to a configuration that other Emacs users will envy.

But remember: keybindings are like vocabulary. The more you know, the more eloquently you can express your intentions to Emacs. And once you’re fluent, you’ll wonder how you ever lived without them.


“The keyboard is my piano, Emacs is my music, and text is my symphony.” —An Emacs user who learned all the keybindings


Chapter 3 Summary

Master these keys, and you’ll edit text faster than you can think about editing text.