Vim is an editor built around modes. Once the modes make sense, the commands stop feeling like a secret code and start feeling like a language. This note is the short version I would give to someone who wants to be productive this week, not in six months.
Modes
- Normal mode is where you move and issue commands. It is the mode Vim starts in.
- Insert mode is where you type text. Press
ito enter it andEscapeto leave. - Visual mode is where you select text. Press
vfor characters orVfor lines. - Command mode is where you save, quit, and run substitutions. Press
:from normal mode.
Most confusion comes from typing in the wrong mode. If the keys are inserting themselves into the document, press Escape and start again.
Move
hjklmove left, down, up, and right.wmoves to the next word,bto the previous word, andeto the end of the word.0goes to the start of the line and$goes to the end.gggoes to the top of the file andGgoes to the bottom.Ctrlanddmoves down half a page;Ctrlandumoves up.
Edit
iinserts before the cursor,ainserts after it, andoopens a new line below.xdeletes one character.dddeletes a line andyycopies one.ppastes after the cursor.uundoes andCtrlandrredoes.cwchanges a word andrreplaces one character.
Search and replace
Type / followed by a pattern to search forward. Press n for the next match and N for the previous one. For a file wide replacement, use the command below and add a confirmation flag if the change is risky.
:%s/old/new/g
Files and windows
:wwrites the file,:qquits, and:wqwrites and quits.:q!quits without saving.:e fileopens another file.:spsplits horizontally and:vspsplits vertically.Ctrlandwfollowed by a direction moves between windows.
A small starter configuration
set number
set relativenumber
syntax on
set expandtab
set shiftwidth=4
set tabstop=4
Line numbers and relative line numbers make vertical motion easier. Expanding tabs to spaces keeps a file portable across editors. Change the width only when the project has a convention.
Practice
Open a scratch file and move through it without arrow keys. Edit a line, undo it, search for a word, and save. The first session will feel slow. The second will feel less slow. That is the whole method.
The goal is not to memorise every command. It is to keep your hands on the keyboard and your attention on the text.