Skip to content

Instantly share code, notes, and snippets.

@jbritton
Last active October 15, 2017 16:48
Show Gist options
  • Save jbritton/ac9ba42802dc6795edf5532c47444f6d to your computer and use it in GitHub Desktop.
Save jbritton/ac9ba42802dc6795edf5532c47444f6d to your computer and use it in GitHub Desktop.
emacs-reference

Emacs Overview

Anatomy of Emacs

  • Menu Bar - top application menu bar containing common File, Edit, Options, Buffers, Tools related tasks
  • Tool Bar - quick tasks for file editing
  • Main Window - the main area, displaying the text editor
  • Mode Line - gives information about the current line, etc.
  • Mini Buffer - allows commands to be typed
  • Fringe - side gutter showing line wraps, etc.

Concepts

  • Windows - analagous to tabs in browser or other IDEs.
  • Frames - analagous to windows in browser or other IDEs.
  • Buffer - text editor, based on data structure on which Emacs operates, piece of text in memory. Buffers are not necessarily tied to files. They can be directories, emails, etc.
  • Key Sequences - key strokes that map to commands.
  • Deleting something is commonly referred to as "killing" it.

Keys in MacOSX

  • C is the control key. This is control on a mac.
  • M is the meta key. This is alt on a mac.

Working in Emacs

Window Key Sequences

  • Split window below: C-x 2
  • Split window right: C-x 3
  • Close split windows: C-x 1

Frames

  • Open new frame: C-x 5 2
  • Close frame: C-x 5 0
  • Open file: C-x C-f
  • Close frame: C-x 5 0

Files

  • Open file (open buffer): C-x C-f
  • Close file (kill buffer): C-x k
  • Save file: C-x C-s

Directories using dired

  • Directories are viewed using dired (directory editor)
  • List contents of a directory: C-x d
  • Finding files by name: M-x find-name-dired <FILE_PATTERN>
  • Within dired buffer
  • Change sort order: s
  • Navigate directories and files: < and >
  • Insert directory into current dired buffer: i
  • Go to parent directory: ^
  • Open a file or directory: RET or o

General Key Sequences

  • Quit command input: C-g
  • Help command: C-h k
  • Quit Emacs: C-x C-c
  • Meta Command: M-x => M-x: split-window-vertically

Navigation Key Sequences

  • Scroll down: C-v
  • Scroll up: M-v
  • Scroll other window down: C-M-v
  • Scroll other window up: C-M-SHIFT-v
  • Scroll to the end of file: M-SHIFT-.
  • Scroll to beginning of file: M-SHIFT-,
  • Move next line: C-n
  • Move previous line: C-p
  • Move forward: C-f
  • Move backward: C-b
  • Move end of line: C-e
  • Move beginning of line: C-a
  • Center line with uniform space above/below: C-l
  • Move forward by word: M-f
  • Move backward by word: M-b
  • Move end of sentence: M-e
  • Move beginning of sentence: M-a
  • Move start of paragraph: M-SHIFT-[
  • Move end of paragraph: M-SHIFT-]
  • Move down 10 lines: C-u 10 C-n OR M-10 C-n
  • Move forward 5 words: C-u 5 M-f OR M-5 M-f

Basic Editing Key Sequences

  • Undo: C-/
  • Delete next key: C-d
  • Delete next word: M-d
  • Delete to the end of the line: C-k
  • Delete to the end of the sentence: M-k
  • Delete to the start of the sentence: C-x backspace
  • Delete the entire line: C-SHIFT-backspace
  • Delete to a specified character: M-z
  • Capitalize a word: M-c
  • Lowercase a word: M-l
  • Uppercase a word: M-u
  • Swap letters: C-t
  • Swap words: M-t
  • Swap lines: C-x C-t
  • Wrap lines (aka text filling): M-q
  • Text completion: M-/
  • Select multiple lines: C-space C-n
  • Operate on multiple lines: M-x
    • Align a map of properties: M-x align-regexp :
    • Find and replace: M-x replace-string <REPLACE_TEXT>
    • Find and replace regex: M-x replace-regexp <REG_EX>
    • Find and confirm replace: M-x query-replace <REPLACE_TEXT>

Regions

  • Point: number that represents the position of the cursor in the buffer
  • Mark: number that represents a starting point of a selection in the buffer
  • Region: the selected part of the buffer between the mark and the point
  • Set the mark: C-space
  • Move the point to select text between the point and the mark: C-n, C-e, etc.
  • Switch the mark and the point: C-x C-x
  • Select a code block: C-M-H
  • Undos can be restricted to a selection: C-/
  • Narrowing to a block of code, hides the rest: C-x n n
  • Widening to whole file: C-x n w
  • Cutting text (aka killing): C-w
  • Pasting text (aka yanking): C-y
  • Cycling through cut history (aka kill ring): C-y M-y
  • Copying text: M-w
  • Appending to a previous kill: C-M-w C-w
  • Cycling through mark ring: C-u C-space

Buffers

  • Buffers are the data structure used to store text
  • File contents are read into the buffer, editing occurs in the buffer, and it is written back to the file
  • Switching buffers: C-x b <BUFFER_NAME>
  • Killing buffers: C-x k <BUFFER_NAME>
  • Listing buffers: C-x C-b
    • Columns can be toggled/sorted
    • Shift-T displays only buffers tied to files
    • Shift-G refreshes buffers
    • S marks a buffer for save
    • D marks a buffer for delete
    • U unmarks a buffer
    • X executes the operations
    • M can be used to mark buffers to open, search, process, etc. multiple files

Mode Line Overview:

-:*** example.txt Bot(10,25) (Text)

  • First '-'in '-:---" indicates the type of file encoding
  • Second '-' in '-:---" indicates if buffer is write-enabled or read-only
  • Third '-' in '-:---" indicates if buffer has been modified (ie has unsaved changes)
  • 'example.txt' is the name of the buffer, this can be clicked to cycle through open buffers
  • 'Bot' in this example indicates the position in the file (ie. top, middle %, bottom)
  • (10,25) indicates the point in the buffer in rows and columns (ie. row 10, column 25)
  • (Text) indicates the mode of the environment.

Modes

  • Switching modes: M-x emacs-lisp-mode
  • Modes are often associated with the buffer based on the file's extension
  • Renaming the buffer: M-x rename-buffer <NEW_BUFFER_NAME>
  • Note that renaming the buffer does not rename the file

Registers:

  • Storage bins which can hold useful information (buffer positions, numbers, bits of text, etc.)
  • Registers named by A-Z,a-z,0-9 (62 total)
  • Saving a position to a register: C-x r space <REGISTER_ID>
  • Recalling a position from register: C-x r j <REGISTER_ID>
  • Saving text to a register: C-x r s <REGISTER_ID>
  • Recalling text from a register: C-x r i <REGISTER_ID>
  • Saving window configuration to register: C-x r w <REGISTER_ID>
  • Recalling window configuration from register: C-x r j <REGISTER_ID>
  • Saving a bookmark: C-x r m <REGISTER_ID>
  • Recalling a bookmark: C-x r l

Minor Modes:

  • Utilities or functionality which improve experience while editing
  • 2 kinds of minor modes: local * active for current buffer, global * active for all buffers
  • Toggle toolbar: M-x tool-bar-mode
  • Toggle line number: M-x line-number-mode
  • Toggle column number: M-x column-number-mode
  • Toggle scrollbar: M-x scroll-bar-mode
  • Toggle completion: M-x completion-list-mode
  • Toggle autosave: M-x autosave-mode
  • Toggle syntax-highlighting: M-x font-lock-mode, global-font-lock-mode
  • Toggle line numbers: M-x linux-mode
  • Toggle line highlight: M-x hl-line-mode

Customizations:

  • emacs is written in elisp, is dialect of a lisp
  • Executing commands: C-x C-e OR C-j
  • Set the background color: (set-background-color "white")
  • Set the foreground color: (set-foreground-color "black")
  • emacs startup files: ~/.emacs ~/.emacs.el ~/.emacs.d/init.el

.emacs file

Setting options:

(set-background-color "black")
(set-foreground-color "white")
(setq visible-bell t)
(tool-bar-mode -1)
(show-paren-mode 1)

Macros

  • Macros enables the recording and playback of keystrokes for the purpose of automating tasks
  • Macros can be bound to key sequences
  • Macros can be loaded on startup within the emacs init scripts
  • Start recording: C-x (
  • Stop recording: C-x )
  • Run the macro: C-x e

Writing Clojure in Emacs

Setup

Essential Clojure Editing Commands

See the CIDER docs: https://cider.readthedocs.io/en/latest/interactive_programming/

  • Starting the REPL: M-x cider-jack-in
  • Evaluate a single line: C-x C-e
  • Evaluate an expression and print result on the next line: C-u C-x C-e
  • Set the REPL's namespace to that of the current file: C-c M-n
  • Cycling command history in REPL: C-↑ and C-↓
  • Compile current file: C-c C-k
  • Displaying docs for function under point: C-c C-d C-d
  • Search docs for arbitrary text: C-c C-d C-a
  • Navigate to source code under point: M-.
  • Return to current buffer after viewing source: M-,
  • Closing docs: q
  • Switching between windows: C-x o
  • Closing stack traces: q

Paredit

Working with Packages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment