The Emacs Editor

Written by Michael Chang, Chris Gregg and Julie Zelenski, with modifications by Wesley Rodriguez and Nick Troccoli

Click here for a walkthrough video.

Click here for an Emacs reference card.

emacs is the command-line text editor used in CS107 to work on projects. It is a text editor that was modified by Richard Stallman (also see Richard Stallman on Wikipedia, who has an interesting outlook on life. This guide can get you up and running with emacs!

Getting Started

Before you start using emacs, you'll want to configure it to use the CS107 default preferences; this sets up the editor to do things like use mouse controls, display line numbers, standardize indentation, etc. To do this, execute the following command after logging into myth:

wget https://cs107.stanford.edu/resources/sample_emacs -O ~/.emacs

emacs looks for a special .emacs file on your system to know what preferences you would like, and this command downloads our pre-made .emacs file and puts it on your system. If you don't do this, you won't be able to use some of the shortcuts and commands highlighted in lecture and the guides.

Bonus: Custom Themes

If you're interested, you can further customize the default color theme. If you don't like the theme chosen in our configuration, open any file in emacs, and then type M-x customize-themes (M means the "Meta" key, which is discussed later on). 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 new file in emacs to see what the theme looks like.

Overview

Emacs works similarly to other editors you might have used; it lets you enter and edit text, and has certain keyboard shortcuts to perform common commands. The two keys it uses for these shortcuts are Control and Meta (which is Alt, or Option on a Mac).

Before continuing: if you're using a Mac, or a Windows computer with SecureCRT, make sure you have followed the instructions to configure your Meta key, as described in the logging-in guide. This will ensure that your Meta key is properly set to be Option (Mac) or Alt (Windows). Otherwise, some of the keyboard shortcuts below will not work!

Opening emacs

To open a file in emacs (or create a new one if a file with this name does not exist):

$ emacs filename

If emacs is already open and you would like to edit a different file, use the Ctl-x Ctl-f command. Again, if this file exists in your current directory it will open it, or it will create it if it does not already exist in your current directory.

You can open multiple files in emacs side-by-side (e.g. to copy-paste between them) by specifying multiple filenames when you open emacs:

emacs file1.c file2.c

Saving and Quitting emacs

Ctl-x Ctl-s save the current changes to a file. Ctl-x Ctl-w lets you save the file under a different name (like "Save As" in graphical editors).

Ctl-x Ctl-c quits Emacs. If you have unsaved changes, you will be asked whether or not you'd like to save your changes before quitting.

Navigating A File

You can use the mouse to navigate a file as you might expect in other graphical text editors. Specifically, you can click to position the cursor, and use the mouse to scroll through the file. You can also use the arrow keys to navigate the file if you would prefer.

There are also several additional keyboard shortcuts to navigate within a file:

Meta-f or Meta-right arrow move to the next word

Meta-b or Meta-back arrow move to the previous word

Ctl-a or Meta-a move to the beginning of the line

Ctl-e or Meta-e move to the end of the line

Meta-g g [NUMBER] jump to a line number. For example, typing Meta-g g 123 would jump to line 123.

Ctl-v page down

Meta-v page up

Meta-< (less-than sign) jump to the start of the file

Meta-> (greater-than sign) jump to the end of the file

Searching

Search is another great way to move your cursor.

Ctl-s searches the file, and prompts you to enter text to search for.

Ctl-s move the cursor to the next search match

Ctl-r move the cursor to the previous search match

Meta-% find and replace text ahead of the current cursor position

Editing Text

You can type as normal to insert or delete text wherever the cursor is in the document. There are also additional shortcuts below for cut/copy/paste and selecting/deleting text:

Click and drag with the mouse to highlight text. (Note: the highlighting text feature will not highlight the text you have chosen until after you have released the mouse).

Ctl-SPACE to put a marker down, and move the cursor to select text

Ctl-w to cut ("kill") the current selection Meta-w to copy the current selection Ctl-y to paste ("yank") whatever is in the copy-paste buffer

Ctl-k cut ("kill") the text on the current line to the right of the cursor

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

Ctl-d to delete the character under the cursor. Backspace to delete the character before the cursor.

Undo/Redo

Ctl-x u undo the last action

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

More Resources

The best way to get familiar with emacs is to just start using it to edit files - type something, anything! Over time, you'll become more comfortable with the standard commands, and pick up more advanced ones. If you are looking for more references for how to use emacs, check out the following additional resources:

Frequently Asked Questions

Do I have to use emacs? Can I use another text editor?

While there are a variety of text editors available, both command-line editors and GUI editors, emacs is the editor we choose to work on projects in CS107. It works similarly to other text editors you might have already used, and because you are always editing your files on the myth machines via ssh, it means that there is less risk of you losing your work. It is also easy to learn other editors that you may be interested in once you learn emacs.

While we won't prevent you from using a different editor if you do not want to use emacs, note that we only officially support using emacs in CS107 and can't answer questions about other editors or configurations. In particular, we do not recommend using a local editor such as Sublime Text to edit remote files. This is because of risks associated with missed updates, weird synchronization issues, dangerous data loss, and more, which we have seen cause students to lose work. Additionally, learning a Unix-based editor is an essential skill, as a local editor setup may not always be available or appropriate.