emacs basics

Return to Phill Conrad's:         home page             CS8 page          CS16 page        

Introduction

In many Computer Science courses at UCSB, you may need to use a basic text editor.

Although both emacs and vi/vim are fine editors, emacs is often a little easier for beginners to learn.

This is not a complete introduction to emacs—it is intended only to get you started, and then refer you to resources where you can learn more.

Emacs QuickStart

  1. First thing to know: in the instructions which follow, "CTRL/X" means "hold down CTRL, and type X".
  2. To get started with emacs, open a terminal session (ssh session, or xterm) type emacs at the Unix prompt.
  3. When emacs comes up, immediately type first CTRL/X, then CTRL/C. This will take you back to the Unix prompt.

    You've just learned your first, and most important lesson: how to get out of emacs. It is also the easiest way to quickly learn how to save files; if you have made any changes, emacs will ask you if you want to save before you exit.

  4. Now, to create or edit a file called "foo.dat", type

    emacs foo.dat

    at the Unix comand prompt. Emacs should come up with a blank screen if foo.dat doesn't exist yet, or the contents of foo.dat if it already does.

  5. To insert text into the file, just start typing.
  6. Use the arrow keys to move around.
  7. To make changes, use CTRL/D to erase the current character.
  8. Another option: to erase the previous character, use either the delete key or the backspace key. Which one? Unfortunately, which one works will depend on how your account and keyboard are setup, and usually, if one works, the other doesn't.
  9. If you type backspace and it doesn't delete, it might instead bring up some funky stuff, like the examples below. In either case, type "CTRL/G", and the funky stuff will go away. That works with lots of things in emacs.. .if you accidentally hit the wrong key, CTRL/G is your friend; it will get you back to where you need to be.

    Here are two examples of funky stuff that you might see if you are having "backspace" vs. "delete" problems.

    C-h (Type ? for further options)-

    or splits your screen and you see this:

    stuff you typed appears here
    blah blah blah

    ...You have typed C-h, the help character. Type a Help option:
    (Use SPC or DEL to scroll through thi


    Solution in either case: CTRL/G. CTRL/G makes it all better.
  10. If you are working on an Xterminal, you might also be able to use the mouse to move around, scroll up and down, and access menus at the top of the screen. Those options won't be available if you ssh in from a PC, but if you are on an XTerm, you'll see familiar nice things like "cut" and "paste", etc. It is possible to do "cut" and "paste" from emacs on a PC, but you have to use other keystrokes; we'll save that lesson for later.

That's most all you need to know. Here are two quick troubleshooting tips

  1. If the arrow keys don't work, you can also use CTRL/F, CTRL/B, CTRL/P and CTRL/N for "forward", "backward", "previousLine", "nextLine" instead of the arrow keys.
  2. If you get into trouble, CTRL/G is your friend.

Starting emacs

To bring up emacs to make changes to ctof.c, type:

emacs ctof.c

This may bring up a new window where you can edit this program.

Do I have to use emacs?

If there is another program that you prefer, e.g. vim, that is already available on the Cooper Lab (and CSIL) computers, and you already know how to use it (or are willing to take responsibility for learning how) that's fine. So, no, you don't have to use emacs, specifically. However you will need to learn some editor that works on the lab computers. Working only on your own PC or Mac at home will not be sufficient.

By the way, knowing how to use either vi/vim or emacs is really a kind of a "basic skill" that every software developer should have in his/her toolkit. So if you don't already know one of those editors, now is as good a time as any to start learning one of them.

Some emacs commands

Here are a few emacs commands that may be helpful to know.

Remember that:

C-x is called "control-X" and means "hold down the control or Ctrl key while typing the letter x"
C-x 1 means "type control-X, then type the number 1".
C-x C-c means "type control-X then type control-C".

Keystroke Command
C-x 1 If there are multiple windows, get rid of the other ones.
I only want the one the cursor is in right now.
Arrow Keys Used to move around
Delete/Backspace Deletes the previous character
C-d Deletes the character immediately under the cursor
C-x C-w Save As
C-x C-s Save
C-x C-c Exit emacs (it will ask if you want to save)
C-g Cancel current command ("get me out of trouble")
C-k Kill (delete) the current line

An emacs reference

Here's a link to a PDF File with lots more emacs commands—it is suitable for printing on a single sheet of paper (front and back) and having handy while you work in emacs.

Accessing the Emacs Built-in Tutorial

(These instructions assume you already read through the quickstart above.)

Emacs has a tutorial built right into itself!
Here's the easy way to bring up the tutorial.

  1. Go into emacs without a file (just type emacs at the unix command line).
  2. If you are lucky, you'll be able to bring up the tutorial by just typing CTRL/H, then typing the small letter "t" (t is typed without the control button.) If that works, you'll see a screen like the one in the box below. (If it doesn't work, scroll on down this web page past the little box below, to where it says "another way to get to the built-in tutorial")
  3. Now all you have to do is read what's on the screen, and follow the instructions. Essentially, you are now editing a copy of a great big file called TUTORIAL that teaches you all about emacs.

    The first thing you learn is the short-hand notation for control and meta (C-C is CTRL/C, and M-C is Meta-C), and how to type them. Then you learn that C-V (CTRL/V) will scroll you down a screen... you try it, and suddenly, there's more stuff to learn and try on the next screen.. and you're off and running with learning about emacs! When all done, type CTRL/X then CTRL/C to get out (don't save any changes, otherwise you end up with a big copy of the TUTORIAL file in your directory.)
    Copyright (c) 1985 Free Software Foundation, Inc;  See end for conditions.
    You are looking at the Emacs tutorial.
    Emacs commands generally involve the CONTROL key (sometimes labeled
                 CTRL or CTL) or the META key (sometimes labeled EDIT or ALT). Rather than
                 write that in full each time, we'll use the following abbreviations:
     C-<chr> means hold the CONTROL key while typing the character <chr>
                 Thus, C-f would be: hold the CONTROL key and type f.
                 M-<chr> means hold the META or EDIT or ALT key down while typing <chr>.
                 If there is no META, EDIT or ALT key, instead press and release the
                 ESC key and then type <chr>. We write <ESC> for the ESC key.
    Important note: to end the Emacs session, type C-x C-c. (Two characters.)
                 The characters ">>" at the left margin indicate directions for you to
                 try using a command. For instance:
    >> Now type C-v (View next screen) to move to the next screen.
                 (go ahead, do it by holding down the control key while typing v).
                 From now on, you should do this again whenever you finish
                 reading the screen.
               

The slightly harder way to get to the tutorial (but still not too tough)

  1. Go into emacs without a file (just type emacs at the unix command line)
  2. Once inside, the first thing you will do is the "meta-X" keystroke.

    To do a meta-X keystroke, first type the Escape key (abbreviated "Esc", and usually at the upper-left-hand corner of the keyboard), and type the letter X.

    Note that this is different from CTRL/X; with CTRL/X, you hold down the CTRL key while typing the x, but with "meta-X", you just "type" the esc key, then type the X (two separate keystrokes.)
  3. The meta-X keystroke is used to put the cursor at the bottom of the screen so you can type in a "named command".Type in the following, and hit return: help-with-tutorial. It will look something like the box below. Once you hit enter, you should be able to pick up with step 3 on the "easy way" (click here to go back there now.)
    emacs meta x help-with-tutorial

Emacs reference guide (PDF): refcard.pdf

Last update: P. Conrad, 12/03/2013



Valid XHTML 1.1 Valid CSS!