Basic Emacs Commands Lab

Created: January 24, 2008

Revised: August 20, 2009; August 28, 2018; and July 1, 2020

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 with the tools that you use as you program and emacs is one such tool.

1. Start an Emacs Session

Try one of the following two:

2. Simple Emacs Commands

Assuming that the cursor is at the beginning of your 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.

After you edit your file, you must save the changes. Otherwise, you will lose the changes that have made when you exit out of emacs. C-x C-s saves the current buffer. C-x C-c exits out of emacs. So, make a few changes and save it and start a new session with the file you saved and continue with the rest of the lab. This way at least you will know a complete lifecycle of an emacs session.

Now that you came back to the same file to edit more, try these commands.

    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. You've already tried this above. ]

    C-x C-c [ Exit emacs permanently. You've already tried this above also. ]

    C-g [ Abort the current command, whatever command you happen to be
        in the middle of. 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, sometimes
        more than once if needed. If you try too many times, it will
        beep. ] 

    C-f [ This will move your cursor one character 'f'orward ]

    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-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-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 currently 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 [ You already tried this above, but... 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 other 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 . . .