This is a superb overview! I've used Vim for about 2 years now, but I still learned a bunch of things from this post that I didn't pick up from other cheatsheets or articles.
My 2-cents: Vim itself is powerful as an editor, but I always missed some IDE features. What I've come to realise is that the real power of Vim is not the editor, but the keybindings. I installed the Vim extension in VSCode some time ago and have loved the hybrid workflow. Since then, I've been gradually incorporating Vim keybindings into all the tools I use for text — like Overleaf for writing papers in LaTeX and Zettlr for writing notes in Markdown. I still use Vim itself for small scripts and quickly editing files. It's so powerful being able to go between applications and never have to think about what your fingers are doing to transform ideas into output.
One thing I still haven't quite figured out is in-browser text entry. So far, I haven't liked the solutions I've found, but it's something I'm looking into for the future. Writing this comment without my usual keybindings is... slow.
Yes! Even many websites and web apps implement some Vim standards. Particularly \ for search.
Vim is a terminal-based editor optimized for speed. Keys are hotkeys by default. Vim does not even use
Ctrlfor most of them. Maneuvering via hotkeys is so efficient mouse input is often disabled by default.This guide focuses on the features I use most frequently. It has nothing about with native Vim windows because I use i3 or Spacemacs instead. If you feel I left out a valuable feature then please let me know about it in the comments.
Movement
The simplest way to move is via the
hjklkeys.The
hlkeys operate within a single line. Pressinghat the start of a line does nothing. Pressinglat the end of a line does nothing. By line I mean a string of characters ending with a carriage return. The fact that a long paragraph appears to wrap around on your screen is irrelevant.The
jkkeys move the cursor down and up a single line. If the cursor is in the 3rd column of the 6th line then pressingjmoves the cursor to the 3th column of the 7th line.Most commands can be prefixed with a number.
4hmoves the cursor left 4 characters.101jmoves the cursor down 101 lines.A sequence of nonempty lines is called a paragraph. The
{}keys move the cursor forward and back by paragraphs.There are two kinds of "words" in Vim. A word is a sequence of alphanumeric characters. A Word is a sequence of non-whitespace characters. The
wbekeys move the cursor around words. TheWBEkeys move the cursor around Words.The
#*keys move the cursor forward and backward to the next "idenfitier". If the cursor hovers over the word "marmot" then pressing*will move the cursor forward to the next instance of the word "marmot" and pressing#will move the cursor backward to the previous instance of the word "marmot".The
fkey takes a character as an argument and moves the cursor forward to that character. For example,famoves the cursor forward to the next instance ofa. Thefkey operates within a line. It will never take the cursor to another line. TheFcommand is likefexcept it searches backward. ThetandTcommands are likefandFexvcept they move the cursor one fewer character.The
;command repeats the most recentfFtTcommand. The,command is like;except backwards. If you use anFcommand followed by a,then the double-backwardsness will cancel itself out.fFtTcommandfFtTcommand except reversedThe
0^$keys move the cursor to the beginning and end of a line.The
HLMkeys move the cursor around relative to the viewing window itself.You can move the viewing window itself with
zandCtrlkeys.You can move the cursor itself up and down visible lines with the
gprefix.The
gkey can also go to a particular line.The
|key is likegexcept for columns.The
/key performs search. You type/then the string you are searching for and then pressEnter. The?nNkeys are analogous toF;n.The
()characters move forward and backward one sentence.The
[]characters move the cursor forward and backward to a variety of things. My favorite uses of[]is to go forward and backward to matching parenthesis. For example,])moves forward to the next unmatched closing parenthesis. It is indispensable when writing Lisp. Inner quotes and tags behave similarly to parentheticals.You can (invisibly) mark places in your buffer to return to later. You can set one mark per letter. Lowercase letters a-z are buffer-specific e.g. each buffer can have its own "a" mark. Uppercase letters A-Z are global e.g. only one buffer at a time can have an "A" mark.
Some marks are populated automatically. Of the automatic marks, I only use
''. You can find the others here.Editing
Now that you know how to move, we can edit some text. Most of the time you edit text you should use the
dypkeys.dstands for delete, which is similar to "cut".ystands for yank, which is similar to "copy".pstands for put, which is similar to "paste".The
dkey operates on text objects and movement commands. For exampledtAdeletes everything up to (but not including) the next "A" on the current line.Every delete operation operates on characters or lines.
dtAoperates on characters and thus behaves similarly to other editors. A delete operation which operates on lines deletes an integer number of lines. Delete operations which operate on lines delete the current line and the destination line. Thus,djdeletes the current line and the next line.yis identical todexcept it leaves your buffer unchanged.pputs the selection before the cursor.Pputs the selection after the cursor. "Before" and "after" refer to characters or lines depending on whether you deleted/yanked characters or lines.By default,
pandPwill put the last text you deleted or yanked. You can save text into registers by prefixing"<register>before your delete or yank command. For example,"adddeletes the current line and saves it into register "a".There is one register for each letter a-z. If you use a capital letter then your delete or yank command will append to the register instead of overwriting it. You can put from a register by prefixing your
porPcommand with the register. For example,"ap"will put from register "a".You can find a list of special registers here.
Text Objects
Remember how I said you could follow
dandywith a movement command or a text object? Remember from earlier the concept of "words" and "paragraphs"? Here we put them together into a simple grammar.dadeletes a text object.You can replace the
awith anito delete an inner word, an inner paragraph, etcetera. An inner text object is just like the regular text object except it does not include any surrounding delimiters. In the case of a parenthetical, the "inner parenthetical" does not include the outside parenthesis. For words/sentences/paragraphs, the delimiter is whitespace.Other text objects include
'"[]{}<>. They all do what you would expect them to do.trefers to an HTML-like tag.Insert Mode
You cannot write everything by yanking and putting pieces of existing text together. Sometimes you have to insert text into a document. Several different keys drop you into insert mode.
You can exit insert mode by pressing
Escapebut it is faster to remap yourCapsLockkey toCtrland then exit insert mode withCtrl-[. Some hotkeys are valid only while in insert mode.The
ckey is likedexcept it drops you into insert mode afterward.After you exit insert mode, the whole insertion counts as a single edit. So if you type
ci(followed by an insertion then the entire replacement of text inside the parenthetical counts as a single edit. You can repeat an edit with the.operator.The
.key might be the most important key in all of Vim. Generally-speaking, the more you use the.operator the better you are at Vim.Miscellaneous edit commands
The Undo Tree
uandCtrl-roperate like the undo and redo stack you are familiar with.If you undo a long series of edits and then mistakenly make an edit you can undo the damage with
g+andg-which traverse the nodes of the tree in chronological and reverse-chronological order.Macros
A macro is a series of keystrokes. Macros are good for automating repetitive tasks, especially editing structured text.
To define a macro, start by pressing the
qkey. Then pick a letter a-z at which to save the macro. Then execute the macro manually. When you are done typing the macro, pressqagain.Once you have a macro defined, you can press
@followed by the macro's letter to execute the macro.Macros are well-behaved. If a macro modifies a line and then goes down one line and you tell Vim to execute the macro 1000 times but your buffer only has 700 lines then Vim will stop when it gets to the end of your buffer.
Visual mode
Visual modes are similar to highlighting. Visual modes have their uses, but it is usually faster to avoid them.
Find and Replace
You can find and replace all the instances of a string by typing
:%s/followed by the original string followed by/followed by the replacement string followed by Enter…assuming you did not type any escape sequences. Find and replace is a complex subject I will not delve into here even though I do use it.Enter and exit
You can enter Vim by typing
viminto your terminal followed by the file you would like to create or edit. You can exit vim by typing:followed by a quit command.Typing
ZZdoes the same thing as the:wqcommand.Cheatsheet
If you are not from a Unix back then the term "buffer" may be unfamiliar. Just translate "buffer" to "file" or "document" in your head even though, technically, a buffer is more general than a document. ↩︎