Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Modes

  • Normal mode: where Vim starts. Keys move the cursor and run commands; they do not type text.
  • Insert mode: keys type text, as in any editor. Enter it with i; leave it with Esc.
  • Visual mode: select text, then act on the selection. Enter it with v.
  • Command-line mode: type : in Normal mode, then a command such as :w, and press Enter.

When in doubt, press Esc once or twice: you are back in Normal mode. Commands are case sensitive, so u and U do different things.

Save and Quit

CommandMeaning
:wSave
:wq or :xSave and quit
ZZSave and quit
:qQuit if saved
:q!Quit, drop changes
:w new.txtWrite a copy

More, including files you may not write to, in how to save and quit in Vim.

Moving Around

KeyMoves to
h j k lLeft, down, up, right
w / bNext / previous word
0 / $Line start / end
gg / GFirst / last line
42GLine 42
Ctrl+f / Ctrl+bPage down / up
%Matching bracket

Jumping to the end of a file is covered in how to go to the end of a file in Vim.

Editing

KeyMeaning
i / aInsert before / after cursor
I / AInsert at line start / end
o / ONew line below / above
xDelete character
ddDelete line
dwDelete word
DDelete to line end
cwChange word
rReplace one character
uUndo
Ctrl+rRedo

See how to delete a line in Vim and how to undo and redo in Vim for the variations.

Copy and Paste

KeyMeaning
yyCopy line
ywCopy word
p / PPaste after / before
dd then pCut and paste a line

Vim calls copying "yanking", and every delete also stores the text, so a delete works as a cut. Details in how to copy, cut, and paste in Vim.

Search and Replace

CommandMeaning
/errorSearch forward
?errorSearch backward
n / NNext / previous match
*Next match of word
:%s/old/new/gReplace in file
:%s/old/new/gcReplace, confirm each

Patterns and ranges are explained in how to search in Vim.

Files, Buffers, and Splits

CommandMeaning
:e fileOpen a file
:lsList open buffers
:bn / :bpNext / previous buffer
:splitSplit horizontally
:vsplitSplit vertically
Ctrl+w wNext window
:qClose window

Counts and the Dot Command

Most Normal mode commands accept a number in front: 3dd deletes three lines, 5j moves five lines down, and 2yy copies two lines. The dot key . repeats your last change. Delete a word with dw, then press . to delete the next one; change a word with cw, then move to another and press . to make the same change.

Visual Mode

KeyMeaning
vSelect characters
VSelect lines
Ctrl+vSelect a block
d / yDelete / copy selection
> / <Indent / outdent

Move the cursor with the usual keys to grow the selection, then press the action key. To practice all of this, run vimtutor, a short lesson inside Vim itself. It comes with the full Vim; on Debian and Ubuntu it is part of the vim-runtime package that the vim package installs, and the small vim-tiny package does not have it.

Was this answer helpful?
Loading...