Using Emacs Last Revised: 4-19-00
Total Page:16
File Type:pdf, Size:1020Kb
SSCC Pub.# 7-10 Using Emacs Last revised: 4-19-00 The GNU Emacs editor is the most commonly used text editor at the SSCC on UNIX. It is an extensible, customizable, self-documenting full-screen editor. Emacs is extremely powerful and has too many specialized commands and features to cover completely here. This handout serves only as an introduction, helping you get started using Emacs. A GNU Emacs Reference Card is attached to the end of this handout which lists many of the most frequently used Emacs commands along with a short description of what each command does. If you find you need to learn more after reading this handout, Emacs has a comprehensive online help facility where you can get more information. This help facility is described in this handout. Two books, Learning GNU Emacs, by Debra Cameron and Bill Rosenblatt and GNU Emacs Manual, by Richard Stallman provide a more comprehensive coverage and are available from Consultant for short term loan. Emacs is a modeless editor. This means that you insert text into your document without having to first enter a special insert mode. Likewise, you do not enter a special command mode when you need to issue a command. When you type an ordinary printable character such as the letter "k", it is inserted into your document; when you type a special character such as "Control-v" or "Meta-v", it is taken as a command (or the beginning of a command) to perform some operation such as deleting text or moving to the next line. Typing Emacs Commands Each command in Emacs has a formal name which is very long. Instead of typing out command names (which you could do), Emacs ties each command name to a short sequence of keystrokes starting with the Control key or Meta key, followed by one or more characters. This tying of commands to keystrokes is called binding. Commands shown in this handout use the following conventions: C-v Hold down the Control key and press v. M-v Hold down the Meta key and press v. On the NCD X-terminals, the Meta key is the Alt Function key. If your keyboard does not have a Meta key, you can use the Escape key instead. Rather than holding down the Escape key and pressing a character as you do with the Meta key, you need to press the Escape key, release it, and then press a character. To complete a command you usually need to press a carriage return: Note: If your keyboard has no Escape key, press C-[ (hold down the Control key and press [) instead. Release the key before pressing the next character in your command. The most commonly used commands are bound to the Control key. Slightly less commonly used commands are bound to the Meta key. Other commonly used commands are bound to C-x something, where something is some character you type in after pressing C-x. You can also get at commands by typing M-x long-command-name, where long-command-name is a command's formal name. Most Emacs users prefer to use the short sequence of keystrokes over the long command names because they are 1 SSCC Pub.# 7-10 easier to learn and take less time to type in. Emacs has a very helpful feature known as completion. For example, if you are typing the name of a file in the minibuffer (more about the minibuffer later) after issuing a command such as find-file (C-f), you need only type the first few letters of the name, enough to make a unique filename. Then press the Tab key and Emacs will complete the filename for you. Getting Into and Out of Emacs To enter Emacs, simply type emacs followed by the name of the file you want to edit. If you use a filename that does not exist, Emacs creates a new file and you will see a window similar to the one below: You simply enter text to begin your editing session. If the file you specify already exists, Emacs reads the file and displays it on the screen: You can also omit the filename when you invoke Emacs. In this case, your screen will look similar to the one below: 2 SSCC Pub.# 7-10 The message printed in the Emacs window disappears as soon as you type the first character. Emacs then puts you in an empty buffer (a buffer is a temporary workspace that may be saved later as a file) called *scratch*. Note: *scratch* buffers are not saved when you exit Emacs. Type C-x C–w to write this temporary buffer to a permanent file. The Emacs window contains three major areas: the largest area which takes up most of the window is where you enter your text. A cursor marks your position in the file. The cursor is also called "point" or "dot". The area at the bottom of the window which is in reverse video is called the mode line and provides information about the editing session: the name of the buffer being edited, the date, the mode you are in (Emacs has many different modes, each of which customizes Emacs for editing text of a particular sort), and where you are in the file. If you are at the beginning of the file, Emacs prints the word Top; if you're at the end, it prints Bot; if you are somewhere in the middle, it shows you a percentage; and if the entire file is visible on the screen, Emacs prints the word All. At the left edge of this line, you may see two asterisks (**). If the asterisks are there, this means that whatever you are editing has been modified since the last time you saved it. At the bottom of the window, below the mode line is the minibuffer. This is the area where Emacs echoes the commands you enter and where you specify filenames for Emacs to find, values for search and replace, and so on. To exit Emacs at any time, type C-x C-c. If you have made any changes that have not been saved, Emacs will prompt you, asking you if you want to save your changes. Getting Help Emacs has an extensive help facility, which you can activate by typing C-h. C-h t activates the Emacs tutorial which provides an excellent guided tour of most of the important Emacs features and concepts. However, do not use it for retrieving information on how to do something in particular. There are better ways of doing this. Pressing C-h k asks Emacs to give you documentation on whatever keystroke you type next. For example, if you type C-h k C-x i, Emacs gives you a description of the insert- 3 SSCC Pub.# 7-10 file command. Pressing C-h f asks Emacs to describe a function (really just a command's full name such as find-file). Essentially, C-h k and C-h f give you the same information; the difference is that with C-h k you press a key and ask what it does, whereas with C-h f you type a command name and ask what it does. To display the help options, type C-h three times. Emacs displays a window listing the options and providing a brief explanation of each one. Some options replace the window by a window of help information; some others split the screen into two windows. The screen always contains at least one window; the window containing the cursor is called the selected window. The following commands provide the window operations you will need in order to use the help system and exit from it when you are done: ! To switch windows when the screen contains more than one window, type C-x o. ! To scroll the selected window forward, type C-v. ! To scroll the selected window backward, type M-v. ! To scroll the other window forward, type M-C-v. ! To make all windows disappear except for the selected window, type C-x 1. ! To remove help information from the selected window, type C-x k. Probably the most useful form of help when you are first starting out is "apropos" help, which you can use to find out all the commands relating to a particular topic. You can get "apropos" help by typing C-h a. Emacs then asks you for some text that might appear in the names of the commands you are interested in. For instance, if you respond with delete, Emacs lists all the commands that have delete in their names along with their explanations. Editing Files Emacs achieves some of its famed versatility by having various editing modes in which it behaves slightly differently. Mode in Emacs simply means being sensitive to the task at hand. When you are writing, you often want features like word wrap so you do not have to press Return at the end of every line. When you are programming, the code must be formatted correctly depending on the language. For writing, Emacs has a text mode; for programming, Emacs has modes for different languages. Whenever you edit a file, Emacs attempts to put you in the correct mode for what you are going to edit. Fill Mode and Text Mode Before you start typing, look at the mode line at the bottom of the Emacs window. If the word Fill appears, you are in fill mode. In fill mode, when you type past the end of a line, 4 SSCC Pub.# 7-10 Emacs automatically starts a new line for you.