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 a family of text editors, including GNU Emacs, which is the most popular version, and the one on the myth machines. This guide can get you up and running with emacs!

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 installation 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 and Ctl-e to move to the line beginning or end, respectively

Meta-a and Meta-e to move back or forward a sentence, respectively

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

How do I copy from outside of emacs into emacs?

Try your standard system copy/paste command to see if that works. On Windows, you need to configure special shortcuts because the system commands conflict with Emacs commands. To do this, right-click in the toolbar for your terminal window to open Properties, and follow the instructions here.

How do I copy from within emacs to outside of emacs?

The easiest way is to use the cat command to print the file contents to your terminal, and copy it from there by selecting it with your mouse.

When I open a file in emacs, it doesn't let me edit it, and at the bottom it prompts me with "Please type q, s, or p; or ? for help". How can I edit the file?

This means the last time you were editing the file, it closed improperly (eg. suddenly disconnected), so emacs locked the file - it shouldn't normally happen. You should enter "s" for "seize" to regain control of the file.

After editing a C file {file}.c in emacs, I see a new file called {file}.c~. What is that file?

This is a temp file that emacs created automatically. You can ignore it, or delete it.

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

Yes, you may use a different text editor, but we strongly recommend using emacs, as this is the editor that our teaching staff officially supports. 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. Learning a Unix-based editor is an essential skill!