Spacemacs Keybindings

I've used emacs for more than a decade, I really loved it, however recently I find the emacs pinky finger problem gets more serious. Since now I am out of job, it is just the right time to give spacemacs a try. I believe that tools are tools, not for fancy show-casing, so I just use Practicalli's spacemacs configuration instead tweaking my own.

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/")))

Visual selection

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

Editing

General editing

Key(s)Action
IEdit from start of line
AEdit from end of line
iEdit before current character
aEdit after current character
oEdit by starting a new line below
OEdit by starting a new line above
c wChange current world
4 c wChange 4 words starting from current word
v cChange current character
SPC v cChange current word
SPC v dDelete current word
CChange till the end of current line
DDelete till the end of current line
x pTranspose current character with next
SPC x t cTranspose current character with previous
SPC x t wTranspose current word with previous
SPC x t lTranspose current line with previous
SPC S cSpell 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-qWrap line (This is hard wrap, for soft wrap use SPC t j)

Programmer editing

Key(s)Action
d a bDelete current s-expression
d i bDelete the contents of current s-expression
c a bChange current s-expression
c i bChange the contents of current s-expression
y a bCopy current s-expression
y i bCopy the contents of current s-expression
SPC k tTranspose current s-expression with previous
g c cComment current line
M-;Add comment to the end of line
%Jump to matching brackets
C-ksp-kill-hybird-sexp, kill line respecting pair delimiters. Key biding works in normal modes in some major modes

Navigation

Key(s)Action
0Go to start of line
^Go to first non-whitespace character in current line
eEnd of world without trailing white space
wEnd of word with trailing white space
f/Go forward to the first occurrence of /
MGo to middle of buffer
HGo to head of buffer
C-uGo up half a page
C-dGo down half a page
SPC j iList of functions/items in current buffer
g dGoto definition
C-oGo back
C-iGo forward (Reverse of C-o)
g ;Go to position of last edit
m aAdd a named marker a
aGo to mark a`
' aGo to the line with mark a
:marksList all marks
''Go to last place
SPC j jJump by character
SPC j lJump by line
C-yscroll down without moving cursor
C-eScroll up without moving cursor
z tPut current line to top of buffer
z bPut current line to bottom of buffer
z zPut current line to center of buffer
8 GGo to line by number
z cFold current block
z oUnfold/Expand current block
SPC t lToggle line wrap, similar to soft wrap seen in other editors
SPC t n vToggle line numbers

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 fNarrow to function/block
SPC n rNarrow to select region
SPC n wWiden, use this to undo narrowing

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 nSwitch to next theme and open themes transition state, use n/p to preview next/previous theme.

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

iedit

Key(s)Action
SPC s eEdit all occurrence of the word under cursor. Spacemacs will display the number of occurrences in the status bar. Use ESC to quit.

Kill ring

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

Magit, git

Key(s)Action
SPC g sShow git status
sAdd the highlighted file to staging
c cCreate a commit, edit commit message. Use , , to submit commit or use , k to cancel the commit.
qClose magit window

Files

Key(s)Action
SPC f fFind file, use C-h to go to parent directory
SPC p fFind file in current project
SPC p kKill project
SPC f e dOpen spacemacs configuration file
SPC f e RReload spacemacs configuration file

Treemacs

treemacs is for viewing and navigating project files.

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

Ranger

ranger is a file manager in emacs.

Key(s)Action
SPC a t r rOpen ranger buffer
hGo to parent directory
lJump to selected item
y yCopy file
p pPaste file
fSearch by file name

Buffers

Key(s)Action
SPC b bList buffers for current layout. (See the remarks below.)
SPC b BList 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 TABToggle buffer
SPC b dKill this buffer
SPC b nGo to next buffer
SPC b pGo to previous buffer
SPC b sGo to scratch buffer
SPC b YCopy current buffer
SPC f cSave current buffer as a new file
SPC b wToggle buffer as read-only
SPC f sSave current buffer
SPC f SSave all buffers
SPC f RChange file name of current buffer
SPC f y nCopy file name of current buffer
SPC b uReopen last killed buffer
SPC b C-dKill other buffers

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.

Layout

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

Key(s)Action
SPC l lList all layouts
SPC l sSave current layout to a file
SPC l LLoad layout from file
SPC l xKill current layout with all buffers

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)CommandAction
C-x r mbookmark-setCreate a named bookmark
C-x r bbookmark-jumpOpen a saved bookmark

Searching

Key(s)Action
SPC s sSearch with helm-swoop in current buffer
SPC s SSearch with helm-swoop in current buffer for current word
SPC s pSearch with helm-swoop in current project
SPC r lResume last search (helm-resume)
SPC s lRepeat 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 cClear 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.

Undo tree

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

Spacemacs Clojure mode key bindings

Key(s)Action
, 'Run Clojure REPL (sesman-start)
, e fEvaluate current s-expression and show result
, s fSend current s-expression to REPL and evaluate
, e vEvaluate expression before point (cider-eval-sexp-at-point)
, e lEvaluate expression at current line
, s bReload current buffer (cider-load-buffer)
, e bEvaluate current buffer
, s aToggle between REPL and Clojure source code
, s nChange REPL namespace to current buffer
, s lClear REPL
, m q qQuit REPL
, m q rRestart REPL (sesman-restart)
, h hShow doc
, e ;Evaluate current s-expression and print result as comment in a new line
, e wEvaluate last s-expression and replace the expression with the result
, e p fEvaluate current s-expression and pretty-print result in a new buffer
, e bEvaluate all code in buffer
, t aRun all tests
, m bBrowse active REPLs (sesman-browse)
, e eEvaluate expression before cursor
, e wEvaluate expression and replace by the results
, e mMacro-expand for immediate macro
, e MMacro-expand for all nested macros
, T eToggle enlighten mode with cider inspector
, d vInspect current symbol
, h acider-apropos, show list of Clojure symbols
, g rFind 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-jCycle up through history
C-kCycle down through history
,Open mini buffer showing cider commands. For instance, you can use clear command to clear REPL buffer.

List 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
LNext expression
HPrevious expression
SPC k wWrap expression with parenthesis
SPC k WUnwrap expression
SPC k yCopy expression
SPC k yCopy expression
SPC k sForward slurp expression (move inside the expression after the current s-expression )
SPC k d xDelete s-expression
M-tTranspose current s-expression with previous

Snippet

To use an existing snippets,

  1. Type the keyword defined in the snippet
  2. Use M-/ to expand with snippet
  3. 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
*** .

Zoom / Adjust font size

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

Org mode

Key(s)Action
SPC a o o nList all agenda and todos
g rIn org-agenda buffer, refresh agenda
C-c [org-agenda-file-to-front, add this file to agenda list
tToggle ToDo state selector
, b dCreate a code block
, d dCreate deadline for the current task
, d sSchedule the start date for the current task
, i bCreate a template block, it's more general than , b d
, T cToggle check box (a check box is simply a pair of brackets: [ ])
M-LeftPromote headings
M-RightDemote headings
M-S-LeftPromote headings with all its sub-items
M-S-RightDemote headings with all its sub-items
, i lInsert a link
, i LInsert a link, auto extract link title from the link
SPC a o lStore a link target (org-store-link), use , i l to insert the link
, d dAdd deadline/schedule a task
, s Aorg-archive-subtree, move current node to the archive file
, C porg-pomodoro
, s SSort subtree
, s nNarrow to subtree, use this to narrow buffer to current org subtree
, s wWiden, this is the opposite of ~, s n~
C-c @org-mark-subtree, select subtree
C-c C-x C-corg-columns view, press q to quit column view
SPC a o C iorg-clock-in
SPC a o C oorg-clock-out
SPC a o C gorg-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.

Journal mode

Key(s)Action
SPC a o j jCreate a new journal for current day
, pPrevious journal
, nNext journal

EasyPG Assistant

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

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

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:

  1. Press q q to start macro definition, all actions afterwards will be recorded.
  2. Preform the editing actions you'd like to record.
  3. Press q to complete the macro definition.
  4. Press C-x e to execute this macro.
  5. Press e to repeat this macro.
  6. 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.

Project

Key(s)Action
SPC p tShow 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 aToggle between source code and test file

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 dDelete current window
SPC w /Split window horizontally
SPC w VSplit window horizontally and focus the new window
SPC w [Resize current window
SPC w sSplit window vertically
SPC w SSplit window vertically and focus the new window
SPC t gToggle window size golden ratio

Toggle Emacs keybindings

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

Flycheck errors

Key(s)Action
SPC e nGo to next error
SPC e pGo to previous error
SPC e LGo to flycheck error list

Flutter

Key(s)Action
, x xFlutter run or hot reload
, g rFind references

Rust

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

Python

Install necessary dependencies:

pip install python-language-server
pip install importmagic epc

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)

Graphviz

Key(s)Action
, ,prview in emacs

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.

Comment