Some Useful EMACS Commands


To get into the text editor EMACS from the operating system, type emacs fname . If fname is a previously existing file, it will be brought into EMACS and you will see it on the screen. If fname does not already exist, it will be created. Whatever you type will be inserted to the left of the blinking cursor. Pressing the DELETE or the BACKSPACE key will erase the character to the left of the cursor. Move around with the arrows.

EMACS is largely controlled by sequences single keystroke commands. To let EMACS know that a sequence is meant to be a command and not something to be inserted into the text, you must either hold down the CONTROL key and press the command key, or strike the ESC (escape) key first and then press the command key.

In this handout, C- and then a letter means hold down the CONTROL key and press the letter. ESC- and then a letter means strike the ESC key and then press the letter. For example

C-x s Saves the file you are editing, erasing the existing version. Really want to? y for yes, n for no.
C-x C-s Saves the file with no prompting.
C-g Makes emacs stop whatever it is trying to do, like prompting you with several choices you are not interested in. This is how you back out of a command you wish you had not entered.
C-x C-w Save the file under a new name; you will be prompted for the new file name. You are now in a buffer with that name.
C-x C-c Exit EMACS, saving all buffers into their respective files. Really want to? Reply y for yes or n for no.

Moving Around

ESC-f Forward a word.
ESC-b Backward a word.
C-a Move to beginning of line.
C-e Move to end of line.
ESC-e Forward a sentence.
ESC-a Back a sentence.
C-v Forward a page.
ESC-v Backward a page.
ESC-> Move to end of file.
ESC-< Move to beginning of file.
C-x r SPC # Save position of point in register number # (SPC means space bar).
C-x r j # Jump to the position saved in register number #.


Search and Replace

C-s Incremental search-forward
C-r Incremental search reverse
ESC-% Search & replace. y=yes, n=no, !=yes from here on with no prompt, ESC=terminate search.

Deleting and inserting text

ESC-d Delete next word.
ESC-DEL Delete previous word.
C-k Kill (delete) to end of line; contents are in "kill" buffer (actually part of kill ring).
C-@ Set "mark". (Now move "point" (cursor) to other boundary of text you want to copy or cut.)
C-w Cuts text between point and mark, placing it in kill buffer.
ESC-w Copies text between point and mark, placing it in kill buffer.
C-y "Yank" (paste) contents of kill buffer. If several items have been deleted consecutively, (like with many C-k's) they're all there.
C-x r k Kill rectangle (think of point & mark at diagonal corners).
C-x r y Yank rectangle.
C-x r s # Copy region between point & mark into register number #.
C-x r i # Insert text from register number #.
C-x r r # Copy region rectangle into register number #. Insert with C-x r i #.
C-X i Insert file at cursor. EMACS will ask for file name.
C-x C-r Read file into current buffer, erasing contents.

Buffers

C-x C-f Find file. Copies file into a new buffer. You're there.
C-x b Select buffer (to move to).
C-x C-b List buffers (in another buffer called *Buffer List*).
C-x k Kill a buffer. Default is this one.
C-x 1 Close windows showing other buffers (that's a one).

Macros (Also see name-last-kbd-macro and insert-kbd-macro)

C-x ( Start defining macro.
C-x ) End defining macro.
C-x e Execute macro
.
Miscellaneous

ESC-# Before a command, executes the command # number of times.
C-x u Undo last action. Keep doing it to keep undoing.
C-g Abort what's going on.
C-z Temorarily suspend emacs. Get back with %emacs or fg.
ESC-! Shell command
ESC-$ Spell word.
C-l Redraw display.
C-h k (key) Help on command (key).
C-h i Run info, see entire emacs manual.
C-h a (expr) Command apropos on (expr).
C-h t Get into emacs tutorial.
C-h v var RET Print value & documentation on var, like C-h v fill-column.
C-x C-x Exchange point and mark. Effectively, go back to where you were.
ESC-u Make following word upper case.
ESC-l Make following word lower case.
C-x C-u Make region upper case.
C-x C-l Make region lower case.
ESC-q Fill current paragraph.
C-x f Set the fill column to current point.

Commands by Name (To execute, ESC-x command <Return>)

recover-file Recover autosave file #fname#
untabify Converts tabs to equivalent number of spaces, between point and mark. edt-emulation-on DEC VMS editor with keypad, more or less.
auto-fill-mode (toggles)
fill-region (Between point & mark).
text-mode
fortran-mode
replace-string
search-forward
line-number-mode
kill-some-buffers
name-last-kbd-macro Now that macro is a command!

insert-kbd-macro RET macroname RET Inserts Lisp code that defines the keyboard macro of the given name. Better be an empty buffer. If this code is inserted into (or loaded by) the .emacs file, it becomes permanent. I would say use sparingly.

global-set-key
local-set-key
enable-command This will modify the .emacs file.
what-line
kill-rectangle
yank-rectangle
doctor Psychotherapist. Free, non-judgemental.


This handout is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.