Princeton University
COS 217:  Introduction to Programming System

Precept 3:  Emacs

Purpose

Help you learn the fundamentals of the Emacs text editor

Reading

Loukides and Oram, Chapter 3

See also: Cameron, Rosenblatt, and Raymond.  Learning GNU Emacs.  O'Reilly and Associates.  Sebastopol, CA.  1996.

Overview

History

Mid 1970s, Richard Stallman

Originally a set of "Editing macros" for an editor that is now extinct

Very popular

Free -- part of GNU tool set

Very customizable (via LISP)

Integrated with other GNU software

bash history mechanism (described in a previous precept)

gcc compiler (described in this precept)

gdb debugger (described in a subsequent precept)

e-mail, news, file browser

Modal

Text mode, C mode, assembly language mode, directory edit (dired) mode, e-mail mode, etc.

Keyed by filename extension

We'll study C mode [and dired mode] today

You'll use assembly language mode in middle of course

We have two Emacs editors, invoked via commands emacs and xemacs

Differences are minimal

Discuss which to use (this semester!)

"Emacs" is the generic term for both

Setup

Show directory mystring, containing solution to Assignment 1

   testmystring
      mystring.h
      mystring2.h
      mystring.c
      mystring2.c
      testmystring.c
      testmystring2c

Fundamentals

To launch Emacs to create/edit a file:  emacs testmystring.c

To move the point: Arrow keys will probably work

To insert characters: Type them

To delete characters: BSP or DEL

To save the file: C-x C-s

To exit Emacs: C-x C-c

See The Emacs Editor summary sheet

The .emacs File

Interpreted at startup

(keyboard-translate ?\C-h ?\C-?)

Without:  BACKSPACE key invokes help system!

(setq c-default-style "style")

Sets C-mode indentation style

Calling Functions

Syntax: ESC x function

emacs testmystring.c
ESC x forward-char

There must be a better way!

C Mode Key Bindings

Commonly used functions are bound to keystrokes -- prefixed by C (control key) or ESC

C-f

Many keystrokes are bound by default

You can bind your own

(global-set-key keystrokes 'function)

Bindings are local to a mode

Moving the Point

Arrow keys will probably work

Home, End, PageUp, and PageDn keys may work

Emacs veterans prefer to use "normal" keys -- faster, more reliable across terminal types

Inserting and Deleting

Can insert and delete characters, as we've seen

Can insert and delete words

Can insert and delete lines

C-k
C-y

Can set a mark.  Mark and point denote a region

C-SP (set-mark-command)
(move point)
C-x C-x (exchange-point-and-mark)
C-w (kill-region) alias "cut"
C-y (yank)

Note:  ESC w (kill-ring-save) alias "copy"

TAB key does not insert tab character -- it indents line according to specified style

TAB

ESC C-\ indents region according to specified style -- when it works!

(mess up indentation)
ESC < (beginning-of-buffer)
C-SP (set-mark-command)
ESC > (end-of-buffer)
ESC C-\ (indent-region)

Suggestion: Experiment with predefined indentation styles

Searching and Replacing

Can search incrementally

C-S mystrn...

Can reverse search incrementally

Can query-replace

ESC % my your

Common options within query-replace:  y, n, !, q

Reading, Writing, and Exiting

To "find" a file means to load it into memory

Can "find" a directory, and then choose a file within it -- when it works!

Managing Windows and Buffers

Buffer: a named region of memory, usually an in-memory version of a file

Window: displays the contents of a buffer

C-x 2 (split-window-vertically)
C-x o (other-window)
C-x C-f mystring.h (find-file)
C-x C-f mystring.c (now 3 buffers, 2 of which are displayed in windows)
C-x C-b (list-buffers)
c-x b (switch-to-buffer)
C-x 1 (delete-other-windows)

Compiling and Debugging

Can preprocess, compile, assemble, and link within Emacs

(Can also debug within Emacs -- described soon)

Intentionally introduce some compiletime errors into testmystring.c

ESC x compile
gcc -o testmystring testmystring.c mystring.c
C-x ` (i.e. backquote)

Using Tags

etags command

Creates a file named TAGS

Contains index that relates function definitions to source file locations

Emacs can understand TAGS file

^z
etags -t mystring.h mystring.c testmystring.c (-t means include typedefs in the DB)
fg
(in testmystring.c, scroll to a function call)
ESC . (find-tag)

Can use tags to do search and replace operations across multiple files; see textbook

Getting Help

Emacs contains a help system

Show all bound keystrokes

Given keystrokes, print function name

Given function name, get description

C-x ? (help-command)

Miscellaneous Commands

To undo a previously issued command: C-x u (advertised-undo)

To abort a multi-keystroke command: C-g (keyboard-quit)

Copyright © 2002 by Robert M. Dondero, Jr.