The goal of this lab is to introduce you to the GNU Emacs editor, a customizable, self-documenting text editor (and more). It also serves as a powerful environment in which to debug programs.

As in all labs, don't hesitate to ask for help as you work through the lab. If you aren't getting anywhere after staring at the screen for a minute or two, call someone over --- your neighbor, a lab assistant, or a professor --- to help you out.

Launching Emacs and running the tutorial (1 point)

  1. Launch emacs by opening a Unix shell and typing:
      emacs &
    The & makes the program run in the background so that you can continue to type other commands to the Unix shell should you want to.

    Be careful not to type another key until you've finished reading the help that comes up on the screen. Note that the `M-' key is the key between with the flag between Ctrl and Alt on the Linux machines.

  2. Memorize how to exit, play around for a minute or two, and then exit emacs.

  3. Restart emacs.

  4. Work through the Emacs tutorial. As you do so, familiarize yourself with the GNU Emacs Quick Reference Guide. You may wish to check off each command on the reference guide as you learn it.

  5. Check-off: Make the tutorial the sole window and ask to be checked off. We'll ask you to do a couple editing tasks such as moving down 153 lines using C-u.

Customizing Emacs (1 point)

  1. The file ~/.emacs contains commands which get executed by Emacs when it initially starts up. You may wish to edit this file to customize the behavior of Emacs to your liking. (If the file doesn't exist yet, you'll need to create it.)

    As an example, you'll change Emacs so that the keystroke M-g allows you to go to any line in a file by entering its line number. Create and edit the file ~/.emacs, placing the following line in the file. The line can be placed at the start or end of the file (or almost anywhere in between).

      (global-set-key "\M-g" 'goto-line)
    Schemers should recognize this command format as a Lisp dialect; using Emacs-Lisp you can define new behaviors and functions of your own.

  2. Now type M-g to see what emacs did before the change. (I've found the built-in function useless. Hit C-g to abort.) Launch a new emacs, type C-h t and try out M-g again. See what it does when given a prefix argument.

  3. You should next edit your .emacs to add some commands that are very useful for editing C++ files. The commands appear in this link. While in that link, you should highlight the region in netscape using your left-mouse button, and then paste it in your .emacs buffer using the middle mouse button.

    I should explain what you've just put in your .emacs file. The command (load "~mc38/.emacs-mc38") tells emacs to load the file ~mc38/.emacs-mc38, which we'll maintain with commands to indent your C++ programs to more closely match the style guidelines in your text. (Be aware that this is a security risk, since you are entrusting us with the ability to put invasive commands there... but we won't.)

    The command (add-hook ... changes the behavior of emacs when editing C++ programs. You may not like the features provided by (turn-on-auto-fill) and (c-toggle-auto-state 1). The latter makes the keys ; and { and } and : all electric keys, putting in newlines and indenting when you type them. After trying these features out in a future lab, feel free to remove them. I've found I like to edit code I've written with c-toggle-auto-state turned off, while entering code from scratch I like it turned on. You can turn it on and off with C-c C-a. Or, you can quote the character to be sure it's inserted non-electrically with, say, C-q {.

    The last command instructs Emacs to color C++ keywords, strings, and comments. In combination with auto-indentation, you'll find you can locate most syntax errors quickly.

  4. Check-off: Show that you have altered your .emacs file and that M-g performs as indicated above.

  5. If you don't like any of these changes, remove the corresponding line from your .emacs file.

  6. Note: As a last resort, many of the most common Emacs commands can also be accessed using the menu bar at the top. Check them out using your left-mouse button. By the way, the options under the menu bar change depending on what kind of file you are editing.

Editing practice (1 point)

  1. Open a file entitled text.txt somewhere in your home directory. Use the left-mouse button to select all the text in the link that follows. Using the middle-mouse button, you can drop this into your emacs buffer. Edit the text as the text itself instructs. This link is the text you need to edit.

  2. Check-off: Leave the corrected text.txt file on your screen as you ask to be checked off for this part. Be sure the file has been saved.

Emacs help (1 point)

  1. Load the file ~mc38/labs/emacs/names.txt in emacs. You will see an unsorted list of names with each line in the form last name, first names. The list is a list of registered students from a similar C++ course at Berkeley a few years ago.

    Try editing the file. You'll notice that Emacs won't let you, since you aren't the owner of the file. Use C-x C-w to write the file somewhere in your home directory (~/mc38/names.txt is a good choice.) C-x C-w is just like C-x C-s but it asks for a name for where to save the file.

    You'll find you still can't edit the file though. Notice the %% on the far left side of your mode line. If you hit C-x C-q (or, equivalently, M-x toggle-read-only), you'll be able to edit the file. Note also that the %% goes away.

  2. You should use the help facilities of emacs (apropos and info) to find out how to sort all these names by last name. In other words, you want to sort the lines of the file. If you are unfamiliar with the help facilities type C-h ? and emacs will guide you through them. Note that {\tt info} has documentation on quite a few programs; not just emacs. The first thing you'll want to do is to select m~Emacs for menu item Emacs. Help clues on Emacs (including how to navigate info) are in Paul Hilfinger's notes on GNU Emacs.

    Save the result using C-x C-s

  3. Using the point and the mark (info can help you if you do not know what those are), copy all the names above and including Kusnadi, Ali to a file named names1.txt somewhere in your home directory and all the remaining names to names2.txt. Save both files but do not alter names.txt.

  4. Within names1.txt and names2.txt, find a sorting command to sort by first names. You will probably need to use info and apropos again. Depening on the approach you try, you may need to pass an argument to an emacs command for this part. Recall that this is done in either of the following two ways:
      M-arg M-x command
      C-u arg M-x command
    A second possible approach will require you to set the mark and the point. If you have troubles locating help in info on this, ask us for help! Save the results.

  5. Check-off: Ask to be checked off for the lab. You should have names1.txt in one buffer and names2.txt in the other.

Congratulations! You are done with the lab. Make sure you get your check-off points before you leave.

Suggested further exercises

  1. Use regular expression searching to find last names which have an I followed by any two letters and then an A.

  2. (Extra credit) Reformat the names so that they appear as follows:
    Catherine Huang
    Brent Allen Vincent     
    There are two ways you might think of
    1. Use keyboard macros
    2. Write an emacs-lisp function
    The first option is quicker and easier, but it can be less robust.