Emacs Basics

This document introduces Emacs. Emacs is different than other text editors in many ways. The first differences you will notice are the key bindings and the interface. Buttons and menus in the graphical version help when you are lost, but learn the key bindings (keyboard shortcuts) to find the true source of power.

Contents

Start Emacs

If you are working with a graphical user interface, start Emacs by clicking its icon or by running emacs & at the command line. The & tells the command line to open Emacs in the background and immediately return control of the terminal to you.

If you using a command line interface with no option (or motivation) to start a GUI application, know your key-bindings well and start Emacs directly in the terminal with emacs -nw (no window).1 Do not use &.

Emacs Interface

Emacs labeled with names of interface features

Labeled Emacs interface:

  • Emacs is showing the contents of a buffer within a window (or frame). A buffer is a logical “text thing” that you are working on, typically the contents of a file.
  • The cursor is a rectangular block also called the point.
  • The mode line displays information about the buffer displayed in the current window.
  • The status flag shows whether the file has been changed (shows U:**-) or remains unchanged (shows U:---). The rest of the status flag indicates other information about file permissions, etc., that you are unlikely to encounter in this course.
  • Every buffer is edited in a mode. The most basic mode is Fundamental, which provides only the most basic Emacs editing features. There are modes for many programming languages that support special features like syntax highlighting, auto-indentation, and other extra language-specific commands.
  • There are many “special” buffers that do not correspond to loaded files. The one above is called *scratch*. This buffer runs in Lisp Interaction mode, which means that you can interactively type and evaluate expressions in the Emacs Lisp programming language. But we will not do that.

Basic Key Bindings

Emacs uses many key combinations involving the Control and Meta keys. Such key combinations are denoted C-x (Control-x) or M-x (Meta-x). Your keyboard lacks a Meta key, so Meta means:

  • Alt on Windows or Linux keyboards
  • Option (or maybe Command ⌘) on Mac keyboards
  • If none of these work, then for M-x, type and release Esc then type x. (This is only if using Esc as Meta. The other Meta substitutes work as usual: hold while pressing the second key.)

Emacs might complain about Super if you get the wrong one. If you or Emacs gets confused about what you are trying to type, use C-g (perhaps repeatedly) to cancel your current command and start fresh.

A sequence of key combinations is written like C-a C-b M-x, which means do the three actions in sequence: Control and lower-case a, then Control and lower-case b, then Meta and lower-case x. Do not hold them all at once.

Emacs calls each shortcut or sequence a key binding. For CS 251 students: these key sequences are bound to commands (functions in Emacs Lisp) within the editor in the same sense as names are bound to values in programming languages.

Quit, cancel, open, save, save as:

  • C-x C-c: Quit Emacs
  • C-g: Cancel the current action (use when there’s part of a key sequence showing in the minibuffer and you want out)
  • C-x C-f: Open a file (whether or not it already exists)
  • C-x C-s: Save a file
  • C-x C-w: Write a file (probably more familiar to you as Save as…)

Cut, copy, paste, undo:

  • Highlight text with the mouse or by hitting C-space to set a mark and then moving the cursor to highlight a region.
  • C-x h: Select all
  • C-w: Cut a highlighted region
  • M-w: Copy a highlighted region
  • C-k: Cut (kill) from the cursor to the end of the line
  • C-y: Paste (yank)
  • C-/ (or C-_, .i.e., Control+Shift+underscore): Undo.

Manipulate buffers

  • C-x b: Switch to another buffer by entering its name
  • C-x C-b: See a list of all current buffers
  • C-x k: Close (kill) an open buffer.
  • C-x 2: Split the window into 2 buffers, one above the other.
  • C-x 3: Split the window into 2 buffers, one to the left and one to the right.
  • C-x o: Move the cursor to the next visible buffer.
  • C-x 0 / C-x 1: “Undo” splitting, hiding (but not closing) the buffer containing the cursor (C-x 0) or returning to show only the buffer containing the cursor (C-x 1).

Get help within Emacs

In addition to the help button/menu on the right…

  • C-h: Help. Hitting this will display a short message in the minibuffer: C-h (Type ? for further options).
  • C-h b: Key bindings. This lists all key bindings that are valid for the current mode. Note that key bindings change from mode to mode.
  • C-h a: Command apropos. After typing C-h a you can type a symbol and a window will appear that lists all symbols and functions that match that phrase.

Next Level

Edit

  • Cutting/killing text:
    • Consecutive kills (with no intervening key sequences) accumulate as one unit of killed text.
      • M-backspace: Kill backward from cursor to beginning-of-word.
      • M-d: Kill forward from cursor to end-of-word.
      • M-k: Kill forward from cursor to end of sentence/paragraph.
  • Pasting/Yanking from the kill ring:
    • Emacs has a “kill ring” that remembers everything you have killed or copied since opening Emacs. Typing M-y (any number of times) immediately after typing C-y lets you cycle through the kill ring to select which text to yank.
  • Infinitely recoverable undo:
    • Emacs remembers every edit you have made in a buffer since you opened the buffer. Continually typing C-/ lets you walk back through that history. Typing anything else between invocations of C-/ “starts fresh.” In fact, the history only grows. The undo is actually recorded as part of the history, meaning you can never completely lose any undone text or action (unlike most other applications). Try it out to understand it!

Move the point (cursor)

  • Click the mouse or …
  • C-a: Move cursor to beginning of line.
  • C-e: Move cursor to end of line.
  • M-a: Move cursor to beginning of sentence/paragraph.
  • M-e: Move cursor to end of sentence/paragraph.
  • → or C-f: Move cursor forward one character.
  • ← or C-b: Move cursor backward one character.
  • M-f: Move cursor forward one word.
  • M-b: Move cursor borward one word.
  • ↓ or C-n: Move cursor down to next line.
  • ↑ or C-b: Move cursor up to previous line.
  • C-l: Center the line containing in the window. Repeat to place at top, bottom, etc.
  • C-v: Move cursor down one page.
  • M-v: Move cursor up one page.
  • C-<: Move cursor to beginning of buffer.
  • C->: Move cursor to end of buffer.
  • M-g M-g: Go to line (enter line number)

Search, replace

  • C-s or C-r: Incremental search (as you type) forward or backward. While searching:
    • keep typing to extend search string
    • Enter to exit search and stay at cursor
    • C-g to cancel search and return to where cursor started
    • C-s/C-r to search for next or go opposite direction
    • without typing search string, C-s/C-r again to search for last searched sring.
  • M-%: Query replace (find and replace)
  • M-g M-g: Go to line (enter line number)

Define and apply keyboard macros

  • C-x (: Start recording key strokes.
  • C-x ): Stop recording key strokes and save recording as a replayable keyboard macro.
  • C-x e: Replay the keyboard macro. Type e after typing this once to play it again.

Burn through your late passes

  • M-x dunnet
  • M-x doctor
  • M-x gomoku
  • M-x pong
  • M-x snake
  • M-x tetris
  • M-x zone

… and Beyond

  1. If you forgot -nw and Emacs is not visible and the command prompt does not return, Emacs may have started on a GUI display where you are also logged in on the same computer (e.g., if you forgot to log out of a lab computer an now you are connected to it via ssh. Type Control-c to kill the hiding Emacs process, then run it again with emacs -nw