Spacemacs cheetsheet March 2022

Table of Contents

Taken from: inflambda.tech

1. Use mirrors in China

For users in China, use a mirror for spacemacs, this will save a lot time downloading packages. Locate configuration-layer-elpa-archives in ~/.spacemacs.d/init.el and edit as follows:

(setq configuration-layer-elpa-archives
      '(("melpa-cn" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
        ("org-cn"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/org/")
        ("gnu-cn"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")))

2. Visual selection

Key(s) Action
v Start visual selection, then use navigation keys to select region
V Start visual selection by entire line(s)
SPC v Start visual selection starting from current word, then use v / V to widen / narrow search.

3. Editing

3.1. General Editing

Key(s) Action  
I Edit from start of line
A Edit from end of line
i Edit before current character
a Edit after current character
o Edit by starting a new line below
O Edit by starting a new line above
c w Change current world
4 c w Change 4 words starting from current word
v c Change current character
SPC v c Change current word
SPC v d Delete current word
C Change till the end of current line
D Delete till the end of current line
x p Transpose current character with next
SPC x t c Transpose current character with previous
SPC x t w Transpose current word with previous
SPC x t l Transpose current line with previous
SPC S c Spell check and change current word
z = Spell check and change all occurrences of current word
SPC j = Indent region or buffer
SPC { d } Kill paragraph
C-R " Paste from register in edit mode
M-q Wrap line (This is hard wrap, for soft wrap use SPC t j)

3.2. Programmer Editing

Key(s) Action
d a b Delete current s-expression
d i b Delete the contents of current s-expression
c a b Change current s-expression
c i b Change the contents of current s-expression
y a b Copy current s-expression
y i b Copy the contents of current s-expression
SPC k t Transpose current s-expression with previous
g c c Comment current line
M-; Add comment to the end of line
% Jump to matching brackets
C-k sp-kill-hybird-sexp, kill line respecting pair delimiters. Key biding works in normal modes in some major modes

4. Navigation

Key(s) Action
0 Go to start of line
^ Go to first non-whitespace character in current line
e End of world without trailing white space
w End of word with trailing white space
f/ Go forward to the first occurrence of /
M Go to middle of buffer
H Go to head of buffer
C-u Go up half a page
C-d Go down half a page
SPC j i List of functions/items in current buffer
g d Goto definition
C-o Go back
C-i Go forward (Reverse of C-o)
g ; Go to position of last edit
m a Add a named marker a
` a Go to mark a
' a Go to the line with mark a
:marks List all marks
' ' Go to last place
SPC j j Jump by character
SPC j l Jump by line
C-y scroll down without moving cursor
C-e Scroll up without moving cursor
z t Put current line to top of buffer
z b Put current line to bottom of buffer
z z Put current line to center of buffer
8 G Go to line by number
z c Fold current block
z o Unfold/Expand current block
SPC t l Toggle line wrap, similar to soft wrap seen in other editors
SPC t n v Toggle line numbers

5. Narrowing

Narrowing is very useful to many occasions, e.g., limiting search/replace region, or focusing on function or block.

Key(s) Action
SPC n f Narrow to function/block
SPC n r Narrow to select region
SPC n w Widen, use this to undo narrowing

6. Toggle Themes

You can edit themes by editing the values of dotspacemacs-themes in spacemacs configuration file (SPC f e d), when emacs starts it will use the first theme as default theme. A list of themes can be found at emacs-theme-gallery and Spacemacs ThemesSpacemacs Themes. My current favorite dark theme is cyberpunk. | |

Key(s) Action
SPC T n Switch to next theme and open themes transition state, use n/p to preview next/previous theme.

7. Multi edit

Evil multi-cursor (evil-mc) mode

Use visual selection to select world(s), then use C-p or C-n to select occurrences above or below, then enter edit mode to edit multiple occurrences, or use g r q to quit multi-edit mode, or use g r p or g r n to exclude previous or next occurrence

8. iedit

Key(s) Action
SPC s e Edit all occurrence of the word under cursor.

Spacemacs will display the number of occurrences in the status bar. Use ESC to quit.

9. Kill ring

Key(s) Action
SPC r y Show kill ring (helm-show-kill-ring), i.e., list of copy/paste history

10. Magit, git

Key(s) Action
SPC g s Show git status
s Add the highlighted file to staging
c c Create a commit, edit commit message. Use , , to submit commit or use , k to cancel the commit.
q Close magit window

11. Files

Key(s) Action
SPC f f Find file, use C-h to go to parent directory
SPC p f Find file in current project
SPC p k Kill project
SPC f e d Open spacemacs configuration file
SPC f e R Reload spacemacs configuration file

12. Treemacs

treemacs is for viewing and navigating project files.

Key(s) Action
SPC f t Open treemacs buffer. Type ? for more key bindings.
SPC p t Open project root in treemacs buffer
C-c C-p d Remove a project from treemacs buffer
SPC 0 If in file buffer, switch to treemacs buffer

13. Ranger

ranger is a file manager in emacs.

Key(s) Action
SPC a t r r Open ranger buffer
h Go to parent directory
l Jump to selected item
y y Copy file
p p Paste file
f Search by file name

14. Buffers

Key(s) Action
SPC b b List buffers for current layout. (See the remarks below.)
SPC b B List buffers for all layouts. (See the remarks below.)
SPC b . Start buffer transition state. You can then use n, p etc to switch to other buffers b.
SPC TAB Toggle buffer
SPC b d Kill this buffer
SPC b n Go to next buffer
SPC b p Go to previous buffer
SPC b s Go to scratch buffer
SPC b Y Copy current buffer
SPC f c Save current buffer as a new file
SPC b w Toggle buffer as read-only
SPC f s Save current buffer
SPC f S Save all buffers
SPC f R Change file name of current buffer
SPC f y n Copy file name of current buffer
SPC b u Reopen last killed buffer
SPC b C-d Kill other buffers

14.1. Remarks

Use M-SPC to enter helm transition state, then you can mark buffer(s) use t and kill the marked buffer(s) with M-D.

15. Layout

Layout is also known as perspective in spacemacs, a layout contains a list of buffers. When spacemacs is started, the default layout is Global

Key(s) Action
SPC l l List all layouts
SPC l s Save current layout to a file
SPC l L Load layout from file
SPC l x Kill current layout with all buffers

16. Bookmarks

It's an easy way to pause and resume editing a file. The bookmark feature is like a light version of layout.

Key(s) Command Action
C-x r m bookmark-set Create a named bookmark
C-x r b bookmark-jump Open a saved bookmark

17. Searching

Key(s) Action
SPC s s Search with helm-swoop in current buffer
SPC s S Search with helm-swoop in current buffer for current word
SPC s p Search with helm-swoop in current project
SPC r l Resume last search (helm-resume)
SPC s l Repeat last search (for project search only)
SPC p % Regex replace in all file in project, type ? for help
* Search forward for the word under cursor. Use n or p to go to next or previous match. You can also start iedit from here.
# Search backward for the word under cursor. Use n or p to go to next or previous match. You can also start iedit from here.
SPC s c Clear search highlights

If you have ripgrep installed, you can use ripgrep arguments as the search pattern. Example patterns for search the term bql in different files:

Search only in Java files:

-g*.java bql // Or use `-t` flag: -tjava bql

Search in any files except Java files:

-g!*.java bql

Show 2 lines above and below the occurrence, this works in project search only:

-C2 bql

Search and replace

After doing search and helm buffered displayed, press C-c C-e, you can then edit the results in a new buffer as if it's a new file, e.g., you can use SPC s e to replace all occurrences.

18. Undo tree

Key(s) Action
SPC a u Show undo tree. Use j, k, h, l to select from the edit history.

19. Spacemacs Clojure mode key bindings

Key(s) Action
, ' Run Clojure REPL (sesman-start)
, e f Evaluate current s-expression and show result
, s f Send current s-expression to REPL and evaluate
, e v Evaluate expression before point (cider-eval-sexp-at-point)
, e l Evaluate expression at current line
, s b Reload current buffer (cider-load-buffer)
, e b Evaluate current buffer
, s a Toggle between REPL and Clojure source code
, s n Change REPL namespace to current buffer
, s l Clear REPL
, m q q Quit REPL
, m q r Restart REPL (sesman-restart)
, h h Show doc
, e ; Evaluate current s-expression and print result as comment in a new line
, e w Evaluate last s-expression and replace the expression with the result
, e p f Evaluate current s-expression and pretty-print result in a new buffer
, e b Evaluate all code in buffer
, t a Run all tests
, m b Browse active REPLs (sesman-browse)
, e e Evaluate expression before cursor
, e w Evaluate expression and replace by the results
, e m Macro-expand for immediate macro
, e M Macro-expand for all nested macros
, T e Toggle enlighten mode with cider inspector
, d v Inspect current symbol
, h a cider-apropos, show list of Clojure symbols
, g r Find reference to the symbol under cursor

Use SPC v followed by v or V to expand or shrink expression selection.

When in Clojure REPL buffer, the following keybinding are available:

Key(s) Action
C-j Cycle up through history
C-k Cycle down through history
, Open mini buffer showing cider commands. For instance, you can use clear command to clear REPL buffer.

19.1. Lisp state

Lisp State is for easy navigating and editing s-expressions in Clojure mode. | |

Key(s) Action
SPC k . Toggle list-state. When lisp-state is on, the status mode indicator is shown as L.

Key bindings in lisp state:

For a complete list of key bindings, see lisp-state

Lisp state uses a different set of keybindings, to switch back to normal state, press the ESC key.

After calling a Lisp state function, the buffer enters Lisp state, lisp state functions can be triggered without the SPC k prefix.

Key(s) Action
SPC k . Start lisp state
L Next expression
H Previous expression
SPC k w Wrap expression with parenthesis
SPC k W Unwrap expression
SPC k y Copy expression
SPC k y Copy expression
SPC k s Forward slurp expression (move inside the expression after the current s-expression )
SPC k d x Delete s-expression
M-t Transpose current s-expression with previous

20. Snippet

To use an existing snippets,

Type the keyword defined in the snippet

Use M-/ to expand with snippet Use TAB to jump to other makers in the snippet

Sinppets are saved in ~/.spacemacs.d/snippets/ or ~/.emacs.d/private/snippets/ directories.

List of snippets can be found with the yas-visit-snippet-file command.

Every snippet is a single file under a directory named with its target major mode.

A example snippet at ~/.spacemacs.d/snippets/org-mode/journal-entry is shown below, you type journal-entry to trigger the snippet and everything below # – will be inserted in place for you.

Note: $0 indicates the end cursor position, while $1, $2 etc indicates the initial and second positions.

# -*- mode: snippet -*-
#name : journal-entry
#key : journal-entry
#contributor : jr0cket
# --
* 201912$1
** Thoughts for today
$0
** Code commits
*** .
** Activities in detail
*** .

21. Zoom / Adjust font size

Key(s) Action
SPC z f Show mini buffer to adjust font size

22. Org mode

Key(s) Action
SPC a o o n List all agenda and todos
g r In org-agenda buffer, refresh agenda
C-c [ org-agenda-file-to-front, add this file to agenda list
t Toggle ToDo state selector
, b d Create a code block
, d d Create deadline for the current task
, d s Schedule the start date for the current task
, i b Create a template block, it's more general than , b d
, T c Toggle check box (a check box is simply a pair of brackets: [ ])
M-Left Promote headings
M-Right Demote headings
M-S-Left Promote headings with all its sub-items
M-S-Right Demote headings with all its sub-items
, i l Insert a link
, i L Insert a link, auto extract link title from the link
SPC a o l Store a link target (org-store-link), use , i l to insert the link
, d d Add deadline/schedule a task
, s A org-archive-subtree, move current node to the archive file
, C p org-pomodoro
, s S Sort subtree
, s n Narrow to subtree, use this to narrow buffer to current org subtree
, s w Widen, this is the opposite of , s n
C-c @ org-mark-subtree, select subtree
C-c C-x C-c org-columns view, press q to quit column view
SPC a o C i org-clock-in
SPC a o C o org-clock-out
SPC a o C g org-clock-goto, goto the active task

If you have source code in an org file, it can be slow to open this file caused by source code evaluation. Disable code eval by adding this header argument: #+PROPERTY: header-args :eval no. See source.

23. Journal mode

Key(s) Action
SPC a o j j Create a new journal for current day
, p Previous journal
, n Next journal

24. EasyPG Assistant

You can encrypt or decrypt files with epa- commands, some are listed below.

Command Action
epa-list-key List all GPG keys
epa-encrypt-file Encrypt a file with GPG key. Use symmetric encryption if no key is selected.
epa-decrypt-file Decrypt a file. Emacs will cache password so you may not be asked to enter password for the file encrypted a short time ago.

25. Macro

Macro in emacs means a sequence of actions usually edits. You can use macro to simplify repetitive tasks.

To define and use a macro:

Press q q to start macro definition, all actions afterwards will be recorded.

Perform the editing actions you'd like to record.

Press q to complete the macro definition.

Press C-x e to execute this macro.

Press e to repeat this macro.

Press ESC to quit macro execution.

If you'd like to execute this macro multiple times, you can always use the number prefix trick. For example pressing 5 C-x e will execute the macro 5 times.

26. Project

Key(s) Action
SPC p t Show project tree. Press the enter key to open the file. SPC 0 to go back to project tree. Press q to quit project tree
SPC p a Toggle between source code and test file

27. Window management

The emacs window can be split in to multiple windows.

Press SPC w and some common window-related commands will be displayed in the mini-buffer.

Key(s) Action
SPC w . Enter window-transition-state, where you can select/move/resize windows
SPC w d Delete current window
SPC w / Split window horizontally
SPC w V Split window horizontally and focus the new window
SPC w [ Resize current window
SPC w s Split window vertically
SPC w S Split window vertically and focus the new window
SPC t g Toggle window size golden ratio

28. Toggle Emacs keybindings

You can use C-z to toggle between the default Evil mode and Emacs mode.

29. Flycheck errors

Key(s) Action
SPC e n Go to next error
SPC e p Go to previous error
SPC e L Go to flycheck error list

30. Flutter

Key(s) Action
, x x Flutter run or hot reload
, g r Find references

31. Rust

Key(s) Action
, q rust-quick-run
, = = rust-format-buffer
   

32. Python

Install necessary dependencies:

pip install python-language-server
pip install importmagic epc

33. JavaScript

To disable warnings of missing trailing semicolon in JavaScript mode, add this to spacemacs config (SPC f e d):

(setq js2-strict-missing-semi-warning nil)

34. Graphviz

Key(s) Action
, , prview in emacs

35. Add your own layer

To create a new layer, press SPC SPC and select configuration-layer/create-layer, a template layer will be auto generated.

Refer to configuring-spacemacs-a-tutorial for more details.

Author: Muhammad Ridho

Created: 2022-03-30 Wed 17:14

Validate