Vi editor usage (1)

Source: Internet
Author: User
There are many types of text editors in Linux, including gedit and kwrite in graphic mode, vi in text mode, vim (enhanced version of vi), and emacs. Vi and emacs are the two most commonly used editors in Linux. This section describes the vi editor, including the basic usage of the vi editor, including starting and exiting vi and working

There are many types of text editors in Linux, including gedit and kwrite in graphic mode, vi in text mode, vim (enhanced version of vi), and emacs. Vi and emacs are the two most commonly used editors in Linux. This section describes the vi editor, including:

Basic use of the vi editor, including vi startup and exit, switching the working mode, moving the cursor, scrolling the screen, inserting and deleting the text, copying and pasting, searching and replacing.

Program editing in the vi editor, including redirection of the optical mark in the program design, completion of keywords, and code indentation.

We hope that through this study, we will be able to master the usage and related skills of the vi editor, and be able to independently edit and modify the program source code.

1 Basic use of the vi editor

The vi editor is a standard editor in Linux. Although many commands are complex and most of the input functions are completed by the keyboard, if we are familiar with them, we will find that the functions and efficiency of the vi editor are unmatched by other graphic interface editors, let's unveil its secrets.

1.1 vi startup

Enter the command vi on the terminal, and then enter the file name you want to create or edit to go to the vi editor.

$ Vi example. c

The command result is 1-1:

-1 create a file in the vi editor

If the file entered after the vi command does not exist, the system automatically creates a text file named after this string. For example, the cursor stays at the top left. because the new file does not contain any content, the beginning of each line is a wavy line. At the bottom of the window is the status bar, showing information about the current edited file.

After opening the file, the cursor stays at the top left of the screen. The status bar displays the file name, number of lines, and number of characters of the currently edited file, as shown in 1-2.

Vi commands can also contain parameters when opening a file. These parameters are used to modify the vi opening method, including the following:

-R open the file in read-only mode.

If you only need to read the file content and do not want to modify it, you can use this parameter to prevent file misoperations, as shown below:

$ Vi? R example. c

Run command 1-3.


-2 open a file in the vi editor

 

-3 open a file in read-only mode

-X encrypts the file.

This parameter can be encrypted when the file is saved. you need to enter the key each time you open the file. otherwise, garbled characters may occur.

If no file name is provided and no parameter is provided when vi is opened, that is:

$ Vi

Command result 1-4:

-4 open the vi editor directly.

In this case, all lines in the vi editor are empty. the help of the vi editor is provided in the center of the window. when you enter text or execute commands in vi, The Help information will automatically disappear. In addition, if you open the vi editor in this way, you must specify the file name when saving the file.

1.2 vi working mode

Vi has three working modes: Normal mode, edit mode, and command mode. these three modes can be switched to each other, as shown in Figure 1-5.

-5 working mode of the vi editor

1.Normal mode

When a Shell enters the vi editor, it first enters the normal mode. In normal mode, any character input from the keyboard is interpreted as a command. In normal mode, there is no prompt, and the command is executed immediately when you enter the command. you do not need to press enter, and the entered characters are not displayed on the screen.

In normal mode, you can enter a command to move the cursor, copy characters, words, and rows, paste, and delete the cursor.

2.Edit mode

The editing mode is mainly used for text input. In this mode, any character entered by the user is saved as the file content 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 (replacement command) will enter the editing mode, at which point the last line of the vi window will display "Insert ". The screen does not change when you enter the I command. However, by executing the I command, the editor switches from normal mode to edit mode, as shown in Figure 1-6.

-6 switch the editor mode from normal mode to edit mode.

Enter x =, as shown in Figure 1-7.

To return to normal mode, press Esc.

-7. enter characters in editing mode.

3.Command mode

In command mode, you can perform some additional processing on the file. Although commands in common mode can perform many functions, you must enter the command mode to perform operations such as string search, replacement, and line number display.

In normal mode, enter the colon to enter the command mode. at this time, the status of the vi window will display a colon, waiting for the user to enter the command. After the user input is complete, press enter to execute, and then the vi editor returns to normal mode.

1.3 save and exit an object

After editing, exit the vi editor by running the command in command mode and return to Shell.

1.Save and exit

Saving and exiting refers to writing the content in the buffer into a file. the following commands can be used: wq and x, as shown in Figure 1-8.

-8 save and exit the vi editor

2.Force exit

Forced Exit refers to unconditional exit. if you do not write the content in the buffer into a file, the command is q !. The exclamation point "!" Indicates that the modified content is forcibly exited regardless of whether the file is modified.

3.Exit directly

The difference between direct exit and forced exit is that if the file content is modified, a prompt is displayed, as shown in 1-9; otherwise, the file exits directly. The exit command is q.

-9 exit directly from the vi editor

Note that when editing files in the vi editor, user operations are performed based on copies in the buffer zone. If it is not saved to the disk when you exit, the content in the buffer will be lost. Therefore, when exiting the vi editor, you should consider whether to save the edited content and then choose to execute the appropriate exit command. Save the command as w. If no file name is provided when you open vi, you also need to give the file name to each other.

Unfinished, to be continued...

Related Article

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.