Notes on Emacs Lecture handout #4 ---------------------- This document describes how to use the simple terminal interface to emacs that you would get through an ssh connection to attu. It is common to use some kind of X-server like Reflection X so that you can have a more classic interface to emacs where you can use your mouse and the pull-down menus. This document explains the more primitive environment you get with a simple ssh session. 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). Basics: Starting, Saving, Exiting --------------------------------- You typically start emacs by invoking it at the command line: emacs You can then open a file by "visiting" it with the command C-x C-v. Alternatively, you can specify a file name when you invoke emacs: emacs hw1.sml You can then edit the file and save your changes by typing C-x C-s. Once you have saved the file, you can type C-x C-c to exit. If you have any unsaved buffers, emacs will ask whether or not you want to save them before you exit. 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 If you have any trouble loading the tutorial this way, you can instead give the following long command: 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: if i spell badly 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 ".java", it uses Java 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 Java mode, the tab key indents the current command using standard Java indentation conventions. A useful command in Java 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 Java formatting conventions. There are several useful commands available to you in the Text major mode. You can use M-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-x 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 C-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-t transpose two words M-$ interactively spell check the current word 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 I'm using in CSE341: ;; inhibit startup message (setq inhibit-startup-message 1) ; SML/NJ stuff (add-to-list 'load-path "/cse/courses/cse341/common/sml-mode") (load "sml-mode-startup") ; Global coloring (global-font-lock-mode) Canceling/Undoing ----------------- Two very useful emacs commands are: C-g cancel current command M-x undo undo most recent command You can also get undo by typing C-_. Miscellaneous Commands ---------------------- Below is a short list of other useful commands: 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 M-x doctor run eliza (ai program) M-x goto-line Go to a specific line number Backspace Key ------------- Often people find that when they type the backspace key, they get a weird result. You can usually configure your terminal software to fix this. For example, if you're using ssh on Windows, go to Edit, Settings, Keyboard, and check the box for "Backspace sends Delete". If all else fails, you can add the following to your .emacs file to have emacs do the translation for you: ;;; Translate C-h to Delete (keyboard-translate ?\C-h ?\C-?) 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. You type "q" to quit from the info system. You can type "?" in info mode to see a complete list of available commands. 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). AutoSave Features ----------------- Emacs does an autosave for you fairly frequently. If you are disconnected while editing, you can generally recover the file you were working on. The command to give is: M-x recover-file It will prompt you for a file name. It will let you know if a recent autosave is available. If your file is called foo.txt, the autosave will be kept in #foo.txt#, so don't be surprised if you see files like these appearing on your directory. You will also find that emacs keeps a recent previous version of your files in a file whose name ends with tilde (as in foo.txt~). This is helpful when you accidentally save and want to revert to an earlier saved version. The tilde version won't always have what you're looking for, but it often does, so it's worth checking out.