Cheat sheet: vi text editor

What is vi?

"vi is a screen-oriented text editor originally created for the Unix operating system. " -Wikipedia

Basic vi commands

Opening a file:

From a terminal on linux or Mac OSX:

$ vi myfile.txt

This will change your terminal into the contents of the text file document. If the document doesn't exist yet, or is empty, the screen will be almost all blank:

~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
"myfile.txt" [New File]                                                                                0,0-1         All

Closing a file withOUT saving:

From within the opened vi file, type:

:q!

Closing a file WITH saving:

:x

Editing a file:

To edit a file, first open with vi and then type:

i

This will allow you to make any changes that you want. When finished, press:

Esc

This will take you OUT of editing mode.

List of vi commands:

  • Edit: i
  • Stop editing: Esc
  • Save file: :x+return
  • Quit, no save: :q!+return
  • Go to line #: :[line number]+return
  • Find string: /[string]