The Second Draft

By Ashton Wiersdorf

Write one to throw away.

About | Résumé

GitHub | Keybase | Twitter

23 September 2017

Emacs Tips and Tricks

Emacs Tips and Tricks

To Learn About

Things that make me happy


You can filter buffers by pattern with Helm. Type: @pattern to find buffers matching pattern. If you want to have spaces in the pattern, you must escape them with a backslash.

Searching with the Silver Searcher

You’ll need helm-ag. After searching, you get the following keybindings:

Key Bindings
Key Action
C-c o Open other window
C-l Search in parent directory
C-c C-e Switch to edit mode
C-x C-s Save ag results to buffer(Ask save buffer name if prefix key is specified)
C-c C-f Enable helm-follow-mode
C-c >, right Move to next file
C-c <, left Move to previous file
C-c ? Show help message
Edit mode keymap
Key Action
C-c C-c Commit changes
C-c C-k Abort
C-c C-d Mark delete line
C-c C-u Unmark
Saved buffer keymap
Key Action
RET Jump to current line posion
C-o Jump to current line posion in other window
g Update result


Any letter can be a register. (Uppercase and lowercase are distinct.) In the follinwg examples, <r> represents a register name.

Working with the point:

Working with text:

Working with numbers:


Special Modes


Start everything off right with M-x run-geiser RET racket RET.


Web Mode

I do a fair amount of web programming. web-mode is awesome! There are way too many keystrokes for me to list. Here are my favorite, though:

Markdown Mode

Text keys: (all start with C-c C-s)


Native bookmarks:

Jumping to a bookmark will do so in the current window, and will put you where the point last was in that buffer. If you are already in the buffer, then it will jump to the point where to bookmark was set.

Bookmarks persist over a session—I’m not sure where the file is, but they do get stored in some file.


M-/ will do “dynamic expansion”—if there is a word in one of the buffers of the current session that starts with whatever your cursor is on, it will expand to that word. Multiple consecutive invocations of this function will cycle through available expansions.

There’s a way to do manual expansion, but I don’t know it.

Window enlargements

I’ve defined a few nice functions. Here they are:

(defun sticky-enlarge-window-horizontally (prefix)
  (interactive "P")
  (enlarge-window-horizontally (if prefix (car prefix) 1))
  (unless (current-message)
    (message "(Use `[' and `]' to adjust window size)"))
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "]") 'enlarge-window-horizontally)
    (define-key map (kbd "[") 'shrink-window-horizontally)
    (set-transient-map map t)))

(defun sticky-shrink-window-horizontally (prefix)
  (interactive "P")
  (shrink-window-horizontally (if prefix (car prefix) 1))
  (unless (current-message)
    (message "(Use `[' and `]' to adjust window size)"))
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "]") 'enlarge-window-horizontally)
    (define-key map (kbd "[") 'shrink-window-horizontally)
    (set-transient-map map t)))

(define-key global-map (kbd "C-x }") 'sticky-enlarge-window-horizontally)
(define-key global-map (kbd "C-x {") 'sticky-shrink-window-horizontally)
(define-key global-map (kbd "<f7>") 'shrink-window-horizontally)
(define-key global-map (kbd "<f8>") 'balance-windows)
(define-key global-map (kbd "<f9>") 'enlarge-window-horizontally)


Macro wisdom

Put cursor where it is supposed to go, begin recording (C-x (), do thingy, isearch to next location, and then stop recording. (C-x )) This lets you see what is going to be edited next, and hit C-s C-s if you want to skip to the next match.

<f3> Is a very fancy key. Normally, it will begin recording a macro. Once you are defining a macro, hitting <f3> again will insert the current macro counter.

<f4> is its best friend. Hitting <f4> while defining a macro will end the macro. Hitting <f4> otherwise will then run the last defined keyboard macro. Running C-u <f4> runs the second macro in macro ring. Running C-u 4 <f4> runs the first macro 4 times. (Adjust 4 as you will.)

You can use Lisp inside of a macro. For example, to insert incrementing numbers, do:

M-: (setq x 1) RET
C-u M-: x RET
M-: (setq x (+ x 1))
<whatever else>

You can repeat a macro until an error is signaled with C-u 0 C-x e.

You can also run apply-macro-to-region-lines (C-x C-k r) to fire a macro on every line in the region.

To prompt a user for input while writing a macro, do: C-u C-x q. This is a variant of C-x q which queries the user.

Recursive editing

Hitting C-r will enter a recursive editing level when the macro is run, but not while you are recording.

C-x q enters a query state: y continues to execute the macro, n aborts the current iteration, and q aborts all together.

C-u C-x q lets you enter in some text.

To finish recursive editing, type C-M-c. To abort and halt execution, type C-].


To select text in a rectangle, use C-x SPC. The region will then highlight like a rectangle. The kill and yank commands will work like normal (i.e. hitting C-k will kill the rectangle.)

Misc. Keystrokes



Not like Perl. In (?:aaa|bbb), the characters (, ), and | all match themselves. If you want perl-like behavior, escape them: \(?:aaa\|bbb\).

But when you want to type that in a string literal, use "\\(?:aaa\\|bbb\\)".

Character Classes

Some common character classes:

Regex search and replace:

M-x replace-regexp
Replace regexp: right\|left
Replace regexp with: \,(if (equal "right" \&amp;) "left" "right")

Looks like the \,(...) syntax says “evaluate me”. :)

Regex search and replace with captured bit

M-x replace-regexp
Replace regexp: subject(\([A-Za-z]+\))
Replace regexp with: \1

That gets subject(*), and retuns *

Programming Languages


Compile (using make -k) with M-x compile.

Any errors will show up in a special buffer; visit with C-x \`

tags: programming - tools