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 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
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)chinese-pyfor character input with pinyingchinese-sishengfor literal pinying (https://unix.stackexchange.com/questions/239388/is-there-pinyin-input-mode-in-emacs)
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
<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
Buffers
Turn on word wrap (for prose) with
<M-x> visual-line-modeWord count with
count-words-region(or<M-=>) on a selected regionIn one view, cycle between buffers with
<C-x> RIGHT/LEFTFind major mode (non-display name) with
<M-:> major-modeOpen up a new frame with
<M-x> new-frame RETDelete a window with
<C-x> 0Split a window:
- Horizontally with
<C-x> 2 - Vertically with
<C-x> 3
- Horizontally with
Make current buffer to only window with
<C-x> 1Clear persisted desktop with
<M-x> desktop-clearKill buffer with
<C-x> k bufname RET. Emptybufnamefor current bufferRevert (reload) the buffer with
<C-x> <C-v> RET, or<M-x> revert-buffer(evil's:edoens'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
Navigation
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> mmarks a file^to go up a directorydto mark file for deletionRcan be used to either rename or move a filexto execute deletion+adds a new directorygto 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>(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)
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> kShow 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
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
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
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
<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-h> 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
Helm buffer
<M-Space>marks a file<M-n>copies the symbol at point to the minibuffer<C-c> oopens 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
<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)
("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
- 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
- https://tuhdo.github.io/c-ide.html
- http://martinsosic.com/development/emacs/2017/12/09/emacs-cpp-ide.html
- https://oremacs.com/2017/03/28/emacs-cpp-ide/
- https://eklitzke.org/using-emacs-and-rtags-with-autotools-c++-projects
With cmake:
- https://cmake.org/cmake-tutorial/
- http://derekmolloy.ie/hello-world-introductions-to-cmake/
- https://cmake.org/cmake/help/v3.0/command/add_executable.html
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:
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
<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- 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 link in external browser<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 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> 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
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 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
sas usual kto discard a hunk (kwith evil-magit)
Resources
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 pushFdoes a git pullkto revert changes to an unstaged file (discards hunk).- With evil-magit, use
x
- With evil-magit, use
zzgit stasheszpgit 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-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
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
Resources
- http://ergoemacs.org/emacs/yasnippet_templates_howto.html
- http://joaotavora.github.io/yasnippet/snippet-development.html#sec-1-2
- Markdown snipppets
Company
Completion mode framework.
<F1>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