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 withfg
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
require
d 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-py
for character input with pinyingchinese-sisheng
for literal pinying (https://unix.stackexchange.com/questions/239388/is-there-pinyin-input-mode-in-emacs)
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
Turn on word wrap (for prose) with
<M-x> visual-line-mode
Word count with
count-words-region
(or<M-=>
) on a selected regionIn 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
- 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
. Emptybufname
for current bufferRevert (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 deletionsq
to 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>
m
marks a file^
to go up a directoryd
to mark file for deletionR
can be used to either rename or move a filex
to execute deletion+
adds a new directoryg
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 ofEnter
andTab
) - 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 splitq
to quit- Use
ediff-regions-wordwise
orediff-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
<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 failurem
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
- 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-node
to 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=9222
to 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>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
<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 tohelm-find-files
<C-h> a
has 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> 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
<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 invokeag
<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
- 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:
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
<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
<s
to generate the#+BEGIN_SRC
snippet (useTAB
to 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
:session
causes code block to be run in a persistent inferior process:dir
specifies 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_web
at 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> / 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
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-mode
can 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
;%.2f
for 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> 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 agendaf
andb
to move forward and back weeks in Agenda vieww
for week viewt
for fortnight viewm
for month viewq
to quit
Magit
<C-x> g
to open up, after putting the appropriate keybindings ininit.el
:du
to show unstaged changesm
to merge things<TAB>
on a file to see that file's changesz
to do stash stuffv
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 revisionn
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 fileS
to stage all hunksc
to prepare to commit (can stage stuff along the way as well)p
to prepare to pushF
does a git pullk
to revert changes to an unstaged file (discards hunk).- With evil-magit, use
x
- With evil-magit, use
zz
git stasheszp
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 debugpip install jedi autopep8 flake8
can be done on system
pyvenv-activate
to activatevenv
, thenpyvenv-deactivate
to deactivateelpy-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
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
- Can then be saved with
yas-visit-snippet-file
lets 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