Skip to content

Instantly share code, notes, and snippets.

@timjstewart
Created March 24, 2022 03:10
Show Gist options
  • Save timjstewart/4e9d3338e3c83d6a8d5943c0a505f295 to your computer and use it in GitHub Desktop.
Save timjstewart/4e9d3338e3c83d6a8d5943c0a505f295 to your computer and use it in GitHub Desktop.
Spacemacs Keys

Spacemacs Features To Add to My Workflow

Dedicating Buffers/Windows

Window to Buffer

Press SPC w t to dedicate the current window to the current buffer. This is helpful when you have an important buffer that you don't want to get replaced by another buffer.

To dedicate a window to its Purpose

Press SPC r d to dedicate the current window to its current purpose.

Evil/Holy Mode

Switching from Emacs to Evil Mode

Press M-m t E e when you are in Emacs (holy) mode.

Switching from Evil to Emacs Mode

Press SPC t E e when you are in Evil mode.

General

  • C-] - go to tag

Markdown

Press , c p to preview the current Markdown buffer in your browser.

Insert a Link

Press , i l to insert an HTTP link. For example:

go here

Surrounding Text

In Visual Mode

To surround the selected text:

  1. Select text in visual mode.
  2. Press s key.
  3. Press a character like '(', ')', '"', or even an XMl tag like "
    ".

For character pairs that have a different opening and closing character, use the closing character to avoid adding additional spaces.

Examples:

The [ and ] characters represent the selected region:

Before:

The [quick] brown fox.

After:

The "quick" brown fox.
Before (the entire line is selcted using Shift-v):

[The quick brown fox.]

After:

"
The quick brown fox.
"
Before:

The [quick
brown] fox.

After:

The "quick
brown" fox.

Surround selected text (multi-line version)

Unlike the last tip, this one forces the surrounding text to be on their own lines.

  1. Select text in visual mode.
  2. Press the gS key sequence.
  3. Press a character like '(', ')', '"', or even an XMl tag like "
    ".

This is especially helpful for Scala pattern matching. You can easily turn:

case Some(x) => Right(x)

into:

case Some(x) => {
  println(x)
  Right(x)
}

Just select the text: Right(x) and then type gS{ and then indent the code to your liking.

Examples:

The [ and ] characters represent the selected region:

Before:

The [quick] brown fox.

After:

The "
quick
"brown fox.
Before (the entire line is selcted using Shift-v):

[The quick brown fox.]

After:

"
The quick brown fox.
"
Before:

The [quick
brown] fox.

After:

The "
quick
brown
"fox.

To wrap some text in a function call:

  1. Select the argument(s) to the function call.
  2. Hit the 'sf' key sequence. Mnemonic: "surround Function"
  3. Enter the function name. Don't enter any parenthesis.
  4. Press ENTER

Examples:

Before:

[name]

After (using foo as the function name):

foo(name)
Before:

valid = [flag];

After:

valid = bool(flag);

** In Normal Mode

Similar to visual mode but the key sequences are different.

  1. Instead of 's' use 'ys'.
  2. Instead of 'gS' use 'yS'.

To change the surrounding characters use the 'cs' key sequence. Just put the point (represented by the '|' character) on the first of the surrounding strings. Then press the current surrounding character and then the surrounding character you want.

Examples:

Before:

The |'quick' brown fox.

After:

The "quick" brown fox.

Before (type 'cs<' to change the tags and make sure to enter the angle brackets (e.g. "<quote>")):

The |<bold>quick</bold> brown fox.

After:

The <quote>quick</quote> brown fox.

To delete the surrounding characters us the 'ds' key sequence.

Examples:

Before:

The |"quick" brown fox.

After pressing the `ds"` key sequence:

The quick brown fox.

Expanding the current region.

If you have a region selected, pressing SPC v will 1) expand the region and 2) display a transient menu that shows you other operations you can perform on the region.

This goes great with using the o key to position the point to the other end of the region.

Visualizing the undo tree.

Sometimes your undo history branches. With SPC a u you can navigate the undo tree and see what your buffer looks like each step of the way. Press q to quit.

Erasing the current buffer.

Press SPC b e and you will be prompted to see if you want to erase the current buffer.

List processes Emacs is running.

Press SPC a p and you'll see a buffer with the processes listed in it. Pressing d when point is on a process will kill that process without prompting you.

Press q to close the buffer.

Navigate the current buffer's hierarchy.

Press SPC b i to open a buffer that shows the current buffer's hierarchy. Navigate around and press ENTER to navigate to the selected portion of the document.

Quickly manipulate multiple buffers

Press SPC b . to get a transient menu that has some helpful options you can run to navigate, kill, bury, and relocate buffers.

Quickly go to the Messages buffer.

Press SPC b m a to show the Messages buffer.

Quickly go to the Scratch buffer.

Press SPC b s a to show the Scratch buffer.

Toggle buffer read-only state

Press SPC b w to toggle the read-only state of the current buffer. This has interesting behavior in a Dired buffer. It makes your directory editable.

Reopen the last closed buffer

Did you accidentally close a buffer? Press SPC b u and the last buffer you closed will re-open. This won't bring back changes made to a buffer if you didn't save them and deleted the buffer.

Open IBuffer

Press SPC b I to open Ibuffer. Ibuffer has lots of functionality. You can mark buffers and save, revert, kill, etc. marked buffers.

Hiding comments

Want to temporarily hide comments to focus on the code? Press SPC c h to hide comments. Press the same sequence again to reveal the comments. Of course, this is only useful if the code has comments.

Errors

To see the list of errors for the current buffer, press SPC e l. If you want to see the errors and navigate through the error list, press SPC e L instead.

To see a transient menu that lets you easily navigate your errors in the current buffer, press SPC e ..

Yanking parts of the file path.

To yank the full path to the current buffer's file, press SPC f y y.

To yank the directory of the current buffer's file, press SPC f y d.

To yank the file name of the current buffer's file, press SPC f y n.

To yank the file name (without the file extension) of the current buffer's file, press SPC f y N.

Opening files in an external application

Press SPC f o to open the current buffer's file in an external application.

Deleting the current file

Press SPC f D and you will be prompted to delete the current buffer's file. If you proceed with the delete, the current buffer will be deleted.

Renaming/Moving the current file

Press SPC f R and you will be prompted for a directory and filename to rename the file to. If any directories in the path you supply don't exist, Emacs will offer to create them for you.

Creating a temp file

If you want to create a temp file to experiment on, press SPC f J and you will be prompted for a file extension. If you enter an extension and press ENTER, Emacs will create a junk file for you and visit that file.

Jumping Around

To a visible line

Press 'SPC j l' to enter an interface for selecting a LINE that's currently visible.

To a visible word

Press 'SPC j w' to enter an interface for selecting a WORD that's currently visible.

To your last change

Press SPC j c to jump to the location of your last change.

Narrowing

To selected region

Press SPC n r to narrow to the selected region

To current function

Press SPC n f to narrow to the current function.

To widen (undoing a narrowing operation)

Press SPC n w to widen to the entire buffer.

Projects

To visit the project's TODOs.org file

Press SPC p o to open the current project's TODOs.org file.

Navigating to a Line

By interactive search

Press SPC s s to start searching through lines of the current buffer by text or regexp.

By interactive search across all buffers

Press SPC s C-s to start searching through lines of the all buffers by text or regexp.

Exchanging two regions of text

  1. Select the first region.
  2. Press g x.
  3. Select the second region.
  4. Press g x.

Copy the selected lines and comment them out.

If you've ever wanted to make some experimental changes but "keep a backup", of the lines, select the lines you want to modify and press g y.

To enter Insert mode the last place you were inserting text

Press g i.

Dragging Text

Up

Press SPC x K. You can repeatedly press k and j to move the text up and down respectively.

Down

Press SPC x J. You can repeatedly press j and k to move the text down and up respectively.

Helm

Navigate through history

Press M-n and M-p navigate forwards and backwards through history.

Select multiple files

Press C-SPC to select the current file and move to the next file.

Preview the current file

Press TAB to preview the current file.

Insert the current/selected file(s) into the current buffer.

Press C-c C-i to insert the the Helm selection into the current buffer.

Page up and down in the Helm buffer

Press C-v to scroll down and M-v to scroll up.

Move the helm window

Press C-t to move the Helm window to a vertical window and back to horizontal.

Un-filed

** f

  • t - find project file in Treemacs ** g - version control
  • / - git grep
      • git grep at point
  • b - blame transient state
  • m - magit dispatch
  • r - smerge transient state
  • t - time-machine (AWESOME) *** f - file - l - log for commits for this file *** j - d/D - dired same/different window - f - jump to lisp function - I - imenu all buffers (Just try it!) *** o - org ** n - narrrow/numbering
      • increase number
      • decrease number ** p - Project
  • a - toggle between implementation and test
  • b - switch to project buffer
  • o - project todo list ** r - rings / registers
  • y - kill ring
  • e - show registers
  • P - set window purpose
  • s - last search buffer

** s

  • s - swoop
  • C-s - swoop all buffers
  • r,b - search buffers
  • g
    • p - select the last pasted region.
    • t/T - go to the next/previous tab.
    • M - go to middle of line
    • & - repeat global substitute
    • ; - go to last change
    • i - insert resume ** x - text
    • t,w - transpose words
  • Org mode ** g
    • h - go up element
    • k - go backward element
    • j - go forward one element same level ** , *** * - toggle heading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment