Skip to content
Independent notebook · software, testing, and open source No ads · no sponsored posts · no affiliate links
Soham Parekh software notebook

Coding note / Editor

Vim

A small set of Vim commands that covers most editing, plus the mental model that makes them stick.

Shelf Editor

Format Reference

All notes

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 i to enter it and Escape to leave.
  • Visual mode is where you select text. Press v for characters or V for 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

  • h j k l move left, down, up, and right.
  • w moves to the next word, b to the previous word, and e to the end of the word.
  • 0 goes to the start of the line and $ goes to the end.
  • gg goes to the top of the file and G goes to the bottom.
  • Ctrl and d moves down half a page; Ctrl and u moves up.

Edit

  • i inserts before the cursor, a inserts after it, and o opens a new line below.
  • x deletes one character. dd deletes a line and yy copies one.
  • p pastes after the cursor. u undoes and Ctrl and r redoes.
  • cw changes a word and r replaces 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.

vim command
:%s/old/new/g

Files and windows

  • :w writes the file, :q quits, and :wq writes and quits.
  • :q! quits without saving.
  • :e file opens another file.
  • :sp splits horizontally and :vsp splits vertically.
  • Ctrl and w followed by a direction moves between windows.

A small starter configuration

vimrc
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.

Field note

The goal is not to memorise every command. It is to keep your hands on the keyboard and your attention on the text.

Reference shelf

Official documentation and further reading for this note.