Vim Tutorial

Vim Tutorial

This tutorial includes some basic vim commands and I hope that it will be helpful.

Moving the cursor

  • h : left
  • j : down
  • k : up
  • l : right

It takes time to get used to it.

  • w: move the cursor one word forward (to the first letter)
  • b : one word backward (also to the first letter)
  • e : one word forward (to the last letter)
  • fx : forward to the letter x
  • ( : to the start of the sentence
  • ) : start of the sentence
  • 0 : start of line
  • $ : end of line
  • { : start of paragraph
  • } : end of paragraph
  • G : end of file
    • ctrl+G : to see the cursor location and file status
  • gg : start of file
  • xG : to the number x line of file
    • typing a number before a motion repeats it that many times!

Delete

  • x: delete the character at the cursor
  • dw: delete all the characters between the cursor and the first letter of the next word
    • e.g. Please delete the word. (Assume the cursor is at l)
    • After you press dw, the sentence becomes Please dethe word delete
  • de: delete all the characters between the cursor and the next space
    • e.g. Please delete the word. (Assume the cursor is at l)
    • After you press de, the sentence becomes Please de the word delete
  • d$ : delete to end of line
  • dd : delete whole line
  • p : After you delete something, press p to paste things you delete wherever you like.

Insert

  • a : insert after the cursor
  • A : insert after the end of line
  • i : insert before the cursor
  • I : insert before the start of line
  • o : insert in the next line
  • O : insert in the previous line
  • /yourSearchString + <Enter> : search for yourSearchString
    • n : to search for the same string again (press <Enter> to exit)
    • N : to search for the same string again, but in opposite direction
    • ctrl+o : to go back to where you came from
    • ctrl+i : to go forward
    • set option
      • :set ic : ignore case
      • :set noic : disable ignore case
      • :set hls : highlight the matches
      • :set nohls : disable highlight matches
      • :set is : increase search
      • :set nois: disable increase search
  • % : move the cursor to the other matching parenthesis

Replace

  • rx : replace the character at cursor with x
  • ce : almost the same as de, but this time will place you in Insert Mode
  • s/old/new : replace the first occurrence of ‘old’ with ‘new’
  • s/old/new/g : replace all occurrence of ‘old’ with ‘new’ in one line
  • #,#/old/new/g : #,# are the line numbers of the range of lines where the replace should be done
  • %s/old/new/g : replace all occurrence of ‘old’ with ‘new’ in the whole file
  • %s/old/new/g : replace all occurrence of ‘old’ with ‘new’ in the whole file, with a prompt whether to replace or not

Undo & Redo

  • u : undo the last command
  • U : undo the command excuting on the while line
  • ctrl+R : redo the command

Copy & Paste

  • y : to copy
  • p : to paste
    • e.g. Start Visual Mode with v and move the cursor to chose whatever you want, type y to copy the highlighted text and type p to paste the text.

Others

  • . : repeat the last command
  • <start position><command><end position> : many commands follow this pattern
    • e.g. 0y$ means copy the whole line
      • 0 move the cursor to the start of line
      • y copy
      • $ move the cursor to the end of line
  • ctrl+n : auto complete
# vim
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×