Understanding basic vi (visual editor)


  • Applies to: Grid
    • Difficulty: Medium-Hard
    • Time Needed: 20
    • Tools Required: SSH enabled, SSH client
  • Applies to: All DV
    • Difficulty: Medium
    • Time Needed: 10
    • Tools Required: SSH, root or sudo access

Overview

vi (visual editor) is included with all (mt) Media Temple services. vi is a full screen editor and has two modes of operation.

  • Command Mode - every character entered is a command. This is the default mode when you start vi.
  • Insert Mode - this mode allows you to edit. To enter insert mode simply hit i once vi has started.

NOTE:

vi is case-sensitive. Be sure to not mix uppercase and lowercase letters when running commands, as the result will not be the same.

The best way to learn about vi is with the vimtutor, you can start your lesson by typing the following command via SSH:

vimtutor

Below is just a short list of a few commands that are very useful for beginners.

Requirements

Before you start, you should have:

READ ME FIRST

The publishing of this information does not imply support of this article. This article is provided solely as a courtesy to our customers. Please take a moment to review the Statement of Support.

READ ME FIRST

The publishing of this information does not imply support of this article. This article is provided solely as a courtesy to our customers. Please take a moment to review the Statement of Support.

Instructions

Opening a file in vi

This first line is just to simply open a file with vi:

vi filename

The following command is used to recover a file that was being edited when the system crashed:

vi -r filename

The next command will open a file as read-only:

view filename

To find a line in vi

You can use the following to go to the last line in the file:

G

To go the the first line of the file, you can use the following:


1G

To move around in the editor

  • type h to type move the cursor to the left
  • type l to move it to the right
  • type k to move up
  • type j to move down

To search within vi

To search for text in vi, you can use the "/" key followed by your search term. This example uses mttext:


/ mttext

To exit vi

To quit and not save changes, you can use:

:q!

If you want to quit and save changes, you can use the following command:

:x!

TIP:

The above examples only hint at the power of vi. It is very popular amongst developers/administrators for good reason. We strongly suggest making backups of any files before experimenting with vi, in case you need to revert your changes.