VIM Cheat Sheet - abhishekkhare/UNIX GitHub Wiki

Editing Files With VIM

Navigation Keys

  • h -> Move Left.
  • j -> Move Down.
  • k -> Move Up.
  • l -> Move Right.

Modes

  • i -> Insert Mode.
  • ESC -> Command Mode.

Quitting and Saving

  • :q -> To Quit(This is only possible if no change has been made)
  • :q! -> To Quit and ignore the changes that have been made since the last save.
  • :w -> To Save
  • :wq -> To Save and Quit.

Advanced Insertion Mode

  • i -> allows editing from the current position of the cursor.
  • I -> Takes the cursor to the beginning of the line and goes into Insert Mode.
  • a -> Append, allows editing from next to the current location of the cursor.
  • A -> Takes the cursor to the end of the line and goes into Insert Mode.
  • o -> Adds an empty new line after the current cursor line, and moves the cursor to the new line.
  • O -> Adds an empty new line before the current cursor line, and moves the cursor to the new line.

Replacing, Changing, and Word Movement

  • r -> Replace a single Character. (After r, press the character u want to replace with)
  • R -> Takes you into a Replace Mode, you can continue to replace multiple characters, unless you exit the Replacement Mode.
  • w -> cursor skips from one word to another and goes to the beginning of the word(by punctuation).
  • W -> cursor skips from one word to another and goes to the beginning of the word(by space).
  • e -> cursor skips from one word to another and goes to the end of the word(by punctuation).
  • E -> cursor skips from one word to another and goes to the end of the word(by space).
  • c -> Change whatever we specify, it is used in combination with other modifiers. Try c + e.
  • C -> Deletes the entire line.

Deletion

  • x -> Deletes the current cursor character.
  • X -> Deletes the before cursor character.
  • d -> deleted whatever we specify, it is used in combination with other modifiers. Try d + e.
  • D -> Delete to end of current line.

Copy, Cut, and Paste (Works only within the VIM)

  • y -> Yank(Copy) whatever we specify, it is used in combination with other modifiers. Try y + e + p. Y - Yank the current line. Try Y + p. p - Paste after the cursor's position. P - Paste before the cursor's position. d - Delete whatever we specify, it is used in combination with other modifiers. Try d + e + p. D - Delete to the end of current line (But also copy it - 'Cut').

Count Modifiers and Skipping Lines

  • gg -> Skip to the beginning of a document.
  • G -> Skip to the end of a document.
  • number + G -> Skip to that line number.
  • number + j -> Go number lines below the cursor location. Similarly number + another command.
  • d + number -> deletes the number of lines from the current position of the cursor.
  • d + gg -> delete everything above the current cursor line.

Searching and Replacing

  • / -> Search some text in forward direction.(Special Character would require escaping with ).
  • ? -> Search some text in the reverse direction.
  • n -> next instance of the searched text.
  • N -> previous instance of the searched text.
  • :beg,ends/query/replace/flags -> Search and Replace syntax.
  • :1,10s/carrot/bacon/ -> Go thru line 1 to 10 find every first occurrence of "carrot" in a line and replace it with bacon.
  • :1,10s/carrot/bacon/g -> Go thru line 1 to 10 find every occurrence of "carrot" in a line and replace it with bacon.
  • :%s/carrot/bacon/g -> Go thru entire document and find every occurrence of "carrot" in a line and replace it with bacon.