Emacs

Table of Contents

Resources

Unsorted things

  • Difference between setq and defvar
  • Config file placed in ~/.emacs.d/init.el
  • Dismiss welcome screen with q
  • Save program with <C-x> <C-s>
  • Cancel an action with <C-g>
  • Close emacs with <C-x> <C-c>
  • Start editing a new file with <C-x> <C-f>
  • Repeat the last minibuffer command with <C-x> ESC ESC
  • List recent command history with <M-x> list-command-history
  • Suspend emacs with <M-x> suspend-emacs, resume with fg in parent process
  • Check what keybinding of a command is with <M-x> where-is (<C-h> w also works)
  • Check what command is bound to a key with <M-x> describe-bindings (<C-h> k also works)
  • Files ending with ~ are Emacs backup files. Can get rid of them with (setq make-backup-files nil) according to this, but will leave them in for now...
  • Check recent keystrokes with <M-x> view-lossage (also bound to <C-h> l)
  • Show line numbers with <M-x> linum-mode
  • Toggle line wrapping with <M-x> truncate-long-lines
  • Change color schemes with <M-x> load-theme
  • To indent a selection, use the <Tab> key
  • Replace all with <M-x> string-replace
  • Replace some with <M-C-%>
  • <F10> brings up the menu option, <Esc> to cancel
  • To convert from DOS to unix, do <C-x> <C-m> f. Source
  • To look up scheme symbols use <C-h> S
  • Kill an unresponsive scheme process with <M-x>delete-process "*scheme*" in the scratch buffer
  • Seeing which fonts you have available
  • Adjust font sizes without restarting with <C-x> <C-+> or <C-x> <C-->
  • Insert a symbol with <C-x> 8 <RET>, then the name of the unicode symbol
  • Look up messages history with view-echo-area-messages
  • Reload a required feature with <M-x> unload-feature, then load it again.
  • Evaluate an elisp fragment with <M-:> (+ 1 2)
  • Set Chinese input with <M-x> set-input-method (Source)

Getting Help

  • Check/describe variable name with <C-h> v or <F1> v
  • Check/describe current mode with <C-h> m or <F1> m
  • Check/describe key or chord with <C-h> k or <F1> k
  • Whilst entering chords, use <F1> to see what the completions are

TRAMP mode

  • Edit a remote file in TRAMP mode with <C-x> <C-f> /ssh:eddie@wintermute.lan:/home/eddie/.bashrc
  • Clean up connections and buffers with <M-x> tramp-cleanup-all-buffers

Spelling

  • Use flyspell-buffer to check entire buffer
  • Use flyspell-mode to toggle flyspelling

Buffers

  • Buffer documentation

  • Multiple windows

  • Turn on word wrap (for prose) with <M-x> visual-line-mode

  • Word count with count-words-region (or <M-=>) on a selected region

  • In one view, cycle between buffers with <C-x> RIGHT/LEFT

  • Find major mode (non-display name) with <M-:> major-mode

  • Open up a new frame with <M-x> new-frame RET

  • Delete a window with <C-x> 0

  • Split a window:

    • Horizontally with <C-x> 2
    • Vertically with <C-x> 3
  • Make current buffer to only window with <C-x> 1

  • Clear persisted desktop with <M-x> desktop-clear

  • Kill buffer with <C-x> k bufname RET. Empty bufname for current buffer

  • Revert (reload) the buffer with <C-x> <C-v> RET, or <M-x> revert-buffer (evil's :e doens't seem to work)

List buffers with <C-x><C-b> (opens up the Buffer Menu as a split window)

  • d marks a buffer for deletion (to be killed)
  • x performs flagged deletions
  • q to close menu

Interbuffer

  • Toggle between buffer windows with <C-x> o
  • Resizing window (prefix argument can be added, e.g. 15 <C-u>):
    • <C-x> } to enlarge horizontally
    • <C-x> { to shrink horizontally
    • <C-x> ^ to enlarge vertically

Bookmarks

https://www.gnu.org/software/emacs/manual/html_node/emacs/Bookmarks.html

  • Set bookmark with <C-x> r m
  • Jump to a bookmark with <C-x> r b
  • List bookmarks with <C-x> r l
  • Can also use marks: <C-u><C-u> to set a mark without starting the highlight of the region, and then <C-u><C-space> to jump back to it.

Dired

Can be used as a file browser

Open Dired with <C-x><d>

  • Navigate with <S-ArrowKeys>
  • m marks a file
  • ^ to go up a directory
  • d to mark file for deletion
  • R can be used to either rename or move a file
  • x to execute deletion
  • + adds a new directory
  • g to reload the dired buffer
  • <C-x><C-q> goes into editing mode
  • Use the prefix argument to maintain context of selected file when creating a new file (<C-x><C-f>) or directory (+)

Intrabuffer

  • Incremental search forward with <C-s>, repeat with <C-s> as well
  • Incremental search backward with <C-r>, repeat with <C-r> as well
  • Start of line with <C-a>
  • Go to a particular line with <M-g> g
  • Go to a particular column with <M-g> <TAB>
  • End of line with <C-e>
  • Start of paragraph with <M-a>
  • End of paragraph with <M-e>
  • Start of word with <M-b>
  • End of word with <M-f>
  • Next line with <C-n>
  • Previous line with <C-p>
  • End of buffer with <M-<>
  • Start of buffer with <M->>
  • Make current line center of window with <C-l>
  • Move cursor to middle of screen with <M-r>
  • Scroll up a window with <M-v>
  • Scroll down a window with <C-v>

S-Expressions

  • Move forward over a balanced expression with <C-M-f>
  • Move backward over a balanced expression with <C-M-b>

Editing

  • Indent sexp with <C-M-q>
  • Undo with <C-x> u
  • Kill a word with <M-DEL> (DEL seems to be backspace in cygwin)
  • Forward delete word with <M-d>
  • Forward delete word with <M-d>
  • Kill next character with <C-d>
  • Kill till end of line with <C-k>
  • Kill whole line with <C-k>
  • Paste from the system clipboard with <S-Ins> (seems to be cygwin in general)
  • Autocomplete key is <M-/> (just keep hitting it)
  • Kill whitespace until next word is <M-\>
  • Autoindent on the new line with <C-j> (instead of Enter and Tab)
  • Kill balanced expression forward with <C-M-k>
  • Comment current line with <C-x> <C-;> (more comment commands: https://www.gnu.org/software/emacs/manual/html_node/emacs/Comment-Commands.html)
  • Toggle selected region comment with M-;
  • Join current line to the line above with <M-^>
  • Join current line to the line below with <C-u><M-^>
  • Insert a literal tab character with <C-q> <TAB> (https://stackoverflow.com/a/5146702/1010076)

Copy and pasting (marking)

Need to select marking the region first with <C-space>, then move to the other end of the region. Other shortcuts. You can also mark with <C-@>, if <C-space> is used by something else (e.g. language input toggle on MacOS).

  • Cut text with <C-w>. If using terminal Emacs to copy to system clipboard, you'll have to use an OS command to do so (e.g. pbcopy on MacOS; select region, then <M-|> pbcopy)
  • Copy (yank) text with <M-w>
  • Paste from system clipboard with <C-y>
  • <M-h> selects a paragraph
  • Select an sexp with <C-M-h>

Ediff

  • | to view in vertical split
  • q to quit
  • Use ediff-regions-wordwise or ediff-regions-linewise to compare two bits of text
  • <space> to go to next difference

Shell

Runs the standard shell is.

Open with <M-x> shell

  • <M-p> to recall previous command
  • <M-r> to reverse search
  • Autocomplete with <M-/> as usual

Macros

From EmacsWiki:

  • <C-x> ( to start defining a macro
  • <C-x> ) to stop defining a macro
  • <C-x> e to execute defined macro
  • <C-u> 10 <C-x> e to execute defined macro 10 times (prefix command)

Misc commands

List minor modes

  • <M-:> evaluates an elisp s-expression

  • <C-c> capitalises the first character of a word

  • <M-X> executes extended command (e.g. eval-buffer)

  • <C-[> can be pressed instead of the Meta key (useful for <M-TAB> - Source)

  • Open up help with <C-h> m (shows minor modes as well) (describe-mode)

  • Show specific keybinding with <C-h> k

  • Show all keybindings available in the buffer <C-h> b

Scheme integration

  • Run scheme interpreter with <M-x> run-scheme
  • Load a scheme buffer into the running guile process with <C-c> <C-l>
  • Evaluate S-expressions into the running Guile process by placing the cursor at the end of the parenthesis, then <C-x> <C-e> (make sure the buffer you're doing this in is in Scheme mode first with <M-x> scheme-mode)

Ert

  • Run tests with (ert t)
  • <TAB> to get to next test failure
  • m to view messages on that test

Packages

Can usually install package with <M-x> package-install. If you get a 'not found' error though, try the following:

  • Install packages by adding the snippet given here, then run M-x package-list-packages:
    • Press Enter on the package you want to install
    • Use d to mark any obsolete package for deletion
    • Use i to mark any package for installation
    • Use U to upgrade installed upgradable packages
    • x to execute
  • You can run byte-force-compile and give it ~/.emacs.d/elpa to recompile outdated .elc files

Indium

Slime

User Manual

  • Spin up a SLIME server with <M-x> slime <RET>
  • <C-x> <C-e> evaluates expression before cursor into echo area
  • <C-c> <C-r> evaluates the region
  • <C-c> <C-d>h looks up the documentation for the keyword at point of cursor
  • <C-c> <C-k> to compile and load the current file (effectively evaluating the buffer)
  • <C-M-i> triggers autocomplete of function
  • <C-x> slime-restart-inferior-lisp restarts the process

Geiser

  • Cheatsheet
  • Start it up with <M-x> run-geiser <RET> guile
  • Enable geiser minor mode on a buffer with <M-x> geiser-mode, or <C-c><C-z> for it to guess
  • <C-c> <C-s> sets the Scheme implementation if necessary (e.g. can be used to switch from Chicken to Racket)
  • <C-c> <C-d> <C-d> sees the documentation for symbol at point
  • <C-c> <M-e> to jump to REPL after evaluation (in case it fails)
  • <M-x>geiser-restart-repl to restart the repl
  • Evaluated expressions/defines will only be visible in that file's 'module'; enter it in the REPL with something like ,enter "my-file.rkt"

Editing

  • <C-c><C-e><C-[> toggles between surrounding square or regular brackets

Documentation

  • Between the parens

  • <C-c><C-d>a to toggle autodocumentation

  • <C-c><C-d><C-d> asks for further information on idenitifer

  • <C-c><C-d>i opens up the manual automatically

Sexp Evaluation

  • <C-u> <C-x> <C-e> evaluates the sexp before point, places result into buffer. Only mentioned off-hand in the official docs, and seems to be inspired by SLIME.
  • <C-c> <C-b> evaluates the buffer
  • <C-c> <C-m> <C-e> macro expands a region

Helm

Incremental completion and selection, analogous to {u,de}nite, superset of CtrlP

  • Preconfigured commands
  • helm-M-x is pretty cool.
  • <C-x> b runs helm mini
  • <C-x> <C-f> has been rebound to helm-find-files
  • <C-h> a has been rebound to helm-apropos
    • Useful: When invoked, the keyword at point is automatically selected
  • <C-space> (or <M-space>) to mark a candidate
    • Then <S-M-d> to kill

Helm buffer

  • <M-Space> marks a file
  • <M-n> copies the symbol at point to the minibuffer
  • <C-c> o opens up a candiate in a split window (find-file-other-window)
  • <C-j> to look up documentation
  • <C-n> and <C-p> to scroll through results
    • <←> and <→>, or <C-o> switches through sources

Ag

Projectile

Helm integration. Cheatsheet

  • <C-p> p p to change projects
  • <C-p> p f to find file in project
  • <C-p> p F to find file in all projects
  • <C-c> p s s to invoke ag
  • <C-c> p D to open Dired view of the current project

.projectile doesn't seem to detect ignoring individual files, but Projectile does respect .gitignore though.

JS2 Mode

Useful links:

You can use js2-mode-toggle-warnings-and-errors to suppress custom

Ido

Stands for Interactively Do Things

Open ido with <C-x> b to navigate to open buffers

  • MRU is the first entry
  • <C-s> (next) and <C-r> (previous) to move through list
  • <C-x> <C-b> and <C-x> <C-f> work as usual

Smartparens

Bindings

(Updated occasionally from my init.el)

(use-package smartparens
  :bind (
   ("C-M-f" . sp-forward-sexp)
   ("C-M-b" . sp-backward-sexp)

   ;; Forwards
   ("C-M-d" . sp-down-sexp)
   ("C-M-e" . sp-up-sexp)

   ("C-M-a" . sp-backward-up-sexp)
   ("C-M-u" . sp-backward-down-sexp)

   ("C-)" . sp-forward-slurp-sexp) ;; Sometimes <C-0> in WSL
   ("C-}" . sp-forward-barf-sexp)

   ("M-[" . sp-backward-unwrap-sexp)
   ("M-]" . sp-unwrap-sexp)

   ("C-M-k" . sp-kill-sexp)

   ;; Binding shadows transpose-sexps
   ("C-M-t" . sp-transpose-sexp)))
  • Use <M--> <C-M-t> to transpose a sexp backward

Markdown

Markdown Major Mode

  • Follow link with <C-c><C-o>

Paredit

Tree editing keybindings

Traversal

  • <C-M-f> goes to next s-expression (if none available, ascends to outer s-expression)

  • <C-M-b> goes to previous s-expression

  • <C-M-d> descends into the next s-expression

  • <C-M-p> descends into the previous s-expression

  • <C-M-u> ascends out of current s-expression on the left

  • <C-M-n> ascends out of current s-expression on the right

Editing

  • <C-)> forward 'slurps' (pulls in next s-expression into current s-expression, e.g. (a b │) c(a b │c))
  • <C-}> backward 'barfs' (spits out s-expression that cursor is on out of enclosing s-expression, e.g. (a b c|)(a b) c│
  • <M-s> gets rid of surrounding parenthesis, e.g. (a b c│)a b c│
  • <C-M-t> transposes sexps. With cursor on the end of a form, you can move it down

rtags

MacOS

Install onto system with e.g. brew install rtags. Then:

  • rdm

Make sure cmake has compiled the compiler commands

Ubuntu

Install from rtags emacs package. Ensure it's running with (rtags-start-process-unless-running).

Binaries are placed in the Emacs packages directory once installed, e.g. ~/.emacs.d/elpa/rtags-20190820.502/rtags-2.33/bin

C/C++ Development

Currently using the irony (and company) stack:

  • company
    • irony
    • company-irony
    • company-irony-c-headers
    • irony-eldoc
    • flycheck-irony

Irony depends on cmake; compile with cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON . to create the compile_commands.json file.

Resources

With cmake:

Make sure to use

Eyebrowse

  • <C-c><C-w><C-c> creates a new window config
  • <C-c><C-w>" deletes the current window config
  • <C-c><C-w>, renames the current window config

With opinionated Evil bindings activated:

  • zx switches to the last window config

Spacemacs

Emacs distribution with custom integrations to make it more vi-like

Not really using this anymore. Remove by deleting all of /.emacs.d

Orgmode

  • Compact guide

  • Beginner's guide

  • Creating timestamps

  • Extensively documented workflow

  • <C-c> . to add a timestamp (date only)

  • <C-u> <C-c> . to add a timestamp (date and time)

  • <C-c> ! to add an inactive timestamp (not an agenda entry)

  • <C-c> / brings up sparse tree prompt

  • <C-c> <C-c> removes highlighting of sparse tree views

  • <C-c> <C-e> starts the exporter

  • <C-c> <Space> to clear a cell in a table

Code blocks

#+begin_src emacs-lisp
(+ 3 4)
#+end_src

Note that xml code blocks does not work well for syntax highlighting in emacs; use nxml instead.

Ditaa and Artist mode

https://stackoverflow.com/questions/30861929/emacs-artist-mode-no-gui

  • <C-c> <C-a> <C-o> selects what to draw with the mouse

Archiving

Can move subtrees in to a separate archive file to stop it contributing to the agenda (Source)

  • <C-c $> to archive to org-archive-location

Outlines

  • Tabbing on a headline toggles folding
  • Add a 'cookie' to the parent entry of the checkbox list with [/] or [%]
  • <S-←> and <S-→> cycles through workflow states
  • <M-RET> inserts a new heading/item at same level as the one at point
  • <C-u> <C-c> <C-c> to convert add a checkbox to list item
  • <C-c> <C-c> to tick/untick a checkbox
  • <C-c> <C-q> to set a tag on a headline
  • <C-c> ' to edit a code block. <C-x> <C-s> to save, <C-c> ' again to exit
  • <C-c> <C-t> to turn entry into a todo entry
  • <C-S-↓> and <C-S-↑> to move an entry up/down
  • <C-c><C-x>\ to toggle utf-8 display, after inputting special/greek symbols
  • <C-c> / m to create a sparse tree, with headlines matching a tag (org-match-sparse-tree)
  • Use outline-show-all to subsequently expand the sparse tree back, and <C-c> <C-c> to get rid of the higlighting
  • <S-→> or <S-←> while in a list to cycle through numbered/unnumbered bullet point formats

Orgmode can autohide links for readability. (Source)

Format is [[link][description]].

  • <C-c><C-o> to open link in external browser
  • <C-c><C-l> expands link and description for editing
  • <M-x> font-lock-mode can expose these links to make them easier to edit and see

Tables

Pressing tab at the end of the following creates the next column:

|1|2

  • <C-e> - creates a row of lines below
  • <C-c> <Space> clears current field
  • <C-c ? tells you which row/column you are on
  • <C-c> <C-c> calculates a formula on a cell

Images

Images and Tables

Can do images like so:

#+CAPTION: This is the caption for the next figure link (or table)
[[./img/a.jpg]]
  • Toggle displaying inline images with <C-c><C-x><C-v>

Motion

From the docs:

  • <C-c> <C-n> next heading
  • <C-c> <C-p> previous heading

Structured:

  • <C-c> <C-f> next heading same level

  • <C-c> <C-b> previous heading same level

  • <C-c> <C-u> up a level heading

Agenda

  • <C-c> a opens agenda
  • <C-c> [ adds current document to agendas
  • <C-c> ] removes current document from agendas
  • <C-'> to go to next buffer in the agenda list
  • <C-c> a m to see a global list of tags from all agenda files (http://orgmode.org/manual/Tag-searches.html#Tag-searches)
  • <C-c> a < to restrict agenda commands to the current buffer

Agenda buffer

  • <C-c> a to bring up agenda
  • f and b to move forward and back weeks in Agenda view
  • w for week view
  • t for fortnight view
  • m for month view
  • q to quit

Magit

Git integration in Emacs

  • <C-x> g to open up, after putting the appropriate keybindings in init.el:
  • du to show unstaged changes
  • m to merge things
  • <TAB> on a file to see that file's changes
  • z to do stash stuff
    • v to show the contents of the item on the stash
  • y to remote locations (y r with evil-magit)
  • M to bring up remote configuration, a to add a remote (<C-y> to paste from clipboard while in minibuffer)
  • magit-log-buffer-file (bound to , d) to see a list of commits on the current buffer
(global-set-key (kbd "C-x g") 'magit-status)

As an aside, there is also vc-annotate for the entire files of previous commits:

  • p to go to previous revision
  • n to go to next revision

Diff Commands

Enter diff mode with d, then another (e.g. u)

  • Go down with <space>, go up with <del> (or <S-space>)
  • Stage with s as usual
  • k to discard a hunk (k with evil-magit)

Resources

Magit status

  • s to stage/start tracking a file

  • S to stage all hunks

  • c to prepare to commit (can stage stuff along the way as well)

  • p to prepare to push

  • F does a git pull

  • k to revert changes to an unstaged file (discards hunk).

    • With evil-magit, use x
  • zz git stashes

  • zp git stash pops

  • <TAB> on a modified file to see changes

  • <C-u> <C-c> . adds a datetime entry

  • <C-c> . adds a date entry

Elpy

https://github.com/jorgenschaefer/elpy

  • elpy-config to debug
    • pip install jedi autopep8 flake8 can be done on system
  • pyvenv-activate to activate venv, then pyvenv-deactivate to deactivate
  • elpy-shell-switch-to-shell is bound to <C-c> <C-z>
  • elpy-doc (<C-c> <C-d) to check for documentation under point. Try restarting if it can't pick it up (e.g. package may have been installed after elpy was loaded).

Evil

Insert mode keybindings

  • gd goes to the definition of the symbol
  • <C-z> to toggle evil mode

Regexes that work in Vim don't seem to work (\w, \s etc.) - refer to Emacs regexes instead.

Treemacs

NERDTree-style tree navigation browser: https://github.com/Alexander-Miller/treemacs

Yasnippet

Can only have one snippet per file, unlike Vim's Ultisnips

  • <C-c> & <C-e> creates a buffer for snippet creation (yas-new-snippet also works)
    • Can then be saved with <C-c><C-c>, which intelligently finds and creates any directory
  • yas-visit-snippet-file lets you find a snippet and edit it

Resources

Company

Completion mode framework.

Tern

  • <C-c> <C-r> to rename a variable

Glossary

  • Minibuffer: Where Emacs reads complicated arguments, command names, Lisp expressions. By default, uses the echo area for prompts etc. <M-x> commands are minibuffer commands