VI Editor Usage (1)

Source: Internet
Author: User

There are many kinds of text editors under the Linux system, including Gedit, kwrite in graphic mode, vi in text mode, VIM (enhanced version of VI), Emacs, etc. Of these, vi and Emacs are the most commonly used two editors in Linux systems. This section mainly introduces VI editor, the main content includes:

    • VI Editor basic use, including the start and exit of VI, the switch of working mode, the movement of the cursor, the scrolling of the screen, the insertion and deletion of text, copying and pasting, finding and replacing.
    • VI Editor of program editing, including the design of the cursor jump, keyword completion, and code indentation and so on.

Hope that through this section of learning, can master the VI editor of the use of methods and related skills, can independently complete the program source code editing and modification.

Basic use of the 1 VI editor

The VI Editor is the standard editor for Linux systems. Although the command is complex, and the majority of the input of the function depends on the keyboard to complete, but if we are familiar with the function of the VI editor, efficiency, etc. are not comparable to other graphical interface editor, let us uncover its mystery.

1.1 VI Start-up

Enter Command VI at the terminal, followed by the name of the file you want to create or edit, to enter the VI editor.

$ VI example.c

The result of the command is shown in 1-1:

Figure 1-1 VI editor new file

If the file entered after the VI command does not exist, a text file named after that string is automatically created. For example, the cursor stays at the top left, and since there is nothing in the new file, each line starts with a wavy line. The bottom of the window is the status bar, which displays information about the currently edited file.

When the file is open, the cursor rests at the top left of the screen. The status bar displays information such as the file name, number of lines, and number of characters for the currently edited files, as shown in 1-2.

The VI command can also take parameters when opening a file, which is used to correct the opening of VI, mainly including the following:

    • -R opens the file in read-only mode.

If you only need to read the contents of the file and do not want to modify it, you can use this parameter, in case of the wrong operation of the file, as follows:

$ vi–r example.c

Run command 1-3 as shown.

Figure 1-2 VI editor open file figure 1-3 Open file as read-only

    • -X encrypts the file.

This parameter can be encrypted when you save the file, and you will need to enter the key each time you open it, otherwise garbled.

If you open VI, the file name is not given, and no parameters are given, i.e.:

$ VI

The command result 1-4 shows:

Figure 1-4 Opening the VI editor directly

At this point, all the lines in the VI editor are empty, and the center of the window gives help for the VI editor, which automatically disappears when the user enters text in VI or executes a command. In addition, if you open the VI editor in this way, you need to specify a file name when saving your files.

Working mode of 1.2 VI

VI has 3 modes of operation: Normal mode, edit mode and command mode, these 3 modes can switch to each other, 1-5 is shown.

Figure 1-5 VI Editor's working mode

1. Normal Mode

When the shell enters the VI editor, it first enters normal mode. In normal mode, entering any character from the keyboard is interpreted as a command. There is no prompt in normal mode, which executes immediately when the command is entered, does not require a carriage return, and the characters entered are not displayed on the screen.

In normal mode, you can enter commands to move the cursor, character, word, line copy, paste, and delete operations.

2. edit Mode

Edit mode is used primarily for text input. In this mode, any characters entered by the user are saved as the contents of the file and displayed on the screen. In normal mode, enter a (additional command), C (Modify command), I (insert command), O (New line), R (Replace command), and S (replace command) will enter the edit mode, the last line of the VI window displays "insert". There is no change in the input I command, but by executing the i command, the editor is switched from normal mode to edit mode, as shown in 1-6.

Figure 1-6 Editor switch from normal mode to edit mode

Then enter x=, shown on screen 1-7.

To return to normal mode, simply press ESC.

Figure 1-7 Entering characters in edit mode

3. Command mode

Command mode, the user can perform some additional processing on the file. Although commands in normal mode can do many things, it is necessary to perform some operations such as string lookups, substitutions, display line numbers, or command mode.

Enter a colon in normal mode to enter command mode, where the status line of the VI window displays a colon, waiting for the user to enter a command. After the user input is completed, press ENTER to execute, and then the VI Editor returns to normal mode.

1.3 Saving and exiting a file

When you have finished editing and need to exit the VI editor, you can return to the shell by using the command Exit VI in command mode.

1. Save Exit

Save exit refers to writing the contents of the buffer to a file, and the commands you can use are shown in Wq and x,1-8.

Figure 1-8 VI editor Save exit

2. forced Exit

Forced exit refers to the unconditional exit, without writing the contents of the buffer to the file, using the command q!. where exclamation mark "!" Indicates that the modified content is forced to quit, regardless of whether the file has been modified.

3. Direct Exit

The difference between a direct exit and a forced exit is if the contents of the file are modified to give a hint, 1-9, or exit directly. The command that is used to exit directly is Q.

Figure 1-9 VI Editor's direct exit

It should be noted that when the VI editor edits a file, the user's actions are based on the copy in the buffer. If you do not save to disk when you exit, the contents of the buffer will be lost. Therefore, when exiting the VI editor, you should consider whether you need to save the edited content and then choose to execute the appropriate exit command. Save Command W, if you open VI does not give the file name, you also need to give the mutual file name.

VI Editor Usage (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.