Post

Vim Cheatsheet

Introduction

Vim is mostly known by its memes on how nobody is able to quit (:q! btw). Here I want to demystify the rabbit hole of vim and create a Cheatsheet as a reference mostly, for myself.

The reasons why I want to use vim in my life more are the following. Reducing the reliance on the mouse is something I look forward to. To be clear, this is not about speed. While speed is cool and all, at the current stage of my life, my typing speed is not the bottleneck of my development. Simply the fact that I don’t have to use my mouse/trackpad is the goal. “Hand me a keyboard and leave me to it” is the idea. My current goal is to see a low percentage keyboard and not be afraid to use it, but rather excited.

This article will be a continuous work in progress, as I learn new things.


Modes

There are several Modes in vim. Every time you use vim, you are in one of those modes. After starting up vim, you are in “Normal Mode”. You cannot write text here, but you can navigate and edit your text. Here is a quick overview of the modes in vim

ModeKeysPurpose
NormalEscNavigate, modify text
Inserta, A, i, I, o, Owrite text
Visualv, Vhighlight text
Command:Enter commands e.G. save or quit

In normal mode, use h,j,k,l to move through the text. They correspond to ← ↓ ↑ →. Navigating in broader terms, my current ways are the following

KeysResult
CTRL + DMove half a page down
CTRL + U Move half a page up
ggmove to the first line
Gmove to the last line
:<number>jump to line number
0jump to first character in line
$jump to last character in line

Motions

Motions are a powerful way that vim uses to combine the keyboard shortcuts into keymaps. The best way to explain them is by example. Let’s look at d a p for example. d here stands for delete. a tells the deletion command to delete around something. p tells it to delete around the paragraph you are currently in. This can also be combined with curly braces or any other text object selector. In this post, you will learn more cool ways to use motions.


Ways to go to insert mode

As seen in the table above, there are multiple keys to enter Insert mode. Here is a quick table with explanations.

KeyResult
aappend text after cursor
Aappend text at end of line
iinsert text before cursor
Iinsert text before block (consecutive lines)
oopen line below
Oopen line above

Search, find and replace

There are several ways of searching for text. Here are some of the ways I use to find stuff around my text.

KeysPurpose
/open find menu, type what to search for
?open find menu, search backwards (I dont actually use this one)
ngo to next search result
Ngo to previous search result
*find all occurence of the word your cursor is at

Then replacing the text that was found is done with the c(change) key. A good motion to get started is c i w. This means “change in word” and will replace the word your cursor is at and put you in insert mode. After you made your changes you can hit esc to go back to normal mode, n to find the next occurence and hit . to replay the action you made before. This will rename the next occurence of your search to your newly given name.


Copy and Paste

The following commands are useful for copy and paste. Intuitively, copying in vim is named yanking. So y is used to yank text, p to paste.

KeyResult
y (normal mode)yank text. e.G. y w copies the word on the cursor
y (visual mode)yank highlighted text
*ycopy into system clipboard
ppaste text at cursor

Macros, repeating actions

Recording and playing back macros is a beautiful way to make vim actually be useful. You can record actions you take and then replay them with a shortcut. Here are some cool keybinds.

KeyResult
q<letter>start recording a macro, save it in the letters register. Valid registers are all lower case letters a-z.
q (when done recording)save macro
@<letter>replay macro at the letters register

This can also be connected with motions. e.G. 5 @ a replays the register a 5 times.


Random cool motions and shortcuts

Motions

MotionResult
gu$uncapitalize characters until end of line
gU$capitalize characters until end of line
guucapitalize whole line
gvreselect previous selection

Resources

Youtube: typecraft “30 Vim commands”

This post is licensed under CC BY 4.0 by the author.