COLLEGE OF ARTS AND SCIENCES
DEPARTMENT OF COMPUTER SCIENCE
REFERENCE AND HELP

Starting to Use Emacs


  • Introduction
  • The Emacs Environment
  • Setting Up Emacs
  • Ctrl and Meta Keys
  • Common Emacs Commands
  • Getting Additional Help

  • Introduction

    One of the earliest skills needed by an advanced computer user, especially when using UNIX, is the ability to learn and master text editors. The pico editor, popular among new UNIX computer users for it's simplicity, can do the job, but you will find yourself limited in your potential editing capabilities. You need to learn more advanced editors, such as vi and emacs. We recommend using the Emacs text editor.It is widely available and very powerful. It is the editor that many recitation instructors use.

    After using Emacs for a short learning period, the speed and efficiency at which you write code should increase. To start Emacs type emacs -nw <filename> at the command prompt. The -nw is a flag that stands for "No Window". In its default mode, Emacs tries to send its output to an X-Windows terminal. SecureCRT is not an X-Windows emulator so it cannot display this output. If you forget to use the -nw flag, you will get a message that looks similar to this:
        emacs: Cannot connect to X server polaris.oddl.fsu.edu:0.0.
        Check the DISPLAY environment variable or use `-d'.
        Also use the `xhost' program to verify that it is set to permit connections from your machine.

    The Emacs Environment

    Any definition of good code includes the idea of readability. It is extremely important that others be able to read and understand the code you produce. Readability begins with logical and consistent formating. The Emacs C/C++ mode defined in the colorEmacs profile serves this purpose well. We strongly urge you not to deviate from this mode unnecessarily. Undoubtedly, you may develop a personal style, but ensure that it is clean, consistent, and predictable for the reader.

    We highly recommend that you format your code as you write it. Postponing the tasks of formatting and documenting your code is not advisable for two reasons. First you may forget or run out of time. Eventually, you may end up delivering code that is not readable. And secondly, your code will be hard to read while you are creating and debugging it. This could result in a significantly prolonged development time.

    Good and consistent formatting are in fact, an important form of documentation. After all, the purpose of embedded documentation is readability (including comprehension of what is read). Good formatting and careful choice of names will go a long way toward documenting your code, largely eliminating the need for embedded comments.

    We strongly advise continue using the 2-space indentation scheme that is the default in the colorEmacs environment. You will be writing some complicated code, and you don't want to waste too much left margin with multiple indentations!

    Setting Up Emacs

    To get the most out of Emacs you need to ensure that it is configured correctly. Copy the file colorEmacs.tar.gz and unzip/untar it on your personal workstation desktop. Then use SecureCRT to upload it to your home directory on quake. You might want to save the desktop file, in case you decide to install Emacs on your own workstation.

    The file is named "colorEmacs" because it displays color syntax highlighting if you are using an X-terminal. You won't get the benefit of color in a SecureCRT session with quake, but you will whenever you use an X terminal, or when you use it locally on your own workstation. But, an important benefit of using the colorEmacs profile is its definition of various key bindings that customize an editing session depending on the filename extension. In particular, in C/C++ mode, there are many useful key bindings and an excellent mode/environment. Some of the benefits of the C/C++ mode are described in the slide.

    Ctrl and Meta Keys

    Emacs uses two keys, aside from the ones used in all text editors, to perform any non-typing actions. These two keys are called the control and meta keys. In all of the examples, the control/Ctrl key will be abbreviated as 'C' and the meta key will be abbreviated as 'M'. The Meta key can be set up as either Esc or Alt. We strongly recommend Alt as the Meta key. The following discussion assumes this is the case.

    Control and Meta keys are used in exactly the same way. To use a control sequence, simply hold down the Ctrl key and press the other key. After pressing the other key, you may release both keys. For example, in the sequence C-x, you press both the Ctrl and the x keys. Some control sequences involve more than one set of keys to press, such as C-x C-s. In this case you can hold down the Ctrl, then press x, then press s, then release both Ctrl and s. You also have control sequences that use three keystrokes, such as C-x k. To execute this statement, hold down Ctrl and press x. Release Ctrl and press k.

    An odd situation occurs when an upper level symbol is required. For example, to invoke "replace with query", you need to enter M-%. Because '%' is in the upper range, you must actually press three keys: M, Shift, and %/5.

    As a side note, if you choose not to use ALT as your Meta key (which is not recommended) then the Meta key is used a little differently than I described earlier. Instead of holding the Meta key down while pressing the other key, simply press and release the Meta key. Then press and release the other key.

    Now let's try a few commands. First, open Emacs by typing emacs -nw at the command line. This will open up the scratch buffer with a few comments in it. Enter M-? to enter the built-in tutorial. It wouldn't be a bad idea to read through the tutorial but at the moment we're just going to use it as some text to edit. You can use the arrow keys to move around. Hitting any alphanumeric character will insert it at the location of the cursor. Now type C-v a couple of times to scroll down a few pages. You can use M-v to scroll up.

    The status line of Emacs, which is the black line at the bottom of the screen, contains some useful information. The number after the letter 'L' on the right side of the screen indicates the line number you are on. On the left, immediately after the : (hereinafter referred to as the colen), there will be a -- if the file has not been modified. If the file has changed since last opened, there will be a ** to the right of the colon. If the file is "read only", %% will appear after the colon.

    Move to a line that has text on it and type C-k. This clears the line. Hit C-k again to remove the blank line that was left behind. Now type C-y to restore the removed lines. C-k removes everything from the cursor to the end of the line. Successive C-k's all place the information into the recall queue. When you execute a C-y, all the lines stored in the recall queue are recalled. You can think of this queue as the clipboard in Windows. Try deleting several lines and then yanking them back from the queue (the y in C-y stands for "yank").

    Deleting many lines at one time can be accomplished more easily by using a sophisticated cut action. First place your cursor where you want to start a cut. Then press C-SPACEl; this sets the mark. Now, move the cursor to the end point of the cut and press C-w. This removes all the data in-between the mark and the cursor. To restore the information, simply type C-y. C-y places the information at the cursor, so the information need not be placed at the same position as it was deleted/cut.

    You can use the C-y to paste the same line multiple times. Try this by using C-k C-k to delete a whole line. Now, enter C-y C-y C-y to create three copies, each on its own line. Finally enter C-x C-c to exit Emacs. When asked if you want to save, just type n then yes.

    Emacs creates some special files that you should be aware of. Emacs automatically makes a backup file every time you save. This file is named <saved file>~. If you are sure you don't need to undo the save, you can always delete this file. Also Emacs periodically makes an auto backup. This auto backup is saved to a file if the computer goes down, or if you exit Emacs without saving any changes. This file looks like #<filename>#. You can also delete these files if you are sure you don't need them.

    Common Emacs Commands

    A few of the most commonly used Emacs commands are listed on the slides. You should take some time to go through the built-in tutorial, which explains many of these commands in more depth and provides examples.

    Getting Additional Help

    Emacs is a large and powerful program. There are hundreds of commands and ways to modify Emacs, which can be both a blessing and a curse. There are many places to get additional information about emacs. All of the sites listed on this page were found on the Web using a generic search engine. We do not recommend that you try to learn all there is to know about emacs in one week, or even one semester (unless you are devoting full time to the task). Be happy with the knowledge that there is usually something left to appreciate about emacs that you don't know yet.