Vim Notes

Vim is a powerful text editor in Linux.

Introduction

Summarize some basic commands of Vim for quick review.

Commands

Movements

  • h      left

  • j      down

  • k      up

  • l      right

  • w      to the start of next word

  • e      to the end of word

  • $      to the end of line

  • 0      move to the start of line

  • number motion      repeat a motion many times

  • 2w      move two words forward

  • 3e      move to the end of the third word forward

  • CTRL-G      dsplay location in the file and file status

  • G      move to the end of file

  • gg      move to first line

  • number G      move to specific line

Insert and Delete

  • i      insert before the cursor

  • o      insert a new line below

  • O      insert a new line above

  • a      insert after the cursor

  • A      append after the line

  • x      delete the character at the cursor

  • d motion      delete to word motion type

  • dw      delete until the start of next word

  • d$      delete to the end of line

  • d number motion      delete to the word motion many times

  • d2w      delete until the start of next next word

  • dd      delete one line

  • 2dd      delete two lines

Change, Replace and Copy

  • u      undo previous action

  • U      undo all changes on a line

  • CTRL-R      undo the undo’s

  • p      put text after the cursor

  • r      replace the character under the cursor

  • R      replace until ESC

  • c [number] motion      change to the location of multiple times’ motion

  • ce      change until the end of word

  • c$      change to the end of line

Search and Substitute

  • /phrase      search forward for the phrase

  • ?phrase      search backward for the phrase

  • n      find next occurence

  • N      find last occurence

  • CTRL-o      back to older positions

  • CTRL-I      to newer positions

  • %      match (,),[,],{,}

  • :s/old/new      substitute new for first old in the line

  • :s/old/new/g      substitute new for old globally in the line

  • :#,#s/old/new/g      substitute between two lines

  • :%s/old/new/g      substitute in the whole file

  • :%s/old/new/gc      find occurences in the whole file and check whether to substitute

  • set ic      ignore upper/lower case

  • set is      show partial matches for a search phrases

  • set hls      highlight matching phrases

  • :q      quit

  • :q!      force to quit

  • :wq      save and quit

  • :w      save

  • :w FILENAME      write to specific file

  • v      visual selection

  • :r FILENAME      retrieve file and put it below the cursor

  • :r !dir      retrieve output of dir and put it below the cursor

Other Basic Commands

  • :!command      execute external command

  • :help      help

  • :help user-manual      user manual

  • ESC      normal mode

Reference

1
vimtutor