Basic Emacs Commands Lab

January 24, 2008

Revised: August 20, 2009

The purpose of this lab is to teach you some basic emacs commands. You will be a much more productive programmer if you are proficient in the tools that you use as you program and emacs is one such tool.

Simple Emacs Commands

Assuming that you have started your emacs editing session, try the following.

    Start typing whatever you want to type in that buffer of your
    current emacs session.  In fact, type about 30 simple lines of
    text so that we can start editing it.  If you already know how to
    bring in an existing file into emacs as a buffer (C-x C-f below),
    do that instead.

    Assuming that the cursor is at the beginning of the buffer, let's
    move the cursor around - the location of the cursor is where your
    typing point is in your buffer.  Buffer is what you see in the
    emacs window as you edit.  The buffer will be lost unless you save
    it to a file at some point.  See later on how to save a buffer
    into a file.

    C-f [ Try control-f meaning holding down the control key and the f
        key simultaneously - this will move your cursor one
        character 'f'orward.  Uppercase F or lowercase f will do the
        same.  The same is the case with other commands below. ]

    C-b [ One character 'b'ackward ]

    C-n [ To the 'n'ext line ]

    C-p [ To the 'p'revious line ]

    C-a [ To the beginning of the current line ]

    c-e [ To the 'e'nd of the current line ]

    mouse [ You can also move your mouse to a new location and move
        the cursor to that location by clicking on it ]

    C-x C-s [ Save the changes made so far in the buffer - try this
        occasionally to save incrementally so that you don't lose any
        editing you have done so far if system crashes in the middle
        of your editing session ]

    C-x C-w [ To save the current buffer into a new file.  When you
        try this command, it will ask for a new file name.  This is a
        way to create a new file.  You can copy a file this way too. ]

    C-x C-c [ Exit emacs permanently ]

    C-g [ Abort the current command, whatever command you happen to be
        executing. You will use this quite often if you are like
        me.  Sometimes, you are in the middle of a command and you
        don't want to continue with the command or you are not sure
        exactly where you are.  Well, try C-g in such cases. ] 


    c-s [ Incremental search.  After you hit c-s type in the text that
          you want to search forward in the buffer.  Try this command,
          i.e., c-s repeatedly searching a text that occurs multiple
          times in the buffer.  In fact, as you start the command,
          emacs will highlight all the occurrences of the text that
          you are currenting searching in the current window. ]

    c-r [ Incremental search backward.  Similar to c-s except you are
          searching the text backward.  Again try repeatedly. ]

    M-< [ Go to the beginning of the buffer.  M- means the Esc key.
        This is useful when you are editing a large file. ]

    M-> [ Go to the end of the buffer. ]

    C-v [ Go to the next page - useful if you are editing a large file ]

    M-v [ Go to the previous page - again for a large file ]

    backspace [ To delete the character before the cursor ]

    C-d [ To delete the character on the cursor ]

    C-k [ To 'k'ill the current line into the kill buffer.  Try this
        four time consecutively.  After you try C-k consecutively
        more than once, if you move the cursor to a different location
        and try C-k again, you started a new kill buffer.  Experiment
        with C-k and C-y below a few times until you understand how
        they work. ]

    c-y [ To retrieve the content of the 'kill buffer'.  See the c-k
          command above.  In fact, if you want to move a series of
          lines to a different location of your buffer, try c-k to
          kill the text that you want to move, relocate the cursor to
          the target location, and try c-y (this is cut-and-paste).
          If you want to copy, you can try a similar thing except try
          c-y twice after you kill them: once in the current location
          and once more in the target location. ]

    C-x 2 [ To split the window into two (2), above and below. Try
          this recursively, that is, try this on a split window.  You
          can view multiple windows on a single screen - quite useful
          when you want to see multiple files on the same screen. ]

    C-x 3 [ To split the window into two, side by side ]

    C-x o [ To move the cursor to the 'o'ther buffer.  Try this
          repeatedly with multiple split windows. ]

    C-x 1 [ To close all the other split windows except the one where
          the cursor is located ]


    C-x C-f [ To bring a file into the editing session as a new buffer.
        So, you can have as many files open into your editing session as
        you want using this command.  In fact, bring in several for
        this exercise.  As you deal with existing file names, you will
        find automatic file name completion feature useful.  Emacs knows
        how to complete the file name so use the space bar to try to
        complete the file name whenever appropriate. ]

    C-x C-b [ To see all the buffers in the session in the other half
        of the window.  At this point, if you want to get to another
        buffer, use C-x o to ge to the buffer with the list and move
        the cursor to the buffer you want and simply type 'f'
        without the quotes on the line containing the buffer's
        name. When you list the buffers this way, the ones annotated
        with a * are the ones that have been modified but not yet
        saved.  You will most likely want to save them unless you
        want to lose them when you exit out of emacs.  In fact, if
        you try to exit emacs with modified buffers not saved, emacs
        will pester you to save them.  Try it. ]

    C-x b [ To move to another buffer without seeing the list of the
        buffers in the ohter half of the window.  This is useful if
        you already know the name of the buffer.  Or if you want to
        move between two buffers since the other one will be the
        default one that you can just accept. ]

    c-z [ Suspend emacs and return to the Unix prompt if you are using
          Unix.  Ignore this command if you are using Windows.  To
          continue the editing session with the suspended emacs, try
          the unix command 'fg' meaning foreground ]

    More... There are many many more commands that you will find useful.
    See a manual or at least the Quick Reference card (there is one on
    my website: http://www.cmc.edu/math/alee/). Or, search 'emacs
    commands' or 'emacs manual' using google in the Internet.

Go back and continue the rest of the Unix lab if you came from the Unix Lab.


Last modified . . .