Vim basic Commands / file operations

Edit a file with vim

  • open the file with vim filename
  • Go to Insert mode press “i”
  • Update the file content.
  • Press ESC then write :wq to save and quit the editor

Copy or Cut and Paste a Line in Vim

  • Bring your cursor to the line you want to copy
  • Press yy and line will be copied or Press dd to cut a line
  • Bring your cursor to the line where you want to paste
  • Press p and line will be pasted there.

Find and Replace in Vim

  • Press ESC then perform the following
  • :%s/find/replace/g
  • %s here represents whole file you can also specify :5,6s it will only search and replace in line from 5 to 6.
  • /g represents replacing of all the occurrences of a string. If you don’t specify /g it will only replace the first occurrence.

Search / Find in Vim

  • Press ESC then type :/findword hit enter.
  • After performing above you can go to the next occurrence of that word by pressing n and to perform reverse search hit N.

Navigating in file using VIM

  • Press ESC the G to go to the end of the file.
  • Press ESC then gg to go to the top of the file.
  • ESC then :se nu then hit enter to show line number of the file.
  • ESC then :[number] to go to that particular line. [number] represents the line number here.
  • h move one character to the left
  • l move one character to right
  • j moves one line upwards
  • k moves one line downwords

3,479 Replies to “Vim basic Commands / file operations”