vi is default advanced editor available in majority of the UNIX Operating System. It stands for visual Editor. It becomes very useful when you're using Terminal and want to access/edit file on UNIX system. Following are some of the useful vi commands which can be used for quick editing of file.

  • Open file for reading/editing.
    $ vi FILENAME.txt
  • To go to particular line, use Esc key, then type Colon (:), type the line number and press Enter (used as < return >) key.
    Esc :30< return >
  • To go to last line of file, type Shift+g and press Enter (used as < return >) Key.
    Shift+g< return >
  • To write in file, navigate to line where you need to write, then type i key and then start writing to file. NOTE: Make sure you've proper privileges to write to file.
    i
  • To Search for string, use Esc key, then type Slash (/), type string to search (keyword), press press Enter (used as < return >) key. Use n to find next matching keyword.
    Esc /[KEYWORD]< return >
  • To find and replace keyword, use Esc key, then type %s/, type string to search ABC, type string to replace with DEF and then press Enter (used as < return >) key. If you want to replace all occurrences of matching string, then append /g at end of line.
    Esc :%s/ABC/DEF/g< return >
  • To exit file without saving new changes. Type Esc key, then type Color(:), then type q and then press Enter (used as < return >) key. Use ! to forcefully close file.
    Esc :q< return >
  • To save new changes and exit file. Type Esc key, then type Color(:), then type wq and then press Enter (used as < return >) key. Use ! to forcefully close file.
  • Esc :wq< return >

Reference:

Tags: