emacs

Table of Contents

1 Help

  • C-h i, M-x info-apropos for info exploration
  • C-h d: apropos documentation
  • C-h a: apropos command

2 Window Management

  • C-x 4: working with other windows
  • C-x 5: same feature for frame

3 Bookmarks & Registers

3.1 Bookmark Keybinding

Key Binding Purpose
C-x r m Set a bookmark
C-x r l List bookmarks
C-x r b Jump to bookmark

3.2 Register Keybinding

See C-x r C-h

4 Movement

  • M-m: Moves to the first non-whitespace char on this line.

4.1 Moving by Balanced Expressions

Key Binding Purpose
C-M-f Forward
C-M-b Backward
C-M-d Down into a list
C-M-u Up out of a list
C-M-n Forward to the next list
C-M-p Backward to the previous list

4.2 Moving by Paragraph

Key Binding Purpose
M-} Move forward to end of paragraph
M-{ Move backward to start of paragraph
M-a Move to beginning of sentence
M-e Move to end of sentence

4.3 Moving by defun

Key Binding Purpose
C-M-a Move to beginning of defun
C-M-e Move to end of defun

4.4 Scrolling

Key Binding Purpose
C-M-v Scroll down the other window
C-M– C-M-v Scroll up the other window
  • C-u - same as C-M--

4.5 Go to

Key Binding Purpose
M-g M-g Go to line
M-g TAB Go to column
M-g c Go to character position

5 Marking

Key Binding Purpose
C-x h Marks the whole buffer
C-M-h Marks the next defun
M-@ Marks the next word
C-M-<SPC> Marks the next s-expression
C-M– C-M-<SPC> Marks the previouss s-expression

6 Searching

6.1 /occur/(M-s o)

6.1.1 Show Context Line

list-matching-lines-default-context-lines

6.1.2 multi-occur

for multiple buffers

  • multi-occur-in-matching-buffers: match with regex

6.1.3 Edit Matches

Type e to entry editable mode. Type C-c C-c to commit the changes. The feature is especially great for keyboard macros and search & replace.

6.2 Query & Replace

Key Binding Purpose
C-M-% Query regexp search and replace
M-% Query search and replace

6.3 Emacs Regexp Engine

  • These constructs require backslashes or Emacs will treat

them like literal characters: \|, \(, \), \{, \}

  • no \d: use [0-9] or [:digit:]

6.3.1 Emacs-only features

Constructs Description
\<, \> Matches beginning and end of word
\_<, \_> Matches beginning and end of symbol
\scode Matches any character whose syntax table code is code
\Scode Matches any character whose syntax table code is not code
\1 to \9 Inserts text from group \N
\? Prompts for text input from user. Useful
\# Inserts a number incremented from 0
\& Inserts whole match string
,(form ...) Call an elisp form
  • Some syntax classes:
    • Whitespace characters -
    • Word constituents w
    • Symbol constituents _
    • Punctuation characters .
    • String characters "
  • \s-: to search all whitespace chars
  • Examples of calling elisp form: \,(upcase \N), \,(format "%.2f" \#N)

6.4 Counting

Commands: count-lines-region, count-matches, count-words, count-words-region(M-=)

6.5 Very Useful Keybinding

Key Binding Purpose
M-g M-n next "error"
M-g M-p previous "error"
C-x = what-cursor-position

6.6 Highlighters

prefix key M-s h

7 Editing

7.1 Kill & Yank

Key Binding Purpose
M-d Kill word
C-k Kill rest of line
M-k Kill sentence
C-M-k Kill s-expression
C-w Kill active region
C-S-<backspace> Kill current line
M-w Copy to kill ring
C-y Yank last kill
M-y Cycle through kill ring
C-M-w Append kill
M-z Kill to some char

7.2 Transposing

Key Binding Purpose
C-t Transpose chars
M-t Transpose words
C-M-t Transpose s-expressions
C-x C-t Transpose lines

7.3 Filling

Key Binding Purpose
M-q Refills the paragraph
C-x f Sets the fill column width
C-x . Sets the fill prefix
M-x auto-fill-mode Toggles auto-filling

7.4 Commenting

Key Binding Purpose
M-; Comment or uncomment DWIM(Do What I Mean)
C-x C-; Comment or uncomment line
M-x comment-box Comments the region but as a box
C-M-j Inserts new line and continues with comment

7.4.1 Mode Comment Style

for major mode authors

Variable Purpose
comment-style Style of comment to use
comment-styles alist of available comment styles
comment-start Character(s) to mark start of comment
comment-end Character(s) to mark end of comment
comment-padding Padding used between comment character(s) and the text

7.5 Changing Case

Key Binding Purpose
C-x C-u Uppercases the region
C-x C-l Lowercases the region
M-x upcase-initials-region Capitalizes the region
M-c Capitalizes the next word
M-u Uppercases the next word
M-l Lowercases the next word
  • maintain the tempo: M-- M-2 M-u

7.6 Text Manipulation

  • Delete duplicates: M-x delete-duplicate-lines. Use C-u C-u C-u not to delete adjacent

7.6.1 Filter Lines

  • flush-lines: Flushes (deletes) all lines in a region that match a pattern
  • keep-lines: Keeps all lines in a region that match a pattern and removes all non-matches

7.6.2 Join&Split Lines

Key Binding Description
C-o Inserts a blank line after point
C-x C-o Deletes all blank lines after point
C-M-o Splits a line after point, keeping the indentation(rarely used)
M-^ Joins the line the point is on with the one above

7.6.3 Manage Whitespaces

Key Binding Description
M-\\ Delete all/N spaces and tabs around point
M-x cycle-spacing As above but cycles through all but one, all, and undo
M-x whitespace-mode Highlights all whitespace characters
M-x whitespace-newline-mode Displays newline characters with a $
M-x whitespace-toggle-options Displays a toggle menu of allwhitespace-mode options
  • other commands: whitespace-report, whitespace-clean-up

7.6.4 Text Expansion

  • To alter how Hippie Expand expands text: check variable hippie-expand-try-functions-list
  • find-library: hippie-exp

7.6.5 Indenting

  • Variable indent-tabs-mode: use TAB or whitespaces
  • Variable tab-width: controls how many characters of spacing each tab uses
Key and Command Description
M-x tab-to-tab-stop Inserts spaces or tabs to next tab stop
M-x edit-tab-stops Edits tab stops
C-M-\ Indents using major mode's region indent command
C-x TAB Rigidly indents(useful with C-u)

7.6.6 Sorting & Aligning

  • M-x sort-xxx: call it with a universal argument it will reverse the sort order
  • M-x align, M-x align-current, C-x \: organized by major mode. example for C-x \ =

    UNIVERSE_ANSWER_CONST = 42
    UNIVERSE_QUESTION     = "What is The Answer ..."
    

7.6.7 Spell Checking

Keys and Commands Description
M-$ Spell checks word at the point
M-x flyspell-prog-mode Highlights strings and doc strings in code
M-x flyspell-mode Minor mode that highlights spelling errors
M-x ispell-region Runs spell check on region
C-. Auto corrects the word at point

7.6.8 Insert ASCII Control Code

C-q: try C-q RET, C-q ESC, C-q C-j, C-q TAB

7.7 Keyboard Macros

Key Binding Description
F3 Starts macro recording, or inserts counter value
F4 Stops macro recording or plays last macro
C-u F3 Starts recording but appends to the last macro
C-u F4 Plays the second macro in the ring
C-u num F3 Starts recording but sets counter to num
C-u num F4 Plays last macro num times
C-x () Starts and stops macro recording
C-x e Plays last macro
C-x C-k Prefix key group for macros
  • C-u 0 F4: run the macro over and over again until reaching the end of a buffer

7.7.1 Counter

When you start recording, Emacs will automatically initialize an internal counter to zero, and every time you press F3 during the recording, Emacs will insert the counter and then increment the internal counter by 1 . There are, of course, many creative uses for the counter: creating numbered lists is the most obvious.

Key Binding Description
C-x C-k C-c Sets counter
C-x C-k C-f Sets format counter
C-x C-k C-a Adds to counter
C-x C-k TAB Runs macro & inserts counter
C-x C-k q Query user during kbd macro execution. Useful

7.7.2 Macro Ring

Key Binding Description
C-x C-k C-n Cycles macro ring to next
C-x C-k C-p Cycles macro ring to previous
C-x C-k n Names the last macro for saving to a file
C-x C-k b Binds the last macro to a key(like F5) Useful
C-x C-k e Edits macro. Useful
C-x C-k l Edits the last 300 keystrokes
M-x insert-kbd-macro Inserts macro as elisp for saving macro to a file

8 TRAMP: Remote File Editing

  • /protocol:[user@]hostname[#port]:path
  • automatically uses ~/.ssh/config
  • can use with bookmark

8.1 Multi-Hops

  • syntax for local: /sudo:user@localhost:/etc/fstab
  • syntax: /ssh:user@host|sudo:host:/etc/fstab

9 Dired

  • C-x 4 d: open dired in another window
  • dired-listing-switches: change the switches for ls (default: ls -al)

9.1 Marking/Flagging

Keys Description
* m Marks region
* u Unmarks region
* % Marks files by regexp
* . Marks files by extension
t, * t Toggles(Inverts) marking
* c Changes mark(e.g. * -> D)
  • The prefix key * is full of mark commands

9.2 Actions

Keys Description
i Insert sub-directory in the same buffer
g Refreshes dired buffer
~~ Creates a sub-directory
C Copy marked
R Renames/moves marked
O chown marked
G chgrp marked
M chmod marked
D Deletes marked
x Deletes flagged
F Visits marked (requires dired-x)
M-s a C-s ISearches all marked files
Q Query replace regexp marked files
! Shell command on marked files
& Async shell command on marked files
C-x s Save some buffers
  • ! and & will attempt to guess the next operation on that file
  • With marks, the shell key take every marked file as arguments by default.
  • Use command * to inserts all marked files as one long argument to a single command.

9.3 Find

Command Description
find-dired Calls find with a pattern
find-name-dired Calls find with -name
find-grep-dired Calls find with grep
find-lisp-find-dired Uses Emacs and regexp to find files
find-grep  

10 Shell Commands

10.1 Executing Command

Key Description
M-! Calls shell command and prints output
C-u M-! As above, but inserts into buffer
M-& Like M-! but asynchronous
C-u M-& Like C-u M-! but asynchronous
M-| Pipes region to shell command
C-u M-| Likes M-| but replaces region

10.2 Compiling

Command Description
M-x compile Runs a command, and tracks errors
M-x recompile Re-runs last command
M-g M-n, M-g M-p Jumps to next/previous error (global)
g Re-runs last command

10.3 Shell Choice

10.3.1 shell

Useful Features:

Keys Description
C-c C-s Saves command output to file
C-c C-o Kills command output to kill ring
C-c C-l Lists command history
C-c C-z Sends stop sub job

10.3.2 eshell

10.3.3 ansi-term

  • C-c C-j: term-line-mode
  • C-c C-k: term-char-mode

11 Read Only Mode

Keys Description
C-x C-r Opens file in read only mode
C-x C-q Toggles read only mode
M-x flush-lines Flushes lines matching a pattern
M-x keep-lines Keeps only lines matching a pattern
M-s o List lines matching a pattern
M-x auto-revert-mode Reverts buffer when file changes
M-x auto-revert-tail-mode Appends changes when file changes

12 Mapping Ctrl on Windows

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00

13 org-mode