This document introduces Emacs. Emacs is different than other text editors in many ways. The first differences you will notice are the keyboard shortcuts 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.

Emacs is installed in the CS 251 computing environments. It is also possible to install in most other environments.

Contents

Starting Emacs

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

If you are working with a command line interface with no option to start GUI application, start Emacs directly in the terminal with emacs. (Do not use the & here, as it will hide Emacs from you.) You will not be able to interact with it using the mouse, so learn those key bindings well! If the 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. Type Control-c to kill Emacs, then run it again with the no-window option: emacs -nw.

Emacs Interface

Emacs labeled with names of interface features, shown running in wx appliance

Labeled Emacs interface, shown running in a wx appliance.

  • 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 (lowercase)) or M-x (Meta-x). On keyboards that don’t have Meta (just about all keyboards today), use:

  • Alt on Windows/Linux keyboards
  • Option (or maybe the Command ⌘ key) on a Mac keyboard.
  • If none of these work, use Esc, but when trying to type M-x, for example, 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 presses 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 just as names are bound to values in Racket and SML.

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).

Getting 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

Editing

  • Cutting/killing text:
    • Consecutive cuts (with no intervening key sequences) accumulate as one unit of cut text.
      • M-backspace: Cut the the word preceding the cursor.
      • M-d: Cut the the word following the cursor.
      • M-k: Cut the the rest of sentence/paragraph following the cursor.
  • Yanking from the kill ring:
    • Emacs has a “kill ring” that remembers everything you have cut or copied since opening Emacs. Typing M-y (any number of types) immediately after typing C-y lets you cycle through that history after to select which of those things to paste.
  • 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 in between invocations of C-/ will let you “start fresh”. In fact, the history is append-only: it only grows and C-/ is actually recorded as part of the history. Meaning you can never completely lose anything you undid (unlike most other applications). Try it out to understand it!

Moving the 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)

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

All the rest…

Far more than a “customizable text editor.”