Use vi to interact with Unix systems

Source: Internet
Author: User
Article Title: Use vi to communicate with Unix systems. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

During work hours, Unix engineers need to communicate and talk with Unix systems through various means. For example, in system firewall management, engineers must tell their ideas, such as which packets are allowed to pass and which packets are filtered out, to the Unix operating system. Without doubt, the vi Program is such a tool. It can build a bridge of communication between engineers and Unix operating systems.

Vi is the most popular full-screen editing program in Unix operating systems. Its functions are rich. It is unrealistic to introduce all its functions through the length of an article. For this reason, I will talk about the practical skills and notes of the vi tool from the perspective of how to improve the utilization and development speed of the vi Program.

1. The vi editing program completes all the work in the work buffer.

The Vi editing program has a feature that all her work is done in the buffer zone. Before the engineer starts editing the conversation, the vi editing program reads the edited files from the hard disk into the working buffer. During the engineer's editing process, all tasks are completed in the buffer until the work buffer content is written back to the hard disk. When an engineer edits a new file, the vi editing program does not immediately create a file on the hard disk, but stores the content in the buffer. Then, the administrator needs to run the w command to save the file to the hard disk.

This mechanism has both advantages and disadvantages. Because the system reads data from the buffer zone much faster than reading data from the hard disk. Therefore, using vi to edit programs will be highly efficient. However, the buffer content is not saved on the hard disk in time, so if the Unix server suddenly loses power, all contents in the buffer will be lost. This results in unnecessary losses. Therefore, the author suggests that when using vi to edit a program to write a large program, pay special attention to the need to use w command to save the content in the buffer zone in a timely manner. However, the vi editing program is not a text editing software after all. It is not wise to use this tool to create complex files.

  2. Select the correct mode.

The Vi editing program has three operation modes: Command mode, input mode, and baseline command mode.

In command mode, the content entered by the engineer is translated into commands and passed to the vi editing program. For example, the "a" command is one of the representative commands. In command mode, all characters entered by the Engineer are not displayed on the screen. The engineer does not need to press the Enter key to execute the command. Commands in the Vi editing program are generally single characters, which are often the first character of the English operation name. For example, the English letter "a" mentioned above indicates the characters to be inserted.

In input mode, the engineer can edit the file. Generally, engineers are in command mode when they enter the vi editing program. At this time, the engineer cannot edit the file. In input mode, the vi editing program looks like a typewriter software. The content entered here will be accepted and displayed as text to the Administrator. After entering or editing the text, Press Esc to return to the command mode.

After editing the text, the engineer needs to use the colon command to move the vi cursor to the bottom line of the screen. In this case, the vi editing program enters the command line mode. Engineers often need to use colons with other commands. For example, ": w" indicates saving files in the buffer zone. In the base-line command mode, it is mainly used to execute some special functions. For example, you can replace text and execute shell commands without exiting the vi editing environment.

Because different modes perform different jobs, engineers need to understand the different features of these three modes, and then select the appropriate operation mode based on the actual situation. Specifically, I have the following suggestions. If you only want to view the file or are not sure how to modify it, you 'd better view the file in command mode. Because it is read-only in command mode, misoperation on the original file can be avoided. At the same time, after writing the file, you need to promptly convert to the final line mode, and use commands such as w to save the modified file to the hard disk in a timely manner. It can be seen that the appropriate mode is conducive to improving the efficiency of engineers, while avoiding the negative impact of misoperations on the original files.

  3. Master some quick operations.

In the vi editing program, you cannot use the mouse to perform operations. The movement of the cursor, the insertion and deletion of text, and so on are completed by the keyboard. Therefore, an experienced Unix engineer often needs to master some shortcuts to improve the efficiency of using the vi editing program. It is often possible to determine whether the other party has enough Unix experience by observing the method in which the other party uses vi to edit the program.

The author believes that what engineers need to learn first is how to quickly locate. The mouse cannot be used for cursor positioning. Therefore, when an engineer needs to insert, add, modify, or delete a file using the vi editing program, he must use the keyboard to position the cursor properly, to insert or add a specified location. If the Engineer wants to quickly determine the current position of the cursor (for example, press ctrl + g), then the cursor can be moved to the specified row through the G parameter. For example, IG can move the cursor to the first line of the file, and enter G to move the cursor to the last line of the file.

In addition to learning how to quickly locate the cursor, engineers also need to learn how to quickly delete the cursor. In the vi editing program, you can delete characters, words, rows, sentences, and paragraphs. For example, in vi command mode, if an engineer needs to delete a word, he can move the cursor down to the first character of the word, and then press the dw two character keys. If you want to delete multiple words at the same time, you need to add a multiplier before the command dw. At the same time, the engineer can also delete it in the unit of action. The engineer moves the cursor to any position in the row and press the dd key. If you press 2dd, you can delete two rows consecutively. And so on.

Although the vi editing program cannot be operated with the mouse, as long as you have mastered these quick operations, you can still use this editing tool to talk to the Unix operating system very quickly. When interviewing new employees, I often ask the interviewer to use the vi tool to edit a file. This method can be used to determine whether the work experience of the other party on the Unix operating system is consistent with their resume. Therefore, I suggest that young people who want to make a difference on Unix systems spend more time on the vi editing tool. This is very necessary.

 

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.