Emacs
Table of Contents
- Resources
- Unsorted things
- Getting Help
- TRAMP mode
- Spelling
- Buffers
- Navigation
- Editing
- Ediff
- Shell
- Macros
- Misc commands
- Scheme integration
- Ert
- Calculator
- Packages
- Glossary
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 withfgin parent process - Check what keybinding of a command is with
<M-x> where-is(<C-h> walso works) - Check what command is bound to a key with
<M-x> describe-bindings(<C-h> kalso 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
require=d feature with =<M-x> unload-feature, then load it again. - Evaluate an elisp fragment with e.g.
<M-:> (+ 1 2) - Set Chinese input with
<M-x> set-input-method(Source)chinese-pyfor character input with pinyinchinese-sishengfor literal pinyin (https://unix.stackexchange.com/questions/239388/is-there-pinyin-input-mode-in-emacs)<C-\>to toggle between selected input methods
- Alternative to paging through
evil-ex-show-digraphsisinsert-char, where are text descriptions of the symbols
Getting Help
- Check/describe variable name with
<C-h> vor<F1> v - Check/describe current mode with
<C-h> mor<F1> m - Check/describe key or chord with
<C-h> kor<F1> k - Whilst entering chords, use
<F1>to see what the completions are
TRAMP mode
- Edit a remote file in TRAMP mode with e.g.
<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-bufferto check entire buffer - Use
flyspell-modeto toggle flyspelling ]s=/[s= to go to next/previous error
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
- Horizontally with
- 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. Emptybufnamefor current buffer - Revert (reload) the buffer with
<C-x> <C-v> RET, or<M-x> revert-buffer<s-u>(Super+u) will also work but will need an escape sequence mapped in iTerm2- evil's
:edoesn't seem to work
List buffers with <C-x><C-b> (opens up the Buffer Menu as a split window)
dmarks a buffer for deletion (to be killed)xperforms flagged deletionsqto close menu
Editing
- Indent sexp with
<C-M-q> - Undo with
<C-x> u - Kill a word with
<M-DEL>(DELseems 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 ofEnterandTab) - 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)- Can be used to replace
^Mcarriage return characters as well (<C-q> <C-m>)
- Can be used to replace
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.pbcopyon 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 splitqto quit- Use
ediff-regions-wordwiseorediff-regions-linewiseto 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> eto execute defined macro<C-u> 10 <C-x> eto execute defined macro 10 times (prefix command)
Misc commands
<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 failuremto view messages on that test
Calculator
Reverse polish calculator. Start with <M-x> calc, <C-x> * c or <C-x> * *
<S-u>to undo operationqto quitQdoes the square root<C-x> * bto toggle full-screeneto enter an exponent
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
dto mark any obsolete package for deletion - Use
ito mark any package for installation - Use
Uto upgrade installed upgradable packages xto execute
- You can run
byte-force-compileand give it~/.emacs.d/elpato recompile outdated.elcfiles
LSP mode
Default prefix bound to <s-l> (Super + l) - may need an escape sequence if used in iTerm2
<s-l> g gto go to definition<s-l> g rto go to references
Indium
- Up and Running: http://indium.readthedocs.io/en/latest/setup.html#up-and-running
- Code evaluation: http://indium.readthedocs.io/en/latest/code-evaluation.html
<C-c> <C-z>to switch to REPL<M-x> indium-run-nodeto start a node process- Alternatively, connect to a browser with
<M-x> indium-connect-to-chrome- On MacOS, open up Chrome with
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222to allow remote debugging
- On MacOS, open up Chrome with
Slime
- 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>hlooks 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-lisprestarts the process
Sly
Updated fork of SLIME
Debugging
t(or<Enter>) to toggle display of a frame's local variableseto eval an expression in the current frame's contextvto go to the source location of the current frame (overridden toSwith evil-collection)ito inspect the value of a local variable in the frame
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-replto 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>ato toggle autodocumentation<C-c><C-d><C-d>asks for further information on idenitifer<C-c><C-d>iopens 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-xis pretty cool.<C-x> bruns helm mini<C-x> <C-f>has been rebound tohelm-find-files<C-x> c ahas been rebound tohelm-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
- Then
- When searching with helm-ag, can narrow down on the extension with something like
-G\.gd$ tween
Helm buffer
<M-Space>marks a file<M-n>copies the symbol at point to the minibuffer<C-c> oopens up a candidate 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
Projectile
<C-p> p pto change projects<C-p> p fto find file in project<C-p> p Fto find file in all projects<C-c> p s sto invokeag<C-c> p Dto 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) ("C-M-]" . sp-unwrap-sexp) ;; Forward ;; This is conflicting with pasting from the terminal (how?) ("C-M-k" . sp-kill-sexp) ("M-k" . sp-backward-kill-sexp) ;; Binding shadows transpose-sexps ("C-M-t" . sp-transpose-sexp)))
- Use
<M--> <C-M-t>to transpose a sexp backward
Markdown
- 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
C/C++ Development
As of 2026/03/15, LSP mode works well out of the box with this, so disregard the below info.
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.
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:
zxswitches 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- Clocking commands (use under a heading): (https://orgmode.org/manual/Clocking-commands.html)
<C-c> <C-x> <C-i>(org-clock-in)<C-c> <C-x> <C-o>(org-clock-out)org-clock-reporton parent heading to insert a summary<Shift-Down>and<Shift-Up>to adjust timestamps
- Add a file to the agenda (so that it shows up in
org-agenda's calendar) withorg-agenda-file-to-front(<C-c> [)
Code blocks
#+begin_src emacs-lisp (+ 3 4) #+end_src
- Use
<C-c> 'to edit the code in a new buffer - Use
<C-c> <C-c>to evaluate code block - Use
<sto generate the#+BEGIN_SRCsnippet (useTABto complete) - Header arguments can be used to alter the environment the block runs in
- https://org-babel.readthedocs.io/en/latest/header-args/#cmdline
- https://orgmode.org/manual/Specific-header-arguments.html#Specific-header-arguments
:sessioncauses code block to be run in a persistent inferior process:dirspecifies the default directory for the code block execution- Specify a buffer-wide header argument with something like
#+PROPERTY: header-args:sh :dir ~/dev/zz_koan_mettle_webat the top of the file
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 toorg-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> / mto create a sparse tree, with headlines matching a tag (org-match-sparse-tree)- Use
outline-show-allto 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
Hyperlinks
Orgmode can autohide links for readability. (Source)
Format is [[link][description]].
<C-c><C-o>to open URL link in external browser, or navigate to internal link- Dedicated targets are internal links, e.g.
<<my-internal-link>>which can also be navigated to <C-c> &to go back from a link (org-mark-ring-goto)
- Dedicated targets are internal links, e.g.
<C-c><C-l>expands link and description for editing<M-x> font-lock-modecan expose these links to make them easier to edit and see
Tables
- http://pragmaticemacs.com/emacs/org-mode-basics-ii-use-simple-tables-in-your-notes/
- https://orgmode.org/manual/Built_002din-table-editor.html
- https://orgmode.org/worg/org-tutorials/org-spreadsheet-intro.html
- Table references https://orgmode.org/manual/References.html#References
- Calc format works for field and range formulae: https://orgmode.org/manual/Formula-syntax-for-Calc.html
;%.2ffor 2 decimal places
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
Can embed images/pictures like so:
#+CAPTION: This is the caption for the next figure link (or table) [[./img/a.jpg]]
- Toggle displaying of inline images with
<C-c><C-x><C-v>(org-toggle-inline-images)
Folding
zoto expand a heading (evil binding)zOto open headings recursively
zcto close a heading (evil binding)zCto close headings recursively
zato toggle foldingzmto close all foldszrto open all folds<Shift-Tab>to cycle visibilities
Agenda
https://orgmode.org/manual/Filtering_002flimiting-agenda-items.html
<C-c> aopens 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 mto 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<C-c> a tto see allTODOitems- Use e.g.
2 rto see allDONEitems /to add filtering
- Use e.g.
Agenda buffer
<C-c> ato bring up agendafandbto move forward and back weeks in Agenda viewwfor week viewtfor fortnight viewmfor month viewqto quit
Magit
<C-x> gto open up, after putting the appropriate keybindings ininit.el:duto show unstaged changesmto merge things<TAB>on a file to see that file's changeszto do stash stuffvto show the contents of the item on the stash
yto remote locations (y rwith evil-magit)Mto bring up remote configuration,ato 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:
pto go to previous revisionnto go to next revisionfto open file at specific revisionlto view logdto view the diff
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
sas usual kto discard a hunk (kwith evil-magit)
Resources
Magit log
Access logs relevant for the current buffer with magit-log-buffer-file.
While on a commit, use magit-find-file to visit that file (read-only) at that commit (git-timemachine may be better for this for browsing through files at different commits).
Magit branch
bfrom status to triggercto create a new branch; can choose a remote branch to base off
Magit status
sto stage/start tracking a fileSto stage all hunkscto prepare to commit (can stage stuff along the way as well)pto prepare to pushfto fetchFdoes a git pullkto revert changes to an unstaged file (discards hunk).- With evil-magit, use
x
- With evil-magit, use
zzstasheszppops stashzaapplies stashzistashes whatever's staged<TAB>on a modified file to see changes?to see optionsoorOto reset.HEAD^is useful
Elpy
https://github.com/jorgenschaefer/elpy
elpy-configto debugpip install jedi autopep8 flake8can be done on system
pyvenv-activateto activatevenv, thenpyvenv-deactivateto deactivateelpy-shell-switch-to-shellis 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
gdgoes 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
o xopens current node in external tooluto go up a directoryWtoggles pane wideningc fto create a filec dto create a directoryy rto copy the name of the node, relative to the project rooty nto copy the name of the node
Yasnippet
Can only have one snippet per file, unlike Vim's Ultisnips
<C-c> & <C-e>creates a buffer for snippet creation (yas-new-snippetalso works)- Can then be saved with
<C-c><C-c>, which intelligently finds and creates any directory
- Can then be saved with
yas-visit-snippet-filelets you find a snippet and edit it
Company
Completion mode framework.
<F1>or<C-h>to show documentation on candidate<M-1>to select the first completion canidate,<M-2>to select second etc.<C-n>to cycle forward on candidates (needs mapping https://emacs.stackexchange.com/questions/17965/company-mode-popup-closes-after-c-n)<C-p>to cycle backward on candidates (needs mapping as well)- Check what backends are currently being used with
<M-x> company-diags
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