Notes on Emacs Lecture ---------------------- Meta Key -------- One of the most confusing aspects of emacs is the fact that many commands are "meta" commands. The original designers of emacs had keyboards that had a shift key, a control key and a meta key. So it was possible to form characters like a normal a, a capital a, a control a, a meta a, and even various other combinations like a control-meta-a. Some versions of emacs, including the Windows version, uses the alt key as a meta key. But most of us will need to simulate the meta key. Instead of holding down a meta key while you strike a character like a, you instead first strike the esc (escape) key and then strike the character you want. This is a simulation because instead of typing one character, you type two (escape followed by the character you want to turn into a meta character). The convention in emacs documentation is to refer to control characters with the prefix "C-" and to refer to meta characters with the prefix "M-". So if you see "C-h", that means control-h. If you see "Meta-x", that means meta-x (simulated by typing esc followed by x). Tutorial -------- If you want to learn basic emacs skills, go through the emacs tutorial. The easiest way to get into the tutorial is to type: C-h t This won't work for current and former CS227 students using the Harvill lab because they were asked to copy an emacs initialization file to their accounts that translates a C-h into a delete. But you can still get into the tutorial by typing the long name: M-x help-with-tutorial Basic Cursor Control -------------------- Below is a diagram that gives an overview of the primary commands used for cursor control in emacs. They are all relative to the cursor at the center of the diagram. You use different commands depending upon how far you want to move the cursor (e.g., forward one char, back one word, to beginning of file). M-< (file) M-v (screen) C-p (line) C-a (line) M-b (word) C-b (char) [cursor] C-f (char) M-f (word) C-e (line) C-n (line) C-v (screen) M-> (file) Arguments to Commands --------------------- Any emacs command can be prefixed by a numeric argument. You provide a numeric argument by typing C-u followed by the argument. For example, if you type: C-u 20 M-f You are asking emacs to move forward 20 words. You can type just the C-u as an argument, which defaults to the integer 4. You can also prefix simple characters. So if you type: C-u 70 - You will get a line of 70 dashes. Regions ------- For many emacs commands, you will need to specify a region of text to change. Emacs has what are called "point" and "mark" to keep track of the current region. Point is the name for the current cursor position. Mark is the name for the location most recently marked in the file. Many emacs commands automatically set the mark (e.g., C-<, which goes to the beginning of the file). You'll know that this has happened because the message "Mark Set" will appear in the lower-left part of the screen after the command is executed. You can set the mark manually by giving the command: M-x set-mark You can see the current region by giving the command: C-x C-x This asks emacs to exchange point and mark. So typing it once puts you to the other end of the region. Typing it again puts you back where you were. Moving/Copying Text ------------------- There are three basic commands for making a copy of text: C-k kill line C-w kill region M-w copy region Once you have killed or copied a region of text, you can type C-y to "yank" it back. Most typically, you would kill or copy a region of text, move the cursor to a different position or possibly even another file and then yank it back in the new position. Searching --------- You can search for a specific text in emacs by typing C-s to search forward or C-r to search backward. As you type characters, you specify the search in more detail. If you want to skip the current item, type another C-s to skip forward or C-r to skip backward. When you find yourself at the appropriate position, type the return key to end the search. Modes ----- Emacs has several major and minor modes. A file buffer will be in exactly one major mode at any time, but it can be in any number of minor modes. Emacs tries to set the major mode by looking at the file extension. When you open a file ending with the extension ".txt", it uses Text mode. When you open a file with the extension ".cc", it uses C++ mode. The mode is displayed in what is called the "mode line" at the bottom of the screen. The behavior of emacs changes with the mode. For example, in C++ mode, the tab key indents the current command using standard C++ indentation conventions. A useful command in C++ mode is to type M-q inside a comment block. It will reformat the comment block so that it fits within a margin 70 wide. Another useful command is M-x Indent-Region, which will indent an entire region of a program using the standard C++ formatting conventions. There are several useful commands available to you in the Text major mode. You can use C-q to "justify" a paragraph to fit within a margin of 70 characters. This command reformats the current paragraph the way a word processor like Microsoft Word would do. You can use M-s to center a line of text using a margin of 70 characters. If you want to change the default margin of 70, you can type C-u followed by the margin you want followed by C-x f (to set the "fill" column). Macros ------ One of the simplest and most powerful tools in emacs is the ability to define a keyboard macro. If you find yourself performing the same series of steps over and over, you can define a macro for those steps and then execute it as many times as you want. You begin a macro by typing: C-x ( When you type this, you will notice that you go into the minor mode called "Def" (defining a macro). You then give the sequence of commands you are interested in. Then you type: C-x ) to close the macro. To execute the macro, type: C-x e As with other commands, you can give a numeric argument to this command to execute it multiple times. If you give it an argument of -1, it will execute the macro until end-of-file is reached. Windows ------- Emacs allows you to have multiple windows open at a time. For example, if you are editing a C++ program, you can type C-x 2 to open a second window. Both windows will be for the same file. You can edit in either window. More typically, you would use one window to view definitions (e.g., function prototypes) while you edit in the other window. To switch from one window to the other, type C-o. To go back to one-window mode, type C-x 1. You can also use multiple windows to view one file while editing another. Simply type C-x f to "find" the other file when you get into the second window. You can have many windows open at a time, even if they aren't all visible. Give the command C-x b to see all of your current buffers. You can then select whatever buffer you are interested in and type a 1 by its name to make it the current buffer in one window. Some other Word Commands ------------------------ Below are a few emacs commands that I have found particularly helpful over the years: M-u uppercase the next word M-c capitalize the next word M-l lowercase the next word M-$ interactively spell check the current word Compiling and Debugging Within Emacs ------------------------------------ There are two useful commands for compiling programs and debugging them within emacs. They are: M-x compile M-x gdb These commands open a second window that allows you to see your program compiling or being debugged. The gdb command is particularly useful in that it displays context and uses an arrow to indicate the current line being executed when you set break points. Customizing Emacs ----------------- When emacs loads, it looks to see if you have a file called ".emacs" on your home directory. If so, it executes the commands in this file. You can use this to completely customize your emacs environment. Emacs commands are written as Lisp expressions. For example, here is the emacs configuration file set up for CS227 this semester: ;; inhibit startup message (setq inhibit-startup-message 1) ;;; Files ending in .h or .template should be edited in c++-mode (setq auto-mode-alist (cons '("\\.h$" . c++-mode) auto-mode-alist)) (setq auto-mode-alist (cons '("\\.template$" . c++-mode) auto-mode-alist)) ;;; Translate C-h to Delete (keyboard-translate ?\C-h ?\C-?) ; ... bind C-x c to compile (define-key global-map "\^xc" 'compile) ; ... bind C-x g to goto-line (define-key global-map "\^xg" 'goto-line) Cancelling/Undoing ------------------ Two very useful emacs commands are: C-g cancel current command M-x undo undo most recent command Miscellaneous Commands ---------------------- Below is a short list of other commands that I have found particularly helpful over the years. M-x replace-string replaces occurrences of one string with another M-x query-replace queries before replacing M-x how-many counts occurrences of a string M-x ispell-buffer interactively spell check a buffer M-x tabify turn spaces to tabs whenever possible in region M-x untabify replace all tabs with spaces in region M-x sort-lines sort region alphabetically Getting Emacs ------------- In addition to the linux version of emacs, you can now get a Windows-95 version of emacs at this site: http://www.cs.washington.edu/homes/voelker/ntemacs.html If you want to use it in conjunction with the gnu tools like the G++ compiler, go to this site: http://www.cygnus.com/misc/gnu-win32 Getting Help ------------ Emacs has extensive online documentation. You can enter the help system by typing: M-x info From there, you want to select the menu item "Emacs". You select a menu item by moving to it and hitting the return key. From there, you can read about a large number of emacs topics. Another handy command is the "apropos" command. Type: M-x apropos Then type in a bit of text. For example, if you ask the apropos command to show you information about "save" commands, you'll get a list of commands that include the word "save" in the name or description of the command. Most of these will be M-x commands (i.e., most will require the M-x prefix).
Stuart Reges
Last modified: Fri Jan 9 09:06:25 PST 2026