The Other Popular Editor: emacs

The vi editor is a full, feature-rich editing package, but there's an alternative that's worth exploring, too, before you settle on the editor you'll use within the UNIX environment. The alternative is the brilliant, if complex, emacs editor.

Task 12.1: Launching emacs and Inserting Text

Starting emacs is as simple as starting any other UNIX program. Simply type the name of the program, followed by any file or files you'd like to work with. The puzzle with emacs is figuring out what it's actually called on your system, if you have it. There are a couple of ways to try to identify emacs; I'll demonstrate these methods in the “Action 2” section for this task.


Once in emacs, it's important to take a look at your computer keyboard. emacs requires you to use not just the Control key, but another key known as the Meta key a sort of alternative Control key. If you have a key labeled Meta or Alt (for Alternative) on your keyboard, that's the one. If, like me, you don't, simply press Escape every time a Meta key is indicated.

Because there are both Control and Meta keys in emacs, the notation for indicating commands is slightly different. Throughout this book, a control-key sequence has been shown either as Control-f or as ^f. emacs people write this differently, to allow for the difference between Control and Meta keys. In emacs notation, ^f is shown as C-f, where C- always means Control. Similarly, M-x is the Meta key plus the character specified by x. If you don't have a Meta key, the sequence is Escape, followed by x. Finally, some arcane commands involve both the Control and the Meta keys being pressed (simultaneously with the other key involved). This notation is C-M-x and indicates that you need either to press and hold down both the Control and the Meta keys while typing x, or, if you don't have a Meta (or Alt) key, to press Escape, followed by C-x.

With this notation in mind, you leave emacs by pressing C-x C-c (Control-x, followed by Control-c).

  1. First, see whether your system has emacs available. The easiest way to find out is to type emacs at the command line and see what happens:

    % emacs
    emacs: Command not found.
    %
    

    This is a good indication that emacs isn't available. If your command worked and you now are in the emacs editor, move down to instruction 2 in this task.

    A popular version of emacs is from the Free Software Foundation, and it's called GNU emacs. To see whether you have this version, type gnuemacs or gnumacs at the command line.

  2. Rather than start with a blank screen, quit the program (C-x C-c), and restart emacs with one of the earlier test files, dickens.note:

    % gnuemacs dickens.note
    								_                             A Tale of Two Cities
                                          Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins'sdrama of The Frozen Deep, I first conceived the main idea of
    this story. A strong desire was upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    care and interest.
    
    As the idea became familiar to me, it gradually shaped itself into
    its present form. Throughout its execution, it has had complete
    possession of me; I have so far verified what
    is done and suffered in these pages,
    as that I have certainly done and suffered it all myself.
    
    Whenever any reference (however slight) is made here to the condition
    of the French people before or during the Revolution, it is truly
    made, on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    -----Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    							

    As you can see, it's quite different from the display shown when vi starts up. The status line at the bottom of the display offers useful information as you edit the file at different points, and it also reminds you at all times of the name of the file, a feature that can be surprisingly helpful. emacs can work with different kinds of files, and here you see by the word Fundamental in the status line that emacs is prepared for a regular text file. If you're programming, emacs can offer special features customized for your particular language.

  3. Quit emacs by using the C-x C-c sequence, but let a few seconds pass after you press C-x to watch what happens. When I press C-x, the bottom of the screen suddenly changes to this:

    on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    -----Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    								C-x-
    							

Confusingly, the cursor remains at the top of the file, but emacs reminds me that I've pressed C-x and that I need to enter a second command once I've decided what to do. I now press C-c and immediately exit emacs.


Already you can see there are some dramatic differences between emacs and vi. If you're comfortable with multiple key sequences such as C-x C-c to quit, I think you're going to enjoy learning emacs. If not, stick with it anyway. Even if you never use emacs, it's good to know a little bit about it.


Why learn about a tool you're not going to use? In this case, the answer is that UNIX people tend to be polarized around the question of which editor is better. Indeed, the debate between vi and emacs is referred to as a “religious war” because of the high levels of heat and low levels of actual sensibility of the participants. My position is that different users will find that different tools work best for them. If emacs is closer to how you edit files, that's wonderful, and it's great that UNIX offers emacs as an alternative to vi. Ultimately, the question isn't whether one is better than the other, but whether you can edit your files more quickly and easily in one or the other.


Task 12.2: How to Move Around in a File

Files are composed of characters, words, lines, sentences, and paragraphs, and emacs has commands to help you move about. Most systems have the arrow keys enabled, which helps you avoid worrying about some of the key sequences, but it's best to know them all anyway.


The most basic motions are C-f and C-b, which are used to move the cursor forward and backward one character, respectively. Switch those to the Meta command equivalents, and the cursor will move by words: M-f moves the cursor forward a word, and M-b moves it back a word. Pressing C-n moves the cursor to the next line, C-p to the previous line, C-a to the beginning of the line, and C-e to the end of the line. (The vi equivalents for all of these are l, h, w, and b for moving forward and backward a character or word; j and k for moving up or down a line; and 0 or $ to move to the beginning or end of the current line. Which makes more sense to you?)

To move forward a sentence, you can use M-e, which actually moves the cursor to the end of the sentence. Pressing M-a moves it to the beginning of a sentence. Notice the parallels between Control and Meta commands: C-a moves the cursor to the beginning of the line, and M-a moves it to the beginning of the sentence.

To scroll within the document, you use C-v to move forward a screen and M-v to move back a screen. To move forward an actual page (usually 60 lines of text; this is based on a printed page of information), you can use either C-x ] or C-x [ for forward motion or backward motion, respectively.

Finally, to move to the top of the file, use M-<, and to move to the bottom, use the M-> command.

  1. Go back into emacs and locate the cursor. It should be at the top of the screen:

    								_                             A Tale of Two Cities
                                          Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. A strong desire was upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    care and interest.
    
    As the idea became familiar to me, it gradually shaped itself into
    its present form. Throughout its execution, it has had complete
    possession of me; I have so far verified what
    is done and suffered in these pages,
    as that I have certainly done and suffered it all myself.
    
    Whenever any reference (however slight) is made here to the condition
    of the French people before or during the Revolution, it is truly
    made, on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    -----Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    							

    Move down four lines by using C-n four times. Your cursor should now be sitting on the d of drama:

    Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. A strong desire was upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    
  2. Next, move to the end of this sentence by using the M-e command (just like vi, emacs expects two spaces to separate sentences):

    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. _ A strong desire was upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    

    Now type the following text: I fought the impulse to write this novel vociferously, but, dear reader, I felt the injustice of the situation too strongly in my breast to deny. Don't press Return or Escape when you're done. The screen should now look similar to this:

    drama of The Frozen Deep, I first conceived the main idea of this
    story. I fought the impulse to write this novel vociferously, but, dear reader,
    								 I felt
    								the injustice of the situation too strongly in my breast to deny_ A strong des
    ire was upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    

    You can see that emacs wrapped the line when the line became too long (between the words felt and the), and because the lines are still too long to display, a few of them end with a backslash. The backslash isn't actually a part of the file; with it, emacs is telling me that those lines are longer than I might expect.

  3. Now try to move back a few characters by pressing Backspace.

    Uh oh! If your system is like mine, the Backspace key doesn't move the cursor back up a character at all. Instead it starts the emacs help system, where you're suddenly confronted with a screen that looks like this:

    You have typed C-h, the help character. Type a Help option:
    
    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.
    I  info. The  info  documentation reader.
    K  describe-key. Type a command key sequence;
                  it displays the full documentation.
    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.
    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.
    --**-Emacs: *Help*                 (Fundamental)----Top--------------
    								---
    A B C F I K L M N S T V W C-c C-d C-n C-w or Space to scroll: _
    							

    To escape the help screen (which you learn more about later in this hour), press Escape, and your screen should be restored. Notice that the filename has been changed and is now shown as *Help* instead of the actual file. The status line also shows what file you're viewing, but you aren't always viewing the file you want to work with.

    The correct key to move the cursor back a few characters is C-b. Use that to back up, and then use C-f to move forward again to the original cursor location.

  4. Check that the last few lines of the file haven't changed, by using the emacs move-to-end-of-file command, M->. (Think of file redirection to remember the file motion commands.) Now the screen looks like this:

    Whenever any reference (however slight) is made here to the condition
    of the French people before or during the Revolution, it is truly
    made,on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    something to the popular and picturesque means of
    understanding that terrible time, though no one can hope
    to add anything to the philosophy of Mr Carlyle's wonderful book.
    
    Tavistock House
    November 1859
    _
    								--**-Emacs: dickens.note           (Fundamental)----Bot--------------
    								---
    							
  5. Changing the words of Charles Dickens was fun, so save these changes and quit. If you try to quit the program with C-x C-c, emacs reminds you that there are unsaved changes:

    								--**-Emacs: dickens.note           (Fundamental)----Bot--------------
    								---
    Save file /users/taylor/dickens.note? (y or n)  _
    							

    Typing y saves the changes; n quits without saving the changes; and if you instead decide to return to the edit session, Escape will cancel the action entirely. Typing n reminds you a second time that the changes will be lost if you don't save them.

    								--**-Emacs: dickens.note           (Fundamental)----Bot--------------
    								---
    Modified buffers exist; exit anyway? (yes or no)  _
    							

    This time type y and, finally, you're back on the command line.

Entering text in emacs is incredibly easy. It's as though the editor is always in insert mode. The price you pay for this, however, is that just about anything else you do requires Control or Meta sequences. Even the Backspace key did something other than what you wanted.


The motion commands are summarized in Table 12.1.

Table 12.1. EMACS Motion Commands
Command Meaning
M-> Move to the end of the file.
M-< Move to the beginning of the file.
C-v Move forward a screen.
M-v Move backward a screen.
C-x ] Move forward a page.
C-x [ Move backward a page.
C-n Move to the next line.
C-p Move to the previous line.
C-a Move to the beginning of the line.
C-e Move to the end of the line.
M-e Move to the end of the sentence.
M-a Move to the beginning of the sentence.
C-f Move forward a character.
C-b Move backward a character.
M-f Move forward a word.
M-b Move backward a word.

Task 12.3: How to Delete Characters and Words

Inserting text into an emacs buffer is simple, and once you get the hang of it, moving about in the file isn't too bad, either. How about deleting text? The series of Control and Meta commands that enable you to insert text are a precursor to all commands in emacs, and it should come as no surprise that C-d deletes the current character, M-d deletes the next word, M-k deletes the rest of the current sentence, and C-k deletes the rest of the current line. If you have a key on your keyboard labeled DEL, RUBOUT, or Delete, you're in luck because Delete deletes the previous character, M-Delete deletes the previous word, and C-x Delete deletes the previous sentence.


Unfortunately, although I have a Delete key, it's tied to the Backspace function on my system, so every time I press it, it actually sends a C-h sequence to the system, not the DEL sequence. The result is that I cannot use any of these backward deletion commands.

Actually, VersaTerm Pro, the terminal emulation package I use on my Macintosh to connect to the various UNIX systems, is smarter than that. I can tell it whether pressing the Delete key should send a C-h or a DEL function in the keyboard configuration screen. One flip of a toggle, and I'm fully functional in emacs. Unfortunately, it's not always this easy to switch from Backspace to DEL.


  1. Restart emacs with the dickens.note file, and move the cursor to the middle of the fifth line (remember, C-n moves to the next line, and C-f moves forward a character). It should look like this:

    Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. A strong desire was upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    

    Notice that my cursor is on the w in was on the fifth line here.

  2. Press C-d C-d C-d to remove the word was. Now simply type came to revise the sentence slightly. The screen should now look like this:

    Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. A strong desire came_upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    

    Now press Delete once to remove the last letter of the new word, and then type e to reinsert it. Instead of backing up a character at a time, I am instead going to use M-Delete to delete the word just added. The word is deleted, but the spaces on either side of the word are retained:

    Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. A strong desire _upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    

    I'll try another word to see whether I can get this sentence to sound the way I'd prefer. Type crept to see how it reads.

  3. On the other hand, it's probably not good to revise classic stories such as A Tale of Two Cities, so the best move is for me to delete this entire sentence. If I press C-x Delete, which is an example of a multikeystroke command in emacs, will it do the right thing? Recall that C-x Delete deletes the previous sentence. I press C-x Delete, and the results are helpful, if not completely what I want to accomplish.

    Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. _upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    

    That's okay. Now I can delete the second part of the sentence by using the M-k command. Now the screen looks like what I want:

    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. _
    
    As the idea became familiar to me, it gradually shaped itself into
    its
    present form. Throughout its execution, it has had complete
    possession
    of me; I have so far verified what
    

emacs also requires some multikeystroke commands, where you might press a control sequence and follow it with a second keystroke. Although this allows you to have many commands to control your text, it also means you need to know many commands.


  1. Here's a great feature of emacs! I just realized that deleting sentences is just as wildly inappropriate as changing words, so I want to undo the last two changes. If I were using vi, I'd be stuck because vi remembers only the last change; but emacs has that beat. With emacs, you can back up as many changes as you'd like, usually until you restore the original file. To step backward, use C-x u.

    The first time I press C-x u, the screen changes to this:

    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. _upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    care and interest.
    As the idea became familiar to me, it gradually shaped itself into
    its
    present form. Throughout its execution, it has had complete
    possession
    

    The second time I press it, the screen goes even further back in my revision history:

    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. A strong desire crept_upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    care and interest.
    
    As the idea became familiar to me, it gradually shaped itself into
    its
    present form. Throughout its execution, it has had complete
    possession
    

    Finally, using C-x u three more times causes the original text to be restored:

                                    A Tale of Two Cities
                                          Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's
    drama of The Frozen Deep, I first conceived the main idea of this
    story. A strong desire came upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    care and interest.
    
    As the idea became familiar to me, it gradually shaped itself into
    its
    present form. Throughout its execution, it has had complete
    possession
    of me; I have so far verified what
    is done and suffered in these pages,
    as that I have certainly done and suffered it all myself.
    
    Whenever any reference (however slight) is made here to the condition
    of the French people before or during the Revolution, it is truly
    made,
    on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    --**-Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    Undo!
    

If you don't have a Delete key, some of the deletion commands will be unavailable to you, regrettably. Generally, though, emacs has as many ways to delete text as vi has, if not more. The best feature, however, is that, unlike vi, emacs remembers edit changes from the beginning of your editing session. You can always back up as far as you want by using the C-x u undo request.


The deletion commands are summarized in Table 12.2.

Table 12.2. Deletion Commands in emacs
Command Meaning
Delete Delete the previous character.
C-d Delete the current character.
M-Delete Delete the previous word.
M-d Delete the next word.
C-x Delete Delete the previous sentence.
M-k Delete the rest of the current sentence.
C-k Delete the rest of the current line.
C-x u Undo the last edit change.

Task 12.4: Search and Replace in emacs

Because emacs reserves the last line of the screen for its own system prompts, searching and replacing are easier than in vi. Moreover, the system prompts for the fields and asks, for each occurrence, whether to change it. On the other hand, this command isn't a simple keystroke or two, but rather it is an example of a named emacs command. A named emacs command is a command that requires you to type its name, such as query-replace, rather than a command key or two.


Searching forward for a pattern is done by pressing C-s, and searching backward is done with C-r (the mnemonics are search forward and reverse search). To leave the search once you've found what you want, press Escape, and to cancel the search and return to your starting point, use C-g.

Unfortunately, you might find that pressing C-s does very strange things to your system. In fact, ^s and ^q are often used as flow control on a terminal, and by pressing the C-s key, you're actually telling the terminal emulator to stop sending information until it sees a C-q. Flow control is the protocol used by your computer and terminal to make sure that neither outpaces the other during data transmission. If this happens to you, you need to try to turn off XON/XOFF flow control. Ask your system administrator for help.


Query and replace is really a whole new feature within emacs. To start a query and replace, use M-x query-replace. emacs will prompt for what to do next. Once a match is shown, you can type various commands to affect what happens: y makes the change; n leaves it as is but moves to the next match; Escape or q quits replace mode; and ! automatically replaces all occurrences of the pattern without further prompting.

  1. I'm still looking at the dickens.note file, and I have moved the cursor to the upper-left corner by using M-<. Somewhere in the file is the word Revolution, but I'm not sure where. Worse, every time I press C-s, the terminal freezes up until I press C-q because of flow control problems. Instead of searching forward, I'll search backward by first moving the cursor to the bottom of the file with M-> and then pressing C-r.

    								-----Emacs: dickens.note           (Fundamental)----Bot--------------
    								---
    I-search backward:
    

    As I type each character of the pattern Revolution, the cursor dances backward, matching the pattern as it grows longer and longer, until emacs finds the word I seek:

    Whenever any reference (however slight) is made here to the condition
    of the French people before or during the Revolution, it is truly
    made, on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    something to the popular and picturesque means of
    understanding that terrible time, though no one can hope
    to add anything to the philosophy of Mr Carlyle's wonderful book.
    
    Tavistock House
    November 1859
    
    
    
    
    
    
    
    -----Emacs: dickens.note           (Fundamental)----Bot--------------
    								---
    I-search backward: Revol
    
  2. Now to try the query-replace feature. To begin, I move to the top of the file with M-< and then press M-x, which causes the notation to show up on the bottom status line:

    of the French people before or during the Revolution, it is truly
    made,
    on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    --**-Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    M-x _
    							

    I then type the words query-replace and press Return. emacs understands that I want to find all occurrences of a pattern and replace them with another. emacs changes the prompt to this:

    of the French people before or during the Revolution, it is truly
    made, on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    --**-Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    Query replace: _
    							

    Now I type the word I want to replace. To cause confusion in the file, I think I'll change French to Danish, because maybe A Tale of Two Cities really takes place in London and Copenhagen! To do this, I type French and press Return. The prompt again changes to this:

    of the French people before or during the Revolution, it is truly
    made,
    on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    --**-Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    Query replace French with: _
    							

    I type Danish and again press Return.

    as that I have certainly done and suffered it all myself.
    
    Whenever any reference (however slight) is made here to the condition
    of the French_people before or during the Revolution, it is truly
    made, on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    --**-Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    Query replacing French with Danish:
    

    It may not be completely obvious, but emacs has found a match (immediately before the cursor) and is prompting me for what to do next. The choices here are summarized in Table 12.3.

Table 12.3. Options During Query and Replace
Command Meaning
y Change this occurrence of the pattern.
n Don't change this occurrence, but look for another.
q Don't change this occurrence. Leave query-replace completely (you can also use Escape for this function).
! Change this occurrence and all others in the file.

I opt to make this, and all other possible changes in the file, by pressing !, and the screen changes to tell me that there were no more occurrences:

Whenever any reference (however slight) is made here to the condition
of the Danish_people before or during the Revolution, it is truly
made, on the faith of the most trustworthy
witnesses. It has been one of my hopes to add
--**-Emacs: dickens.note           (Fundamental)----Top--------------
---
Done

Searching in emacs is awkward, particularly due to the flow control problems you may incur because of your terminal. However, searching and replacing with the query-replace command is fantasticƒmuch better and more powerful than the vi alternative. As I said earlier, your assessment of emacs all depends on what features you prefer.


Task 12.5: Using the emacs Tutorial and Help System

Unlike vi and, indeed, unlike most of UNIX, emacs includes its own extensive built-in documentation and a tutorial to help you learn how to use the program. As I noted earlier, you access the entire help system by pressing C-h. Pressing C-h three times brings up the general help menu screen. There is also an information browser called info (accessed with C-h i) and a tutorial system you can start by pressing C-h t.


emacs enthusiasts insist that the editor is modeless, but in fact it does have modes of its own. You used one just now, the query-replace mode. To obtain help on the current mode you're working in, you can use C-h m.

  1. Boldly, I opted to press C-h C-h C-h, and the entire screen is replaced with this:

    You have typed C-h, the help character. Type a Help option:
    
    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.
    I  info. The  info  documentation reader.
    K  describe-key. Type a command key sequence;
                  it displays the full documentation.
    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.
    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.
    --**-Emacs: *Help*                 (Fundamental)----Top--------------
    								---
    A B C F I K L M N S T V W C-c C-d C-n C-w or Space to scroll: _
    							

    What to do now? There are actually 17 options from this point, as shown in Table 12.4.

Table 12.4. emacs Help System Command Options
Command Meaning
A List all commands matching the specified word.
B List all key mappings.
C Describe any key sequence pressed, instead of doing it.
F Describe the specified function.
I Start the info browser.
K Fully describe the result of a particular key sequence.
L Show the last 100 characters you typed.
M Describe the current mode you're in.
N Show emacs news file.
S List a command syntax table.
T Start the emacs tutorial.
V Define and describe the specified variable.
W Indicate what keystroke invokes a particular function.
C-c Display emacs copyright and distribution information.
C-d Display emacs ordering information.
C-n Display recent emacs changes.
C-w Display emacs warranty.

  1. I choose K and then press M-< to see what that command really does. The first thing that happens after I type K is that the table of help information vanishes, to be replaced by my original text, and then the prompt appears along the bottom:

    of the Danish_people before or during the Revolution, it is truly
    made, on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    --**-Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    Describe key:-
    

    Pressing M-< brings up the desired information:

                                    A Tale of Two Cities
                                          Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's drama of The Frozen Deep, I first conceived the main idea
    of this story. A strong desire came upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    -----Emacs: dickens.note~          (Fundamental)----Top--------------
    								---
    beginning-of-buffer:
    Move point to the beginning of the buffer; leave mark at previous
    position.
    With arg N, put point N/10 of the way from the true beginning.
    Don't use this in Lisp programs!
    (goto-char (point-min)) is faster and does not set the mark.
    
    
    
    
    
    -----Emacs: *Help*                 (Fundamental)----All--------------
    								---
    Type C-x 1 to remove help window.
    

    A quick C-x 1 removes the help information when I'm done with it.

A considerable amount of help is available in the emacs editor. If you're interested in learning more about this editor, the online tutorial is a great place to start. Try C-h t to start it, and go from there.


Task 12.6: Working with Other Files

By this point, it should be no surprise to you that about a million commands are available within the emacs editor, even though it can be a bit tricky to get to them. There are many file-related commands too, but I'm going to focus on just a few essential commands so that you can get around in the program. The emacs help system can offer lots more. (Try using C-h a file to find out what functions are offered in your version of the program.)


To add the contents of a file to the current edit buffer, use the command C-x i. It will prompt for a filename. Pressing C-x C-w prompts for a file to write the buffer into, rather than the default file. To save to the default file, use C-x C-s (that is, if you can; the C-s might again hang you up, just as it did when you tried to use it for searching). If that doesn't work, you always can use the alternative, C-x s, which also works. To move to another file, use C-x C-f (emacs users never specify more than one filename on the command line; they use C-x C-f to move between files instead). What's nice is that when you use the C-x C-f command, you load the contents of that file into another buffer, so you can zip quickly between files by using the C-x b command to switch buffers. emacs allows you to edit several files at once using different areas of the screen; these areas are called buffers.

  1. Without leaving emacs, I press C-x C-f to read another file into the buffer. The system then prompts me as follows:

    of the Danish people before or during the Revolution, it is truly
    made, on the faith of the most trustworthy
    witnesses. It has been one of my hopes to add
    -----Emacs: dickens.note           (Fundamental)----Top--------------
    								---
    Find file: ~/ _
    							

    I type buckaroo, and the editor opens a new buffer, moving me to that file:

    								I found myself stealing a peek at my own watch and overheard
    General Catbird's
    aide give him the latest.
    "He's not even here," went the conversation.
    "Who?"
    "Banzai."
    "Where the hell is he?"
    "At the hospital in El Paso."
    "What? Why weren't we informed? What's wrong with him?"
    
    
    
    
    
    
    
    
    
    -----Emacs: buckaroo               (Fundamental)----All--------------
    								---
    							
  2. Now I'll flip back to the other buffer with C-x b. When I enter that command, however, it doesn't automatically move me there. Instead, it offers this prompt:

    								--**-Emacs: buckaroo               (Fundamental)----All--------------
    								---
    Switch to buffer: (default dickens.note) _
    							

    When I type ?, I receive a split screen indicating what the possible answers are here:

    I found myself stealing a peek at my own watch and overheard
    General Catbird's
    aide give him the latest.
    "He's not even here," went the conversation.
    "Who?"
    "Banzai."
    "Where the hell is he?"
    "At the hospital in El Paso."
    "What? Why weren't we informed? What's wrong with him?"
    
    
    
    --**-Emacs: buckaroo               (Fundamental)----All--------------
    								---
    Possible completions are:
    *Buffer List*                      *Help*
    *scratch*                          buckaroo
    dickens.note
    
    
    
    
    
    
    -----Emacs:  *Completions*         (Fundamental)----All--------------
    								---
    Switch to buffer: (default dickens.note) _
    							

    The default is okay, so I press Return. Voila! I'm back in the Dickens file. One more C-x b; this time the default is buckaroo, so I again press Return to move back.

  3. I'm in the buckaroo file, and I want to see what happens if I read dickens.note into this file. This is done easily. I move the cursor to the end of the file with M->, press C-x i, and answer dickens.note to the prompt Insert file: ~/. Pressing Return yields the following screen display:

    I found myself stealing a peek at my own watch and overheard
    General Catbird's
    aide give him the latest.
    "He's not even here," went the conversation.
    "Who?"
    "Banzai."
    "Where the hell is he?"
    "At the hospital in El Paso."
    "What? Why weren't we informed? What's wrong with him?"
    
                                    A Tale of Two Cities
                                          Preface
    
    When I was acting, with my children and friends, in Mr Wilkie
    Collins's drama of The Frozen Deep, I first conceived the main idea
    of this story. A strong desire came upon me then, to
    embody it in my own person;
    and I traced out in my fancy, the state of mind of which it would
    necessitate the presentation
    to an observant spectator, with particular
    care and interest.
    
    As the idea became familiar to me, it gradually shaped itself into
    its present form. Throughout its execution, it has had complete
    possession
    --**-Emacs: buckaroo               (Fundamental)----Top--------------
    								---
    							
  4. It's time to quit and split. To do this, I press C-x s and wait for an emacs prompt or two. The first one displayed is this:

    As the idea became familiar to me, it gradually shaped itself into
    its present form. Throughout its execution, it has had complete
    possession
    --**-Emacs: buckaroo               (Fundamental)----Top--------------
    								---
    Save file /users/taylor/buckaroo? (y or n) _
    							

    I answer y to save this muddled file. I'm returned to the top of the file, and a quick C-x C-c drops me back to the system prompt.

One of the more useful facets of emacs you have learned about is the capability to work with multiple files.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.33.87