8.0 Emacs, more than an Editor 

Emacs is not cute. In fact, some might argue that it's down right ugly. In the briefest of terms, Emacs is a text editor. It provides a method for entering, editing and viewing text data. We'll stick with this definition for now, so as not to cause the light-hearted to faint. 

Emacs can be run as both a graphical application that includes pull-down menus or as a terminal application that runs without menus. Without the menus, Emacs relies on many <Control> keyboard sequence commands to execute many navigational and editing functions. This might seem like a big pain at first but you'll find that if you take the time to learn a few keyboard command sequences that you'll be much better off and more productive. 

The terminal mode (no windows) is great if you want to dial-in to the computer system from home (or elsewhere) and still use Emacs. 

In this tutorial, we'll cover running Emacs using solely the keyboard commands and ignore the menus. If you want to use the menus that's OK, it shouldn't take you long to learn how but to really know Emacs is to learn to love the <Control> keyboard commands. 


 




 
8.1 Starting and Exiting Emacs 

Starting an Emacs session can be done by simply moving to a command line (like in the Xterm window) and typing:  and pressing the <Return> key. Emacs can also be started from the Workspace Menu in the Text Processing submenu. If you do this correctly, you will be rewarded by a new Emacs window. You'll note that the new Emacs window looks surprisingly like the Emacs windows that was already on your screen. 

As a general rule of thumb, one Emacs window is almost always enough. Emacs supports multiple buffers to allow you to work on many files at a time. Running multiple Emacs sessions adds unneeded overhead to the system. 

In the example above, we started Emacs as it's own window. If you're working in an environment (if you dialed-in, or telnet in, for example) where it is not possible to run a graphics window, you can start Emacs with the "no windows" option like this:  This will start Emacs in the terminal window you have available to you whether its a telnet session or a dial-up session. 

Since we won't be needing two sessions, we can safely exit one of them. Exiting Emacs is different then merely quitting the window, and it is recommended that you do not simply quit the window to exit the program. 

To exit Emacs properly, it must be your active window. If this is the case, you may press <Ctrl-x> (hold the <Control> key will you press the letter <x>) on the keyboard. Doing this will cause Emacs to display 

C-x- 

at the bottom of the screen. This is Emacs' way of letting you know that it has recognized the <Ctrl-x> command, which it abbreviates to C-x, and is waiting for further instruction. This area is often referred to as the minibuffer. 

Once this is displayed, you may press <Ctrl-c> to finish the command and exit Emacs. If you've done this correctly, the Emacs window will disappear. 

So to exit Emacs, the command is <Ctrl-x> <Ctrl-c>


 
8.2 Entering text into a file 

To continue our Emacs introduction, we can move to the remaining Emacs window. 

Emacs performs, in many respects, like any text editor. To begin create a file, simply begin typing. For the sake of this discussion, let's begin by typing the text below. 

Scholars will recall that several years ago a shepherd, wandering  
in the Gulf of Aqaba, stumbled upon a cave containing several clay jars and also two tickets to the Ice Capades. Inside the jars were discovered six parchment scrolls with ancient incomprehensible writing which the shepherd, in his ignorance, sold to the museum  
for $750,000 apiece. 

Don't worry if you make mistakes. Upon completing the text above, you may find that you have errors throughout the document. If you don't, you're typing far too slowly, because nobody gets "Aqaba" right on the first try. 


 
8.3 Navigating around the text 

Emacs allows you to move around the file in a number of ways. The most simplistic movements (up, down, left, right) can be done using the arrow keys, located on the keypad, right of the main keys. These keys (labeled 8, 2, 4, and 6, with the arrows below) can be used to navigate the cursor around the document. 

Using these keys, you should be able to easily move the cursor to the end of the first sentence. 




8.4 Editing with the delete key 

Let's assume, for the moment, that we aren't really certain that it was really the Ice Capades. We can remedy this by deleting the phrase using the <Delete> key. 

It is important to note at this time that the <Delete> and <Backspace> keys are completely different, and should not be confused with each other. In Emacs, pressing the <Backspace> key will do something wholly other than the intended action. 

With that said, and with our cursor on the period at the end of the first sentence, we can press the <Delete> key 11 times and remove the offending phrase. 

You will notice that the entire right hand portion of the sentence shifts each time you press the <Delete> key. 

Once this has been deleted, we can add a more appropriate phrase by typing  at the current location. You'll notice again that the entire right side of the line shifts as each letter is added. 

If, by the way, you've accidentally pressed a <Return> key at the end, you can remedy this by pressing the <Delete> key once again. 

Using the above technique, you can now go through the text of your file and edit any other errors you may have encountered. In case you're wondering, there are many more advanced methods of editing which will be covered in just a little while. 


 
8.5 Saving a File 

Once the file has been edited, we may save it to disk, confident that we have plagiarized Woody Allen with accuracy. 

To save a file in Emacs, begin by pressing:  As before, the message line will display the emacs abbreviation:  To acknowledge our key-press. At this point, we should press:  Which, of course, means <Ctrl-s>, in Emacs-speak, and tells Emacs we wish to save the file. 

The minibuffer will then display a prompt similar to: 

File to save in: ~/ 

With a darkened cursor after it. The tilde, as you recall, indicates our home directory. 

By typing:  And pressing <Return>, we direct Emacs to save our text in a file named scrolls in our home directory. We can quickly confirm this by moving to the Xterm and doing an ls in our home directory as described in an earlier section of the tutorial. 


 
8.6 Loading a File 

Often, we will need to load a file into Emacs to do some editing. This is done as a method similar to saving a file. We begin by pressing  and then follow that with a  Which tells emacs we're ready to find a file. Once again, we will be presented with a prompt in the minibuffer, this time: 

Find file: ~/ 

At this point, we can type in the name of the file (and/or directory path) that we want to load but sometimes, we can't remember the exact name of the file we want. In that case, if we simply press <Return> here, without typing in a file name, Emacs will display a file list of our home directory in the current buffer. 

From the file list we can use our up/down arrow keys to highlight a file or directory and press <Return> to open it. Opening a file will load it in a new Emacs buffer. Opening a directory will load the sub-directory list into a new Emacs buffer. This makes it pretty easy to find the file you're looking for. Looking at your home directory file list, you might see something like: 

/usr/users/taccount: 

total 85  
drwxr-sr-x 7  taccount  512   Aug 20 11:11 . 
drwxr-xr-x 32 root     1024   Aug 17 09:37 ..  
-rw------- 1  taccount  203   Aug 20 09:56 .Xauthority  
-rwxr-x--- 1  taccount  479   Nov 16 1995  .cshrc  
drwxr-sr-x 11 taccount  512   Aug 20 09:58 .dt  
-rwxr-xr-x 1  taccount 5406   Aug 20 09:14 .dtprofile  
-rw------- 1  taccount    0   Aug 20 11:51 .history  
-rwxr-x--- 1  taccount  694   Aug 16 11:28 .login  
-rwxr-x--- 1  taccount  235   Jul 25 1994  .logout  
drwx--S--- 2  taccount  512   Aug 20 13:17 .mailspool  
-rwxr-x--- 1  taccount  171   Aug 18 1994  .mh_profile  
drwxrwsr-x 3  taccount  512   Aug 20 11:50 .netscape  
-rwxr-x--- 1  taccount  64268 Jun 29 1993  .newsrc  
-rwxr-x--- 1  taccount  842   Aug 18 1994  .openwin-init  
-rwxr-x--- 1  taccount  461   Aug 18 1994  .profile  
-rw-rw-r-- 1  taccount  9     Aug 20 09:56 .seat  
drwx------ 6  taccount  512   Aug 20 11:14 Mail  
drwxr-x--- 2  taccount  512   Oct 13 1992  bin  
-rw-r----- 1  taccount  909   Aug 15 10:18 hello   

Just to clear things up, all the files that begin with a period (dot files) are hidden configuration files. When you do an ls command or view your home directory using the File Manager, you don't see your hidden files by default. Do not concern yourself with the dot files. Also, the lines that begin with the letter "d" represent directories, all others are files. 

For the sake of an example, lets load your Netscape inbox mail file. Use the keyboard's arrow keys to highlight the Mail directory. Once the Mail directory is loaded, load the nsmail directory. In the nsmail directory, you should see your Inbox mail file. Highlight the Inbox file and press <Return>. 

Your mail file should now be loaded in an Emacs buffer. 


 
8.7 Paging Through a Document 

Depending on how many messages you had in your inbox mail file, you may find that this file is much longer than the one we previously created. Still, the cursor keys may be used to move around the document, and the document will scroll up and down if you should move off the screen from the top or the bottom. 

This, of course, is a rather tedious method for moving over a large area. To this end, Emacs has provided several keys for enhanced movement. 

Two of these keys, allow you to page forward or backward a screen full at a time. These keys are <Ctrl-v> and <Meta-v> respectively. 

The Meta-key, by the way, is marked with a diamond, and is located to the left and right of the space bar. This key performs much like the <Shift> and <Ctrl> keys. If the Meta-key is not available to you (like when you dial-in) you should be able to use the <Esc> key instead. 

Using these keys, you should be able to page through the entire document rather quickly. 




8.8 The Emacs Tutorial 

As Emacs has quite a number of features available, it is quite fortunate that the programmers have provided an online tutorial covering many of the basic functions. Along with this tutorial, Emacs provides a massive help system, but for now, the tutorial should keep you going. 

To access the tutorial, we need to first enter the Help system. This is done by pressing  Doing so will cause the message 

C-h (Type ? for further options)- 

 to display in the minibuffer. One of these "further options" is the tutorial, which can be entered by pressing  at this time. You'll notice that we didn't use <Ctrl-t> this time. 

Once you've pressed the letter "t", the Emacs window will change, and you will see something similar to Figure 23. 

 
 

 

When you having nothing better to do, you should take the time to run through the built in tutorial. The tutorial covers a great deal of information that we will go over in the next few sections. 




8.9 Editing in Emacs 

To learn more about the Emacs editor, we will make corrections to the file, editing.text in the System Manager's public directory. 

First, copy the file to your home directory by issuing the following command in an Xterm window: 

fdrebin@blowfish> cp ~dmarlin/public/orientation/editing.text . 

The ending "." (period) tells the copy command (cp) to put the file in the current directory. 

Using the same method, go ahead and copy the file search-replace.text from the same directory. 

Now, let's load the file in Emacs using:  as we did before. The minibuffer will prompt you for the name of the file you want to load. Type editing.text and press <Return>

You should wind up with a file loaded that looks very similar to Figure 24. 


 
8.9.1 Cut/Paste or Kill/Yank 

Now briefly read over the file. There are only a few things that need to be corrected here. First you'll notice that there is an odd line after the first paragraph. It looks like: 

 delete large quantities of text is to position the cursor at the 

This line should really go in the third paragraph. Move the cursor to the beginning of the offending line and type  to kill the line. Move the cursor to the place where you want to insert the line. In this case, that's in between the second and third line of the third paragraph. So move the cursor to the beginning of the third line. That line looks like this: 

with C-k. Since anything deleted is actually moved to a buffer, it 

To give us room to put the new line, issue the command:  Now that we have a cleared line, lets put the misplaced line where it belongs. The command  yanks back the line that we killed using C-k. Now that paragraph looks whole again. 

 
 




8.9.2 Removing a Paragraph 

Looking now at the fourth paragraph, you can see that it is nonsense and should be taken out. Move to anywhere inside the paragraph and type:  This marks the entire paragraph. To delete the paragraph, you can then just type  to kill the marked region, in this case, the paragraph. The paragraph should now be gone. 


 
8.9.3 Inserting Files 

Now we want to insert a part of another file into this one. We can bring up the other file in another emacs buffer window with the command  which tells Emacs that we want to find a file using another window. Emacs will respond in the minibuffer by asking you for the file name to put into the other window. 

Find file in other window: / 

Just type search-replace.text. You should see the screen split into two windows with the new file in the lower one. The cursor should be in the new window. 

 We want to copy the text from search-replace.text into editing.text. Move the cursor to the beginning of the text. Type  that's Emacs lingo for <Control-Spacebar> 

This sets the mark that emacs uses to delimit the region we want to manipulate. The cursor itself represents the other mark that delimits the region, this is called the point. 

 Now move the cursor to the end of the text. When you've reached the end of the text, type  This kills the region and puts it into a buffer. We can copy it back out of this buffer any number of times. 

Now that we've moved the text we want to insert into the buffer, we need to get it copied into our original file. 

To move to the other window, type  that's the letter o not the number 0. 

Note that we do this since we are pretending that we only have the command line. Maybe we are remotely logged in from an outside computer or have dialed in from home. In the windowing environment you can, like always, move the cursor to the desired location by using the mouse. 

Now we can move the cursor to where we want to insert the new text. By typing  like we did earlier, we can "yank" the text back out of the buffer. 

Since we are finished with the other window, we can delete it by typing  which essential means that we only want one window, the current window. 


 
8.9.4 Search and Replace 

The last thing that we need to do is to replace all of those stupid references to "ewe" with the word "you". You can search backwards and forwards through a file pretty easily. The commands are described in the tutorial and are on the GNU Emacs cheat sheet. 

Right now we need to do a Query Replace. This is ideal if you have to change the same string of letters many times in a document. We will specify a string that we want to change, in this case "ewe". Then we will specify the string that we want to replace it with, in this case "you". We execute this command with  Emacs responds with 

Query replace: 

in the minibuffer. You then type in the word to be replaced and press <Return>. The minibuffer should then look like 

Query replace: ewe with:  

Now type in you and press <Return>. Emacs will find the first occurrence of the word "ewe" and wait for you to tell it to replace or go on. If you want to replace this occurrence of the word, you just press <Spacebar>. If you don't, you press <Delete>. If you want it to replace all of the remaining matches, just type <!>. If you want to leave Query Replace before all of the matches have been found press <Esc>. Other options are given on the cheat sheet. 




8.9.5 Saving the File 

Now we've made all the necessary changes to our file, we're ready to save it. 

As demonstrated earlier, to save the file using its original file name simply issue the command:  "s" for save. 

The mini-buffer will respond indicating that it wrote the file. 

If you want to specify the name of the file when you save it, use the command:  "w" for write 

The mini-buffer will respond by prompting you for a file name. 




8.10 Getting Help in Emacs 

 The help facility in emacs provides information in a variety of ways. The way to enter the help is by typing  Emacs will respond with the following message in the minibuffer 

 C-h (Type ? for further options)- 

When you type ? as it suggests, Emacs will open a window displaying a list of help options. 

Each of these options provides you with a different way to get help. 

Here is the complete list of help options: 


a command-apropos. Give a substring, and see a list of commands 
            (functions interactively callable) that contain 
            that substring. See also the apropos command. 
b describe-bindings. Display table of all key bindings. 
c describe-key-briefly. Type a command key sequence; 
            it prints the function name that sequence runs. 
f describe-function. Type a function name and get documentation 
            of it. 
C-f Info-goto-emacs-command-node. Type a function name; 
            it takes you to the Info node for that command. 
F view-emacs-FAQ. Shows emacs frequently asked questions file. 
i info. The info documentation reader. 
k describe-key. Type a command key sequence; 
            it displays the full documentation. 
C-k Info-goto-emacs-key-command-node. Type a command key sequence; 
            it takes you to the Info node for the command bound to 
            that key. 
l view-lossage. Shows last 100 characters you typed. 
m describe-mode. Print documentation of current major mode, 
which describes the commands peculiar to it. 
n view-emacs-news. Shows emacs news file. 
p finder-by-keyword. Find packages matching a given topic keyword. 
s describe-syntax. Display contents of syntax table, plus 
            explanations 
t help-with-tutorial. Select the Emacs learn-by-doing tutorial. 
v describe-variable. Type name of a variable; 
            it displays the variable's documentation and value. 
w where-is. Type command name; it prints which keystrokes 
            invoke that command. 
C-c print Emacs copying permission (General Public License). 
C-d print Emacs ordering information. 
C-n print news of recent Emacs changes. 
C-p print information about the GNU project. 
C-w print information on absence of warranty for GNU Emacs. 


 
8.10.1 Emacs Apropos Help 

Let's try one or two of these. First try the apropos command. This is useful if you want to find out what that command is for transposing paragraphs. You knew it, but doggone it you just can't remember it. By typing...  the help system will provide you with a list of commands that contain the word transpose. You can see that transpose characters, lines, paragraphs (Whoa! The command turns out to be transpose-paragraphs. Duh!), sentences, sexps (which stands for syntactically recognizable expressions), and words. The ones that are bound to a key sequence show it in the center of the line with the command name. For example, transpose-chars is bound to C-t. You'll notice that transpose-characters is not bound to a key sequence. There are only so many key sequences that can be used as command sequences. 

To execute a command that is not bound to a key sequence, you have to use the M-x command like this:  The M-x keyboard sequence always means, execute this command. 




8.10.2 Emacs Bindings Help 

Now, lets use the Emacs help system with the "b - describe-bindings" option. Use the command:  This will show us a list of all the commands that are bound to keyboard commands. The list can be quite long. You'll have to change to that window using the C-x o (letter o) command as mentioned earlier. Now you can use C-v and M-v to scroll up and down the list. 


 
8.10.3 Emacs Mode Help 

The only other help option we will explore right now is describe-mode. This is done with the command:  This provides information about the current mode as well as key sequences that are bound to commands in this mode. This will be helpful after you start using some of the modes you'll learn about in a later session. You can manipulate this window in exactly the same way as the others. 




8.11 Emacs Buffers 

Buffers have been mentioned a number of times already in this discussion. Every time you bring up a file or bring up a help window or a variety of other things, Emacs creates a new buffer. It does not get rid of these until you either exit Emacs or you specifically tell it to. Most of the time you don't have a real reason to tell it to and letting Emacs deal with it works just fine. 

Sometimes, though, you might get lost in the buffers and need to find your way out. You can select a buffer in another window by typing  Emacs will respond in the minibuffer by asking you which buffer you want to switch to and showing you the default. The default is the last buffer you previously accessed. If this default is the one you want to move to, you can just press <Return>. Emacs will bring up that buffer and move the cursor to that window. 

You can then close the other window(s) and make the current buffer (the one your cursor is in) the only window by using the command:  To have Emacs show you a list of all your buffers, use the command:  This will open up another buffer in another window which shows the list of all your active buffers. Your list might look something like: 

MR Buffer Size Mode File 
-- ------ ---- ---- ---- 
.  editing.text 2045 Text editing.text 
*  *Help* 163 Help  
*  search-replace.text 6 Text search-replace.text 
%  orientation 1047 Dired by name ~/public/orientation 
*  *Messages* 740 Fundamental  
*% *Buffer List* 413 Buffer Menu  
 
The first column tells you the status of each buffer. An "*" means that the file has not been saved, a "%" means that the buffer is read only. The second column tells you the name of the buffer. This will help you when you want to switch buffers and have to give it the name. The third column is the size of the buffer. The fourth says what mode each buffer is using. The last column shows the file, if any, that is in that buffer. 

You can switch to that window using the C-x o (letter o) and move to the buffer name that you want. With the cursor sitting on the name of the buffer you want to switch to, you can bring it up as the sole window by pressing the number 1.