Table of contents
What is Emacs?
Emacs is a free text editor designed for programmers. With Emacs, you can edit code in common programming languages with context sensitive indentation and layouts. The following basic guide to Emacs can help you get started with this versatile program.
Basic keystrokes
Control keys
Hold 'Contol' (Ctrl) key down while pressing another key. Noted as 'C-'. For example: C-a (control-a goes to beginning of the line.)
Meta keys
Press the 'Escape' (Esc) key down and release, then press another key. Noted as 'M-'. For example: M-> (Esc-> goes to end of file (EOF).) The Meta key can also be used to prefix a command with the number of times you wish that command executed. See examples for C-f and C-b below.
General terms
Point - Where the cursor currently is positioned in the editing session; usually where the cursor is.
Mark - Placeholder for a position (point) in the file; many commands affect the region between the mark and the point.
Region - The area between the point and the mark (or between the mark & the point).
TOF - Top of file.
EOF - End of file.
RET - Return (or Enter) key.
Basic editing
Moving cursor
- Set mark: C-x
- Move cursor forward (non-destructive): C-f (or right arrow on keypad)
Can be modified by preceding with M-N, where N is an integer. Example: M-5 C-f moves the cursor 5 spaces forward. - Move curser backward (non-destructive): C-b (or left arrow on keypad)
Can be modified by preceding with M-N, where N is an integer. Example: M-5 C-b moves the cursor 5 spaces backward. - Jump to beginning of line: C-a
- Jump to end of line (EOL): C-e
- Move down one line: C-n
- Move up one line: C-p
- Move down one screen: C-v
Can be modified by preceding with M-N, where N is an integer. Example: M-5 C-v moves 5 screens forward. - Move up one screen: M-v
Can be modified by preceding with M-N, where N is an integer. Example: M-5 M-v moves 5 screens backward. - Jump to EOF: M-> (greater than)
- Jump to TOF: M-< (less than)
- Jump to specific line: M-x goto-line (or M-g g) - You will prompted for the line number to which to jump.
- Recenter: C-l (that's ell, not one) This will redraw the screen, placing the point at the mid-way point of the display.
- Save current editing session: C-x C-s - Saves to current filename (only).
- Write current buffer to file: C-x C-w [ filename | RET ]
You may specify any filename you wish. If you press RET without entering any filename, the current filename will be assumed.
Search
- Incremental search forward: C-s - Begins searching forward from the point as soon as you type the first character of the search string.
- Incremental search backward: C-r - Begins searching backward from the point as soon as you type the first character of the search string.
Modify text
- Undo: C-_ (or C-x u or M-x undo) - This will undo the last change made to the editing buffer. Successive undo commands will undo changes further back in the session.
- Kill line: C-k - Kills from point to the end of the current line.
- Kill region: C-w - Kills from point to mark (or from mark to point).
- Kill ring: Every time a chunk of text is killed, it's placed in a linked list called the kill ring. When you yank text, you yank from the kill ring/list the most recently killed
chunk. - Yank last killed text: C-y - Yanks back last chunk of killed text.
- Replace yanked text with earlier batch of killed text: M-y - This goes back through the kill ring retrieving previously killed chunks of text. Successive M-y commands will continue to replace the previously yanked text with the next chunk from the kill ring.
- Replace string: M-x replace-string - Finds all occurances of string and changes it to newstring from the current point to EOF.
Example: M-x replace-string RETstringRETnewstringRET - Query replace: M-x query-replace (or M-x %)- Finds all occurances of string and changes it to newstring, from the current point to EOF. For each potential change, requires a "yes (y)" or "no (n)" response.
Example: M-x query-replace RETstringRETnewstringRET - Replace regular expression: M-x replace-regexp - Finds all occurances of regexp and changes it to newstring, from the current point to EOF.
Example: M-x replace-regexp RETregexpRETstringRET - Query replace regexp: M-x query-replace-regexp - Finds all occurances of regexp and changes it to newstring, from the current point to EOF. For each potential change, requires a "yes (y)" or "no (n)" response.
Example: M-x query-replace-regexp RETregexpRETnewstringRET - Kill rectangle: Delete a rectangular area of the file. Set point, move mark, then type C-x r k Deletes the rectangle with point and mark as the opposite diagonal corners.
- Yank rectangle: C-x r y - Retrieves a previously killed rectangle. Any text within the border of where the rectangle will be retreved will be pushed aside (not overwritten).
Window manipulation
- Divide screen horizontally: C-x 2.
- Divide screen vertically: C-x 3 - Very handy when comparing two versions of the same program.
- Find (get) file in other window: C-x 4 f (prompts for file name).
- Go to other window: C-x o (that's an "oh").
- Maximize current window: C-x 1 (that's a "one").
Session manipulation
Display buffer menu: M-x buffer-menu (navigate with arrow keys; select buffer with Enter key)
Case conversion
- Convert region to lower case: C-x C-l.
- Convert region to upper case: C-x C-u.
Quitting Emacs
C-x C-c - If buffer has been modified since the last save, you will be prompted to write to the filename prior to exiting.
Resources
For complete guide, see Emacs Text Editor.