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
Save: Ctl-x Ctl-s saves the current changes to a file.
Save as: Ctl-x Ctl-w lets you save the file under a different name (like "Save As" in graphical editors).
Quit: Ctl-x Ctl-c - 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:
Move to next word: Meta-f or Meta-right arrow
Move to previous word: Meta-b or Meta-back arrow
Move to line beginning: Ctl-a
Move to line end: Ctl-e
Move back a sentence: Meta-a
Move forward a sentence: Meta-e
Jump to start of the file: Meta-< (less-than sign)
Jump to end of file: Meta-> (greater-than sign)
Jump to line N: Meta-g g N. For example, typing Meta-g g 123 would jump to line 123.
Page down: Ctl-v
Page up: Meta-v
Searching
Search is another great way to move your cursor.
Search file for text: Ctl-s - prompts you to enter text to search for.
Move cursor to next search match: Ctl-s
Move cursor to previous search match: Ctl-r
Find and replace text ahead of current cursor position: Meta-%
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).
Select all text: Ctl-x h
Ctl-SPACE to put a marker down, and move the cursor to select text
Cut current selection: Ctl-w ("kill")
Copy current selection: Meta-w
Paste: Ctl-y ("yank")
Cut text on current line to right of cursor: Ctl-k. 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.
Delete character under cursor: Ctl-d
Backspace to delete the character before the cursor.
Auto-indent current selection: TAB (To select all text, press Ctl-x h).
Undo: Ctl-x u
Cancel: Ctl-g (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:
- Eager to take advantage of more advanced Emacs features? Check out some additional tips from CS107 TA Ricardo Iglesias: click here
Ctl-his a command withinemacsthat opens the help menu where you can search for help for different commands.- Emacs Commands Reference Card
- Section 4 of this Stanford Unix Programming Tools document.
- Stanford Farmshare guide to emacs
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.