paulgorman.org/technical

Emacs

I’m a vim guy, but sometimes I need to know about Emacs.

C-x C-c    Exit Emacs
C-g        Abort command
M-x foo    Execute command foo

C-x prefixes many Emacs global and default commands. C-c prefixes user-defined or plug-in commands.

Misc:

C-h                  Help
C-x C-s              Write current buffer/file
C-x o                Switch to other pane/window
M-x c-mode           Enable C programming mode
M-x xterm-mouse-mode Enable mouse in terminal
M-x describe-mode    Show active major and minor modes

Configuration

Traditionally, file ~/.emacs is used as the init file, although Emacs also looks at ~/.emacs.el, ~/.emacs.d/init.el, ~/.config/emacs/init.el, or other locations.

Help

C-h t            Tutorial
C-h i            Manual
C-h a            Search help
C-h c command    Explain command, like C-h c C-x C-f
C-h k key        What command is mapped to key?
C-h m            Describe current major and minor modes of current buffer
C-h e            Show log of echo area

Movement

C-n    Line forward
C-p    Line back
M-f    Word forward
M-b    Word back
C-f    Character forward
C-b    Character back
C-a    Line start
C-e    Line end
M-a    First non-white char of line
M-a    Sentence start
M-e    Sentence end
C-v    Screen forward
M-v    Screen back
C-l    Align view with cursor centered (hit again and again for view aligned top, bottom)
M-<    Start of document
M->    End of document
C-M-f  Forward to matching bracket
C-M-b  Backward to matching bracket

Notice the parallel between C-f and C-b on the one hand, and M-f and M-b on the other hand. Very often Meta characters are used for operations related to the units defined by language (words, sentences, paragraphs), while Control characters operate on basic units that are independent of what you are editing (characters, lines, etc).

Repetition

M-number command M-6 C-f Forward six words

Editing

C-x C-f    Open file
C-x C-s    Save file/buffer
C-x C-w    Save As
C-/        Undo
C-_        Undo (*not* the same as C-- !)
C-x u      Undo
C-SPACE    Start visual selection
C-d        Kill char under cursor
M-d        Kill word forward
M-BACKSPACE  Kill word backwards
C-k        Kill to end of line
C-y        Insert most recent entry from kill ring
C-nth C-y  Insert nth entry in kill ring (like C-2 Cy for second entry)
M-;        Comment/uncomment code
M-/        Auto-complete word
C-M-\      Indent selection
C-j        New line (at cursor) and indent

Kill cuts/deletes content and puts it in the kill ring. Yank gets content from the kill ring and inserts/pastes it.

Marks, Kill, Yank

C-SPACE    Add start mark
C-w        Kill marked
C-y        Yank/paste most recent kill ring entry
M-w        Save to kill ring, but don't delete
C-y M-y    Cycle kill ring 

Buffers

C-x right/left arrow    Next/previous buffer
C-x k                   Kill buffer
C-x b                   Select buffer
M-x load-file           Evaluate Elisp file (to reload ~/.emacs, for example)

Frames

C-x o    Switch frame
C-x 2    Split over-under
C-x 3    Split side-by-side
C-x 0    Close focused frame
C-x 1    Close all frames except focused one

Modes

Each buffer has only one Major Mode, but can have multiple minor modes.

Emacs & Slime

Slime is an add-on mode for Emacs development in Lisp.

To get started, add this to ~/.emacs:

(add-to-list 'load-path "/the/path/to/slime")
(require 'slime)
(add-hook 'lisp-mode-hook (lambda () (slime-mode t)))
(add-hook 'inferior-lisp-mode-hook (lambda () (inferior-slime-mode t)))
;; Optionally, specify the lisp program you are using. Default is "lisp"
(etq inferior-lisp-program "sbcl")

Then, in Emacs, do M-x slime.