3.4. Basic Commands

Learn by Experimenting!

For each of the commands in this section, take notes on what they do and then test them out in the NOTES.txt file in your cs1302/emacs directory (created in the previous section). If you get stuck, feel free to post your question along with a screenshot of what you’re seeing on the course Piazza page.

Try not to move your hands from the keyboard! If done properly, you shouldn’t need the mouse.

3.4.1. Open, Save, and Exit

All of these commands start with a C-x prefix.

Binding

Action

C-x C-c

Close/Exit Emacs

C-x C-f

Open/Find file

C-x C-s

Save current window / buffer

3.4.2. Cancel

To cancel a partially typed command (e.g., when you mistype), press C-g. You will likely use this command often. If you’re ever stuck in Emacs, this is a good place to start. It will clear out the partial commands and let you start over.

Binding

Action

C-g

Quit/cancel a partially typed command

3.4.4. Delete

In Emacs, you can forward and backward delete single characters and forward delete words.

Binding

Action

<DEL>

Backward delete a single character (here <DEL> refers to the backspace key)

C-d

Forward delete a single character (similar to the delete key)

M-d

Forward delete a word

3.4.5. Undo

If you want to undo some recent changes, then use C-_ where _ is the underscore. On most systems, this means holding the CTRL, SHIFT, and - keys at the same time. Using C-_ one undoes one batch of changes. If you hold it down, then it will keep undoing.

Binding

Action

C-x u

Undo one batch of changes–usually, one command worth

C-_

Undo one batch of changes–usually, one command worth

3.4.6. Selections and Copy / Paste

In Emacs, you can create or mark selections and even perform operations like copy and paste. When referring to copying and pasting, the term kill buffer refers to clipboard memory.

Use C-<SPC> to start your selection, then navigate to where you want the selection to end (navigation bindings can be used here). Once your text is selected, use one of the other key bindings to cut or copy the selection into the kill buffer. To paste, navigate to where you want to paste, then yank the text from the kill buffer.

Binding

Action

C-<SPC>

Set the mark point (i.e., start a selection)

C-k

Kill / Cut the current line (goes into kill buffer)

C-w

Cut selection (goes into kill buffer)

M-w

Copy selection (goes into kill buffer)

C-y

Paste / Yank (from kill buffer)

C-x h

Select all

When you edit source code using Emacs, you can usually use <TAB> to fix the indentation for a selected region of code – this assumes that things like parentheses and curly braces, if applicable, are properly closed.

3.4.7. Search and Replace

To search for text, you can use the incremental search commands C-s and C-r, which differ only in their search direction. After pressing C-s, Emacs will display an I-search: prompt at the bottom of the screen. When prompted, type the text you want to find, then press RET. If multiple occurrences are found, then repeated calls to C-s will cycle through the occurrences (C-g can be used to stop).

Binding

Action

C-s

Incremental search (forward direction)

C-r

Incremental search (backward direction)

M-%

Query replace

On most systems, M-% means holding the META, SHIFT, and 5 keys at the same time. Using M-% is interactive. Pay careful attention to the prompts provided by Emacs as you use it.

3.4.8. Splitting the Screen

Emacs makes it easy to split your screen, enabling you to move, copy, paste, etc. between multiple files with ease.

In Emacs, each window is called a buffer. When we talk about splitting, we talk about splitting the current buffer. When multiple buffers are displayed, Emacs places the text cursor in the current buffer and usually highlights the area containing the buffer’s name.

Binding

Action

C-x 1

Maximize current buffer

C-x 2

Split buffer horizontally (i.e., one stacked on top of the other)

C-x 3

Split buffer vertically (i.e., side-by-side)

C-x o

Navigate to another buffer

C-x C-b

List available buffers

To open a file in another buffer, navigate to that buffer then use C-x C-f to find and open your desired file. If you previously opened the file, then it’s likely still available. In that case, use C-x C-b to list the available buffers, navigate to the text for the file’s name, and then type RET to replace the current buffer area with the desired buffer.

When you save using C-x C-s, only the current buffer is saved.

3.5. Customize Emacs

Emacs can be heavily customized. If you look up or are provided with custom Emacs configurations, the usual place to put them is in your ~/.emacs file. You can create it if it does not exist.

Some suggested Emacs custom configurations can be found in the CS1302 Code Style Guide.