Guide to emacs

Written by Sarah Spikes, updated by David Philipson

This guide covers the basics of using emacs to edit files. Emacs has been good to us but there are other good Unix text editors (vim, pico, gedit, and more). You'll find our staff is generally most familiar with the mainstream emacs or vim, but you're welcome to use any editor that gets the job done for you. If you're interested in another editor, some internet research will likely turn up useful resources.

What is Emacs? Why should I use it?

Emacs is a text editing program with a very wide and powerful set of features. In this document, we will only be talking about the basic text-editing functions, but people have written extensions to Emacs to, for example, allow it to send and receive email. As an old joke goes, "Emacs is an excellent operating system. All it's missing is a decent text editor."

Using Emacs is in many ways like using any text editor you are used to (such as Notepad on Windows), but with many unusual keyboard shortcuts. In fact, you could survive decently well by using it just like Notepad, navigating the document with arrow keys and clicking buttons to save and load. However, taking a bit of time to learn Emacs's unique shortcuts will make you much more efficient. Soon, you might feel that Notepad is the unwieldy one!

Opening Emacs

Emacs has two versions: the GUI version which runs in a separate window and has buttons and menus for common functions, and the text-based version which runs in your console window. The emacs GUI has several features that make using emacs as a first-time user much easier, but if you're on a slow connection, it can be agonizingly laggy. Further, if you use a non-windowing client like PuTTY, you have no choice but to use the text-based version.

Once you've decided which version you want to run, log in to your favorite cluster using whatever client you like. The easiest way to launch the program is to simply type "emacs" and hit enter, which will look like the following:


This will open the windowed version if it is available (again, it might be disabled depending on which client you are using for your connection). Note that this locks up your console as long as emacs is open (the console is "busy" running emacs). If you want to be able to continue using your terminal while using emacs, add an ampersand:


If you prefer to work in a console, then you will want to add the -nw flag ("no window"), e.g.

emacs -nw

Alternately, if you already know what file you want to edit, you can cd to the appropriate directory and pass the filename as a parameter to emacs as follows:

 emacs reassemble.c&

If you started without a filename, or you want to open a new file use the keyboard shortcut Ctrl-x Ctrl-f. You will see a prompt at the bottom of the window (this little section is called the mini-buffer) that says

Find file: ~/cs107/assign1/

Note that here I've cd'd into my ~/cs107/assign1 directory before opening emacs, so the path defaults to this. You can then type in (with tab-complete) the filename you want and press enter:

Find file: ~/cs107/assign1/reassemble.c

Emacs Basics

All keyboard shortcuts in Emacs are initiated by pressing either the Ctrl key or the "meta" key, along with some others. The meta key is Alt by default, but depending on your connection client Alt may do something else (for instance, on Windows you may find pressing Alt has the default Windows behavior of selecting the current window's menus). You can try fiddling with your connection client's settings to get Alt to go through correctly, or you can instead use Esc as the meta key. Throughout this guide, I will use the notation, for example, of C-a to mean "press Ctrl and a at the same time," and M-a to mean "press meta and a at the same time." This notation is pretty standard, and if you read guides to Emacs online you will most likely see the same notation.

If at any time you find yourself caught up in the middle of some Emacs command that you don't want, you can usually escape out of it by pressing C-g a few times. C-g is by convention an unbound keyboard combination, so pressing it should get you out of most situations without causing any problems.

By far, the biggest gain in Emacs is being able to easily move the cursor around the document without moving your hands away from the home row. Where in a more common text editor you would have to use awkwardly placed keys like Page Down, Home, or the arrow keys, in Emacs we can do all these things without any real break from our typing. This will actually save a huge amount of time: if you learn one thing from this document, it should be this.

Basic movement commands (learn these!):

C-f: move the cursor one space to the right (think "forward")

C-b: move the cursor one space to the left (think "back")

C-n: move the cursor one space down (think "next")

C-p: move the cursor one space up (think "previous")

C-a: move the cursor to the start of the line (think "a is the start of the alphabet")

C-e: move the cursor to the end of the line (think "end of line")

C-v: page down (think "v looks like a down arrow")

M-v: page up (think "well, it's not C-v . . . ")

Other useful movement commands:

M-f: forward one word

M-b: back one word

M-<: start of file

M->: end of file

M-g M-g: go to line number (brings up a prompt to enter a line number)

Save, Load, Quit

To save the current file, press C-x C-s (again, that means press Ctrl+x, and then Ctrl+s). To do a "Save As," instead press C-x C-w, and a prompt will appear to let you enter the path on which to save. To load a file, press C-x C-f ("find"), and a prompt will appear to let you select a file to open. To close Emacs, press C-x C-c ("close").


To undo, you have a few options, but my favorite is C-_ (that is, hold both Ctrl and Shift and press the - key). The way this command works is a little weird -- you don't undo then redo with separate keystrokes -- instead it undoes until there are no more changes to undo, then starts re-doing them. If you find you have undone too many changes, you can just do something else (like hitting one of the arrow keys) then undo again, but since the last change was undoing something, it redoes that change. If this explanation is too confusing... try reading more about undo in the emacs documentation. C-x u will undo as well. Note that if you close the editor, all recorded changes will be discarded.


One of the most difficult things to get used to in emacs is that all the keyboard shortcuts you've come to know and love in your native windows or mac environment are completely out the window. However, it is possible to get at least the familiar cut/copy/paste shortcuts to work as you expect. If you are using the GUI version, you can just go to the menu (you can mouse to it or use F10 and navigate with the arrow keys); under Options, you should see C-x/C-c/C-v Cut and Paste (CUA) as an option. If you enable it, you'll be able to select text and cut/copy/paste in the usual, expected way. Of course, if you want to be a hardcore emacs user, you'll learn the native shortcuts, described a little later. (disclaimer: there's nothing wrong with turning on CUA. Really.)

If you're in the text version, you can still activate CUA, again using F10 (or Esc-`) to get to the menu, then navigating the menu in a text format (a little trickier). However, it doesn't entirely do what you expect, so if you're in text mode I recommend just using emacs' native cut/copy/paste shortcuts, described below.

One simple way to copy/paste is to use the following shortcuts:

If you hit Ctrl-k multiple times in a row (with no other commands between), all the lines will be yanked (or pasted) when you hit Ctrl- y.

There are several other ways to achieve the same effects. One way is to select a region by setting a mark at the cursor's current location using Ctrl- space, then use the arrow keys to go to the other end of the region you want to cut and use Ctrl-w to kill that region. You can then use Ctrl-k to yank it.

Note: there are lots of other ways to copy/paste, feel free to google them on your own!


If you just want to find, use C-s ("search"), which will take your cursor into the mini-buffer, where you can start typing what you're looking for. If the first occurrence isn't the one you really wanted, you can then hit Ctrl-s again as many times as you wish. It will say "Failing I- Search" if the term either isn't there or it is higher in the document than your cursor. If you think the latter is true, hit C-s again and it will wrap around and start searching from the top. If you interrupt the search with another command or using the keyboard, you can start it again by hitting C-s twice (if you hit it only once, it will expect you to start typing something new).

If you want to do a find/replace, start by hitting M-%. You can then type in the string you want to find, then press enter, then the string you want to use to replace it and hit enter again. This should bring you to the first occurrence of your find string. Here you can either type 'y' to say you want to replace this occurrence, or 'n' to skip, and in either case it will take you to the next occurrence. You can also get out of find/replace by just hitting enter or an arrow key. There are some more options, type '?' if you want to see them. Once you've exited, if you want to do the same find/replace, hit M-% again and hit enter to use the default (this will be whatever you last did a find/replace for). Of course, you can type in a new pair if you want to look for something else.

Multiple Buffers

If you are going to be editing multiple files or using make/gdb within emacs (described in a later section), you may find that you want to have multiple buffers open, and may even want multiple ones displaying at the same time. If you just want multiple buffers you can switch between, just open up all the files you're interested in (remember you can do this with C-x C-f), then use C-x C-b to switch between them (you can just use C-x C-f every time, but if they are in different directories this could get annoying). If you don't remember what all you have open, you can just hit tab twice to see all the possible completions. You can also use C-x [arrow key] to essentially scroll through the open buffers. If you decide you are done with a particular buffer, you can kill or delete it (the file will still exist, you just won't have it open) by hitting C-x C-k then typing in the name (or if it's the one you're currently in, just hit enter).

Now, if you want to have multiple buffers showing at once, you can use C-x 2 to split horizontally (i.e. you will have one buffer on top of the other), and C-x 3 to split vertically (you will have two buffers side-by-side). If you want to go back to a single buffer, hit C-x 1 to retain the current buffer as the only one, or use C-x 0 to get rid of the buffer you're in (if you have more than 2 you will now have one fewer, vs having only 1 if you use Ctrl-x 1).

Running make/gdb wthin emacs

To compile your program from within emacs, you can use M-x compile and type in whatever args you want to pass to make and press enter. This will show you any compile errors in a split screen, and if you are in GUI mode you can click on the errors to see what lines they correspond to. If you are in text mode or just like keyboard shortcuts better, you can use C-x ` to do the same thing. If you want to learn more about running make in emacs, google it!

Similarly, you can run gdb in emacs with M-x gdb. This will open a new gdb buffer which you can then use as normal. If the program segfaults or hits a breakpoint you've set, it will bring up the code in another buffer and show you the line it crashed on. I haven't played with this much, but I found what looks like a useful intro to gdb within emacs.


If you want to make changes to your emacs configuration (i.e. set the background color to something other than white, default to having CUA turned on, etc.), you can edit your .emacs file, which can be found in your base directory, so the path will be ~/.emacs. As an example, you can add these lines to your .emacs file to turn on CUA mode by default (the last three are optional):

(cua-mode t)
(setq cua-auto-tabify-rectangles nil) ;; Don't tabify after rectangle commands
(transient-mark-mode 1) ;; No region when it is not highlighted
(setq cua-keep-region-after-copy t) ;; Standard Windows behavior

Disclaimer: I haven't actually used these, I just found them in the old version of this guide, so use at your own risk! And save a back-up of your .emacs file before you go messing with it in case you break it.

If you want more customizations, google is still your friend.

Frequently asked questions about emacs

How do I configure tab settings to my liking?

The .emacs file in your home directory allows you to configure your settings. The option default-tab-width and c-basic-offset dictate the number of spaces for tab and indent respectively (default is 8 for both). We also recommend that you set indent-tabs-mode to nil to substitute spaces for tabs. This ensures your file looks the same everywhere.