Skip to main content

Vim


Starting & Exiting​

CommandAction
vi file.txtOpen file
:qQuit
:q!Quit without saving
:wSave
:wq / :xSave & quit
ZZSave & quit (fast)

Modes in VI​

ModeDescription
NormalDefault mode (navigation & commands)
InsertType text
VisualSelect text
CommandRun commands (:)

Switch modes:​

  • i → Insert mode
  • Esc → Normal mode
  • v → Visual mode
  • : → Command mode

Insert Mode Commands​

CommandAction
iInsert before cursor
IInsert at line start
aInsert after cursor
AInsert at end of line
oNew line below
ONew line above

Cursor Movement​

KeyAction
hLeft
lRight
jDown
kUp
wNext word
bPrevious word
0Line start
$Line end
ggFirst line
GLast line
:nGo to line n

Editing & Deleting​

CommandAction
xDelete character
ddDelete line
dwDelete word
DDelete till end of line
yyCopy line
ywCopy word
pPaste after
PPaste before
uUndo
Ctrl + rRedo

Search & Replace​

CommandAction
/wordSearch forward
?wordSearch backward
nNext match
NPrevious match

Replace:​

:%s/old/new/gc → replaces all occurrences of old with new in the entire file, asking for confirmation before each change
(: = command mode, % = whole file, s = substitute, g = global, c = confirm).


Visual Mode (Selection)​

CommandAction
vCharacter select
VLine select
Ctrl + vBlock select
yCopy selection
dDelete selection

File Operations​

CommandAction
:e fileOpen file
:w fileSave as
:r fileInsert file content
:!lsRun shell command

Useful Commands​

CommandAction
:set numberShow line numbers
:set nonumberHide line numbers
:syntax onEnable syntax highlight
:syntax offDisable syntax

Power Tips​

  • . → Repeat last command
  • 5dd → Delete 5 lines
  • 3yy → Copy 3 lines
  • % → Jump to matching bracket
  • J → Join lines

Memory Trick​

  • d → delete
  • y → yank (copy)
  • p → paste
  • i → insert
  • Esc → normal mode

Quick Summary​

  • i → insert
  • Esc → normal
  • :wq → save & quit
  • dd → delete line
  • yy → copy line

Quick Summary-2​

  • 'i'