Emacs cheatsheet
Table of Contents
:ID: BB17CF51-DA23-46BB-A641-7B9D599715E0
This is just a dump or random things I’ve learned how to do in Emacs but will probably forget.
# Evil mode (and then some)
# Movement
Command | Description |
---|---|
0 |
beginning of line |
^ |
beginning of non-whitespace |
$ |
end of line |
9j |
move down 9 lines |
w |
move forward by word |
b |
move backward by word |
gg |
first line |
G |
last line |
C-u |
up half page |
C-d |
down half page |
f/ |
move forward to first “/” character |
t/ |
move forward right before the first “/” character |
; |
repeat that command again |
H |
head of the screen |
M |
middle of the screen |
L |
last of the screen |
} |
move forward by paragraph or block |
{ |
move backwards by paragraph or block |
* |
search for word under the cursor |
# |
search backwards for word under cursor |
/ |
search forward |
? |
search backward |
% |
find matching brace, paren, etc |
ma |
mark a line in a file with marker “a” |
`a |
after moving around, go back to the exact position of marker “a” |
'a |
after moving around, go back to line of marker “a” |
:marks |
view all the marks |
'' |
go back to the last place you were before moving |
g; |
go back to the last place edited |
[{ |
go back to beginning of code block |
# Editing
Command | Description |
---|---|
A |
add to end of line |
I |
insert at the beginning of the line |
yy |
copy line |
Y |
copy from cursor to end of line |
cc |
change line |
C |
change from cursor to end of line |
cit |
change text inside html tag |
ci' |
change text inside single quotes |
ci{ |
change text inside curly brackets. |
ci … |
change text inside whatever |
p |
paste after cursor |
P |
paste before cursor |
o |
add line below |
O |
add line above |
. |
repeat last command |
r |
replace character |
R |
replace. (overwrite) (good for columns of text) |
J |
join line (cursor can be anywhere on line) |
# Deleting text
Command | Description |
---|---|
dd |
delete line |
di' |
delete text inside single quotes (or inside whatever) |
x |
delete char under cursor |
X |
delete char before cursor |
D |
delete from cursor to end of line |
d$ |
delete from cursor to end of line |
dtx |
delete up to x where x is the thing you want to delete up to. |
dt space |
will delete up to first whitespace |
df space |
will delete up to and including the first whitespace |
# Visual mode
Command | Description |
---|---|
v |
visual char mode |
V |
visual line mode |
C-v |
block visual mode |
# Word Wrapping
auto-fill-mode
is what you need. This will automatically wrap words after
the line exceeds whatever value is set for fill-column
For example, turn it on for org-mode
(setq-default fill-column 80) (add-hook 'org-mode-hook (lambda () (auto-fill-mode 1)))
fill-region
does what is says
# Advising Functions
# After callback example
;; Save Org buffers after refiling (advice-add 'org-refile :after 'org-save-all-org-buffers)
# Surrounding
Use Evil-surround
s(
to surround region (with parenthesis in this example)
vio s'
cs'
to replace surround (example using single quote, but can be anything)
# Selecting text within surround
vi
[Option]
# Repeating text
- Select the region
SHIFT I
(evil insert line count)- Type characters
ESC
(the repeated chars are added)
# Font
M-x menu-set-font
offers a GUI font picker and size setter with previews.
# helm-projectile-find-file
This does fuzzy matching by default Start search query with a SPC to switch from fuzzy to exact match.
# Treesitter
Having this in emacs means syntax highlighting via a syntax tree rather than regex patters, better movement and hopefully faster.
# Mac
If using emacs plus, tree-sitter will be installed as a dependency of emacs v29+. See also https://github.com/d12frosted/homebrew-emacs-plus/pull/546
# Linux
Need to install tree sitter first, then build emacs v29 from source.
See also https://www.masteringemacs.org/article/how-to-get-started-tree-sitter
# Install Grammars
https://tree-sitter.github.io/tree-sitter/ Tree sitter needs grammars to work. Emacs needs to know where they are. The easiest way is to create a make telling emacs where the grammars are located (ie, git repo) then you can have emacs download and compile them.
(setq treesit-language-source-alist '((bash "https://github.com/tree-sitter/tree-sitter-bash") (css "https://github.com/tree-sitter/tree-sitter-css") (elisp "https://github.com/Wilfred/tree-sitter-elisp") (html "https://github.com/tree-sitter/tree-sitter-html") (javascript "https://github.com/tree-sitter/tree-sitter-javascript" "master" "src") (json "https://github.com/tree-sitter/tree-sitter-json") (markdown "https://github.com/ikatyang/tree-sitter-markdown") (org "https://github.com/tree-sitter/tree-sitter-org") (ruby "https://github.com/ikatyang/tree-sitter-ruby") (scss "https://github.com/ikatyang/tree-sitter-scss") (tsx "https://github.com/tree-sitter/tree-sitter-typescript" "master" "tsx/src") (typescript "https://github.com/tree-sitter/tree-sitter-typescript" "master" "typescript/src") (vue "https://github.com/ikatyang/tree-sitter-vue") (yaml "https://github.com/ikatyang/tree-sitter-yaml")))
Then use treesit-install-language-grammar
Check if its working. This returns t
if the language is supported, otherwise nil
(treesit-language-available-p 'typescript)
# New modes
You can map the old modes to the new ones. but this comes potentially at a price.
(setq major-mode-remap-alist
'((typescript-mode . typescript-ts-mode)))
# Troubleshooting
# Performance issues (profiling)
Use the built in profiler.
M-x profiler-start
and select what you want to profile.- Do the thing that is slow
M-x profiler-stop
M-x profiler-report
- Drill down into the items with
TAB
# Freezes
# C g
will break a loop
# Send kill cmd to trigger debugger
ps aux | grep -ie emacs | grep -v grep | awk '{print $2}' | xargs kill -SIGUSR2
# Reinstall a package
For example, delete and install org. Find org in the list and open. There’s a button to install it.
M-x package-delete RET org RET M-x list-packages RET / n org RET
# Word Processing
# Spell check
# flyspell
See functions flyspell-
# aspell
# Dictionary
See functions dictionary-
# List key strokes
C-h l
calls view-lossage
. This does not update as keys are pressed, but gives
you at static list of the 300 key presses.
# Exiting / Quit / Close
# When in client mode
This will close the client but keep the server running
C-x C-c
save-buffers-kill-terminal
:q
in evil mode does the same thing
# Encryption
See also GPG
# Installing / Upgrading (linux)
Upgrade the same way you install it. Follow install instructions in New computer setup
# GPG
See also GPG