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 are seeing on the course Piazza page.

Try not to move your hands from the keyboard. If done properly, you will not 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 buffer

3.4.2. Cancel

To cancel a partially typed command (such as when you mistype a key chord), press C-g. You will likely use this command often. If you are ever stuck in Emacs, C-g is a good place to start. It will cancel the partial command and let you return to editing.

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, use C-x u or C-/, the latter of which can be held down to undo multiple changes. If you undo too much, type C-g to quit undoing, then use C-x u or C-/ to undo in the other direction (i.e., undo your undos).

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 perform operations like copy and paste. In Emacs terminology, the clipboard is called the kill buffer (or kill ring).

Use C-<SPC> to set the mark point at the start of 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 key bindings below to cut or copy the selection into the kill buffer. To paste, navigate to the desired location and yank the text from the kill buffer.

Binding

Action

C-<SPC>

Set the mark point (start a selection)

C-k

Kill / cut to the end of the line (goes to kill buffer)

C-w

Cut selection (goes to kill buffer)

M-w

Copy selection (goes to kill buffer)

C-y

Paste / yank (from kill buffer)

C-x h

Select all

When editing source code in Emacs, you can select a region and press <TAB> to fix indentation automatically (assuming parentheses and braces are properly matched).

3.4.7. Search and Replace

To search for text, use the incremental search commands C-s and C-r, which differ only in search direction. After pressing C-s, Emacs will display an I-search: prompt in the minibuffer at the bottom of the screen. Type the text you want to find, and Emacs will jump to matches in real time. Repeated presses of C-s cycle through subsequent occurrences, and C-g or RET exits search mode.

Binding

Action

C-s

Incremental search (forward direction)

C-r

Incremental search (backward direction)

M-%

Query replace

On most keyboards, M-% means holding META, SHIFT, and 5 simultaneously. When using query-replace, Emacs prompts for the search pattern and replacement text, then pauses at each occurrence with interactive options:

  • y: Replace the current match and move to the next.

  • n: Skip the current match without replacing.

  • !: Replace all remaining matches without prompting.

  • q or RET: Exit query-replace.

3.4.8. Splitting the Screen (Windows and Buffers)

Emacs makes it easy to split your display, enabling you to view and edit multiple files simultaneously.

In Emacs terminology:

  • A buffer is the in-memory object holding the contents of a file.

  • A window is a viewport on screen displaying a buffer.

When you split the screen, you create multiple windows viewing your buffers. Emacs places the cursor in the active window and highlights the active modeline displaying the buffer name.

Binding

Action

C-x 1

Close other windows (maximize current window)

C-x 2

Split window horizontally (stacked top and bottom)

C-x 3

Split window vertically (side-by-side)

C-x o

Switch cursor focus to the other window

C-x C-b

List available buffers

To open a file in another window, switch focus to that window using C-x o, then use C-x C-f to open the desired file. If the file was previously opened, press C-x C-b to list buffers, navigate to the buffer name, and press RET to display it.

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

3.4.9. Customize Emacs

Emacs can be heavily customized. If you look up or are provided with custom Emacs configurations, the standard location to place configuration code is in your ~/.emacs file. You can create this file in your home directory if it does not already exist.

Suggested Emacs configurations for Java development are available in the CSCI 1302 Code Style Guide.