Skip to content

Vim Quick Referenceλ︎

A reference of the most common keybindings available in Vim Normal mode.

. repeats the last keybinding sequence used in Vim Normal mode or a change made within a complete Vim Insert session.

Moving aroundλ︎

In normal mode you can keep your fingers resting on the main row of your keyboard to move around.

Key action
h move cursor left one character
j move cursor down one line
k move cursor up one line
l move cursor right one character

Ctrl k / j to move the cursor up or down in a menu.

Key Action
f to next character (you specify)
t to just before the next character
; repeat f or t search
w start of next word
W start of next word, white space delimited
e end of current word
b start of previous word
W end of next word, white space delimited
* to next matching symbol name
$ end of current line
0 start of current line
^ start of non-whitespace
% jump to matching parens or next closed paren
Key action
gg start of buffer
G end of buffer
H move cursor to head of buffer
M move cursor to middle of buffer
L move cursor to bottom line of buffer
C-u jump up half a page
C-d jump down half a page
} move cursor forward by paragraph or block
{ move cursor backward by paragraph or block
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 to the last place you were
[{ jump back to the "{" at the beginning of the current code block
SPC j i jump using helm list of headings / functions
SPC j j avy-jump to character (specify)
SPC j l avy-jump to line
C-o jump back to previous cursor location (evil-jump-backwards)
C-i Go to newer position in jump list (opposite of C-o)
: 4 go to line 4

Searchingλ︎

/ for a basic search for the current buffer

Space s b for a buffer search with popup results that can be navigated with Ctrl j / k

Space / to search across the current project. Matches are listed in a popup buffer.

  • Ctrl+c Ctrl+e opens the search results in an editable buffer.
  • Ctrl+c Ctrl+c to commit changes
  • Ctrl+c Ctrl+k to cancel changes.

Boundary for search pattern

\b defines a boundary around the search term, limiting the matches to full word. For example search\b will match search, but not searching.

Key Action
* search forward for the word under the cursor
# search backward for the word under the cursor
/ search forward in current buffer
? search backward in current buffer
n once searching: find forward
N once searching: find backwards

Text Editingλ︎

The following commands put you into the Evil Insert state

Key Action
i insert state at cursor
I insert state at start of line
a append - insert state after cursor
A append - insert state at end of line
o new line after cursor
O new line before cursor

Return to Normal stateλ︎

normal should be the state used most of the time, except when adding new text in insert state. As soon as typing new text is done, form a habit of switching back to normal state.

Esc or press f d keys in extremely quick succession, to switching from insert to normal state.

fd - Evil Escape shortcut

practicalli/doom-emacs-config sets evil escape key binding to fd

emacs title=".config/doom/+bindings.el (after! evil-escape (setq evil-escape-key-sequence "fd")) f d in rapid succession is low risk alternative to pressing Esc as if not typed correctly then either the next d character is searched for on the current line (as f moves to the next character), or nothing happens.

u to undo any unwanted results

Copy, cut, paste, undo, redoλ︎

v in Vim normal mode changes to Visual select mode. Use the navigation keys or any other movement keys to select text to copy or cut.

Key Action
y copy (yank) selection and add to kill ring
x delete character at point and add to kill ring
X delete character before point and add to kill ring
p paste last entry from kill ring
p Meta+y paste with kill-ring history menu
u undo
Ctrl+r redo
Space i y insert value from kill ring history

Undo tips

Undo reverts the last action in normal mode or all the changes made in insert state

Replace and changing textλ︎

Key Action
r replace the character under cursor
R replace multiple characters until ESC
cw change word from cursor to end
4 c w change 4 words
v e select from cursor to end of word
v i w select current word anywhere within the word
v i w c change current word/region
v i w d delete current word/region
d e delete from cursor to end of word
d w delete from cursor to start of next word
C change from cursor to end of line
D delete from cursor to end of line
d $ delete from cursor to end of line

Delete commandsλ︎

Key Action
de delete to end of word, not including space
dw delete to end of word, including space
d$ delete to end of line
dd delete the current line
4 d w delete 4 words
4 d $ delete 4 lines to end
dt delete to a character (not including character)
dab delete a whole block / expression
dib delete contents of a block / expression
cab change all the block / expression
cib change inner block contents / expression
yab yank all block / expression
yib yank inner block contents / expression

Repeat commandsλ︎

Key Action
. repeat last command again
<number> <cmd> repeat command a number of times

The . keybinding will repeat the last command in normal mode or the last text edit in insert mode.

Type a number before a command and that command will run that number of times.

Hint::Inserting a comment borderλ︎

Use the number repeat to create a border of 42 ; characters. Type 42 to repeat the command 42 times Press i for insert mode Press ; as the character to repeat insert Press ESC or fd to leave insert mode and insert all 42 ; characters

Transposing / swapλ︎

Key Description
x p or SPC x t c transpose the current character with the next character
SPC x t w transpose the current character with the previous character
SPC x t l transpose current line with previous line
SPC k tf transpose expression

Comments - works for all major modesλ︎

SPC ; ; or g c c to comment out the current line

SPC ; or g c to comment out the currently selected region

M-; creates a ;; comment on an empty line, or ; at the end of the text of the current line.

To comment multiple lines you can use the repeat command style, especially useful if you are using relative line numbers.

g c 3 j will comment the current line and the following two lines below. Comment in reverse using g c 3 k.

In Visual state, v, select the lines you wish to comment and use g c to comment all the marked lines. Partially marked lines are not commented.

http://spacemacs.org/layers/+vim/evil-commentary/README.html

Working with Buffersλ︎

To work with files in Emacs, they are first loaded into a Buffer.

So when you open a file with M-x find file or SPC f f a new buffer is created so you can manage that file.

Buffers are displayed in a window and you can change the window to show any of the current buffers.

Here are the most commonly used buffer commands

Key Command Description
SPC b b helm-mini List current buffers
SPC b d kill-this-buffer Kill current buffer
SPC b h home Switch to Spacemacs home page
SPC b n next-buffer Switch to next buffer
SPC b p previous-buffer Switch to previous buffer
SPC b s switch-to-scratch-buffer Switch to the scratch buffer
SPC b Y copy-whole-buffer-to-clipboard Copy current buffer contents to kill ring

See the full buffer menu via SPC b

Spacemacs Buffer Menu

Managing Filesλ︎

Key Description
SPC f c copy file - save current buffer with a new file name
SPC f R change file name of current buffer
SPC f y n yank file name of current buffer

Also consider using treemacs and projectile

Quit or Restart Emacsλ︎

I recommend using the Spacemacs menu from normal mode to quit / restart Spacemacs.

Key Action
SPC q q Quit Spacemacs
SPS q r Restart Spacemacs keeping current window and buffer layout
SPS q R Restart Spacemacs

Hint::Work in progress - sorryλ︎

The rest of this section is work in progress and a little messy.

Classic Vim commandsλ︎

Todo::Reference sectionλ︎

The classic vim commands are also available, however, I dont think they are as nice to learn. Adding as reference for those used to Vim already and would miss them.

Substitutionλ︎

key Action
:s/old/new substitute new for the first old pattern
:s/old/new/g substitute new for all 'old's on a line
:#,#s/old/new/g substitute phrases between two line #'s type
:%s/old/new/g substitute all occurrences in the file
:%s/old/new/gc as above, asking for confirmation each time

External commandsλ︎

You can run external commands using :! followed by a command. For example:

:!ls - run the ls command

Spacemacs shell can be configured to pop-up a shell buffer to run commands from the directory of the current buffer.

Spelling Checkingλ︎

As you are typing in any(?) buffer then Spacemacs is checking your spelling as you type. If you type a word that Spacemacs does not recognise then that word is underscored in red. To correct a spelling:

  • Move to the word that is incorrectly spelt
  • SPC S c to change the word and show a list of suggested words
  • Either type in a new spelling or use C-j and C-k keys to move down and back up the list of words, using RET to select that word.

Scrollingλ︎

In evil command mode you can scroll just the text using the classic vi bindings. This does not scroll the cursor unless the cursor hist the top or bottom of the window, in which case the cursor stays at that window boundary.

  • C-y - scroll down
  • C-e - scroll text up

Jump aroundλ︎

SPC j i shows imenu that lists all the def and defn expressions in the current buffer, allowing you to jump to one of these definitions directly by typing its name or using C-j and C-k to select the name and pressing RET to jump to the name in the buffer

SPC j l labels each line of the buffer with a short, unique character code. By entering the character code you are taken directly to the specific line. This is the avy goto line function and this works on all visible buffers by default, although it can be just shown on the current buffer (TODO: how is this set).

Searchingλ︎

Search with SPC /