Guide to emacs

Written by Michael Chang, updated by Wesley Rodriguez

This guide covers the basics of using emacs to edit files. Learning these commands will really make things go a lot faster. There are many great emacs guides available on the web when you're ready for more advanced features.

In this guide, C-a means ctrl-a, while M-a means meta-a, where meta is generally the alt key.

Configuration

First you'll want to set up a .emacs file to give you some useful default settings. If you don't have one yet, view our sample .emacs or download ours into your account using wget http://cs107.stanford.edu/resources/sample_emacs -O ~/.emacs

One setting included in our sample .emacs file is setting a default color theme. Everyone has different preferences here, so if you don't like the theme chosen in the sample .emacs file, the best idea is to chose your own theme. To do so, open any file in emacs, and then type M-x customize-themes. This will take you to a page where you can choose your own theme. Once there, pick a theme by moving your cursor onto a theme and hitting Enter. Finally, move your cursor to Save Theme Settings and hit Enter to save. Open a .c file in emacs to see what the theme looks like.

It's also possible to have much more fine-grained control over the colors used to highlight code in emacs; look up syntax highlighting for emacs if you want to do this.

Learning Emacs

When first ramping up in emacs, I recommend keeping the emacs reference card open. It lists just about every command you'll need, so once you get used to reading from it it's super useful. We've also listed some of the most common commands below to help you get started.

Opening Emacs

emacs myprogram.c open the file myprogram.c if this file exists in your current directory, or will create myprogram.c if it does not already exist in your current directory.

emacs -nw myprogram.c open emacs in the same window (if it otherwise would open in a separate window)

C-x C-f open a new file if you are already in emacs

Save

C-x C-s save the current file

C-x C-w save to a different file ("Save As")

Quit

C-x C-c quit emacs

Move Cursor

Up/Down arrow keys will move your cursor one line up or down.

Left/Right arrow keys will move your cursor one character left or right

C-a move the cursor to the start of the line

C-e move the cursor to the end of the line

C-v page down

M-v page up

M-f or M-right arrow forward one word

M-b or M-left arrow back one word

M-< start of file

M-> end of file

M-g g go to line number

Search

C-s search the file. Press repeatedly to jump to next occurrence

M-% find and replace

Cut/copy/paste

C-k kill (cut) a line

C-y yank (paste) whatever's in the kill buffer

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

Other useful commands

C-x u undo

C-g cancel (if you're stuck in a command or prompt, pressing this, sometimes several times, should get you out)

Common 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.