Skip to content

Latest commit

 

History

History
181 lines (89 loc) · 6.09 KB

File metadata and controls

181 lines (89 loc) · 6.09 KB

Vi Text Editor

There are many ways to edit files in Unix. Editing files using the screen-oriented text editor vi is one of the best ways. This editor enables you to edit lines in context with other lines in the file.

An improved version of the vi editor which is called the VIM has also been made available now. Here, VIM stands for Vi IM proved.

vi is generally considered the de facto standard in Unix editors because −

  • It's usually available on all the flavors of Unix system.

  • Its implementations are very similar across the board.

  • It requires very few resources.

  • It is more user-friendly than other editors such as the ed or the ex.

You can use the vi editor to edit an existing file or to create a new file from scratch. You can also use this editor to just read a text file.

Operation modes

While working with the vi editor, we usually come across the following two modes −

  • Command mode: This mode enables you to perform administrative tasks such as saving the files, executing the commands, moving the cursor, cutting (yanking) and pasting the lines or words, as well as finding and replacing. In this mode, whatever you type is interpreted as a command.

  • Insert mode:
    This mode enables you to insert text into the file. Everything that's typed in this mode is interpreted as input and placed in the file.

vi always starts in the command mode. To enter text, you must be in the insert mode for which simply type i. To come out of the insert mode, press the Esc key, which will take you back to the command mode.

Hint − If you are not sure which mode you are in, press the Esc key twice; this will take you to the command mode. You open a file using the vi editor. Start by typing some characters and then come to the command mode to understand the difference.

Starting The Vi editor

Following is an example to create a new file testfile if it already does not exist in the current working directory −

Testfile image

  • $vi testfile

The above command will generate the following output-

vi editor

You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line. If a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some other non-viewable character present.

You now have one open file to start working on. Before proceeding further, let us understand a few important concepts.

Getting out of vi

The command to quit out of vi is :q. Once in the command mode, type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, the command to quit out of vi without saving is :q!. This lets you exit vi without saving any of the changes.

The command to save the contents of the editor is :w. You can combine the above command with the quit command, or use :wq and return.

The easiest way to save your changes and exit vi is with the ZZ command. When you are in the command mode, type ZZ. The ZZ command works the same way as the :wq command.

If you want to specify/state any particular name for the file, you can do so by specifying it after the :w. For example, if you wanted to save the file you were working on as another filename called filename2, you would type :w filename2 and return.

Commands

To move around within a file without affecting your text, you must be in the command mode (press Esc twice). The following table lists out a few commands you can use to move around one character at a time −

There are many other ways to move within a file in vi. Remember that you must be in the command mode (press Esc twice). The following table lists out a few commands to move around the file −

  • 0 or |
    

    Positions the cursor at the beginning of a line

  • $
    

    Positions the cursor at the end of a line

  • w
    

    Positions the cursor to the next word

  • b
    

    Positions the cursor to the previous word

  • (
    

    Positions the cursor to the beginning of the current sentence

  • )
    

    Positions the cursor to the beginning of the next sentence

  • E
    

    Moves to the end of the blank delimited word

  • {
    

    Moves a paragraph back

  • }
    

    Moves a paragraph forward

  • ]]
    

    Moves a section forward

  • n|
    

    Moves to the column n in the current line

  • 1G
    

    Moves to the first line of the file

  • G
    

    Moves to the last line of the file

  • nG
    

    Moves to the nth line of the file

  • :n
    

    Moves to the nth line of the file

  • fc
    

    Moves forward to c

  • Fc
    

    Moves back to c

  • H
    

    Moves to the top of the screen

  • nH
    

    Moves to the nth line from the top of the screen

  • M
    

    Moves to the middle of the screen

  • L
    

    Move to the bottom of the screen

  • nL
    

    Moves to the nth line from the bottom of the screen

  • :x
    

    Colon followed by a number would position the cursor on the line number represented by x

Running Commands

The vi has the capability to run commands from within the editor. To run a command, you only need to go to the command mode and type :! command.

For example, if you want to check whether a file exists before you try to save your file with that filename, you can type :! ls and you will see the output of ls on the screen.

You can press any key (or the command's escape sequence) to return to your vi session.

Important points to notice

The following points will add to your success with vi −

  • You must be in command mode to use the commands. (Press Esc twice at any time to ensure that you are in command mode.)

  • You must be careful with the commands. These are case-sensitive.

  • You must be in insert mode to enter text.

For more information on how to use vi editor please visit the following link! and watch this interactive video for better understanding and live examples.