Logo Background RSS

» vi

  • Understanding basic vi (visual editor)
    By on September 16th, 2008 | 2 Comments2 Comments Comments

    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.

    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:

    vi ew 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.