vi Text Editor

Cheat Sheet

Open vi

  1. Establish an SSH connection to the server.
  2. Type vi at the command line, or type vim to open vi with syntax highlighting

Text Entry Mode

To manipulate text within a file, vi must be in text entry mode. Hit the insert key to enter text entry mode. The default operation will be to insert text. To replace text, hit insert again.

  • To enter text entry mode and insert text, press the Insert key.
  • To enter text entry mode and replace text, press the Insert key twice.
  • To exit text entry mode, press the Esc key
  • The following commands are to be used when the editor is not in command line mode or text entry mode. If you are unsure, hit Esc before running the command.

    Note: Keystrokes for the following commands do not display within vi. The command will 'know' and respond when the final character of the command is given.

    Search

  • /search_query
  • Delete Line

  • dd
  • Delete Multiple Lines

  • num_lines dd (e.g. 12 dd)
  • Undo

  • u
  • Copy a Line

  • yy
  • Copy Multiple Lines

  • num_lines yy (e.g. 12 yy)
  • Paste

  • p
  • Command Line Mode

  • Type SHIFT+; (:) to enter command line mode.
  • To exit command line mode, press the Esc key
  • Search and Replace

  • %s/old_string/new_string/g
  • Remove Windows newline character - ^M

  • %s/CTRL+V(^)CTRL+M(M)//g [%s/^M//g]
  • Display Line Numbers

  • set number
  • Jump to a Line

  • line_number (e.g. 42)
  • Save File

  • w
  • Quit

  • q
  • Quit Without Saving

  • q!
  • Save File and Quit

  • wq
  • Enter Shell

  • shell
  • Return to vi from Shell

  • exit