From cainiao to expert vi editor teaching manual (1)

Source: Internet
Author: User

We used to explain some basic operation commands in the linux vi editor. However, we still encounter a lot of confusion when using the vi Editor, especially the basic usage methods in Linux or UNIX, in fact, the final analysis of these problems lies in our lack of strong understanding of relevant knowledge. In this article, you will learn how to use the vi editor.

Use vi navigation document

Use the basic vi model

Insert, edit, delete, copy, and search text

This article will help you prepare 103 assessment goals under the 103.8 subject of the Linux Professional Institute's Junior Level Administration (LPIC-1) Exam. The weight of the assessment target is 3.

Prerequisites

To get the most benefit from this article, you should have basic Linux knowledge and a Linux system that can run properly, so that you can practice the commands discussed in this article. Different versions of the program output results may have different formats, so your results may be different from the results shown in the image and the list in this article.

Use vi navigation document

Contact IanIan, one of our most popular and productive authors. View Ian's profile and contact other authors and readers on My developerWorks. Almost all Linux and UNIX systems have the vi editor. In fact, if the system only has one editor, it must be vi, so it is worthwhile to have a deep understanding of vi. This article will introduce you to some basic vi editing commands, but for the complete vi tutorial, please refer to our "vi Introduction-quick reference table" for the vi tutorial ", you can also consult the manual page or reference related books.

Start vi

Most Linux distributions now have the vim viIMproved editor instead of the classic vi. Vim is compatible with vi and has available graphic mode gvim) and Standard vi text mode interface. The vi command is usually an alias or symbolic link of the vim program. There are several vim versions: micro, small, normal, large, and ultra-large. You can use the following command to find the version to run and the features of the version:

Vi -- version

Recall the section in the previous article "Learning Linux, 101: Process Execution priority" to change the priority of running count1.sh shell scripts. You may find that the command runs too fast, so that you do not have enough time to use renice to change the priority. We can use the vi editor to add a row at the beginning of the file to sleep for 20 seconds, so that we can change the priority time.

If you do not have the count1.sh program, open the terminal window in the main directory and paste the command from List 1. This will create a count1.sh in the directory named lpi103-8 and bring you into that directory.

List 1. CPU-intensive script-count1.sh

  1. mkdir -p lpi103-8 && cd lpi103-8 && {  
  2. echo 'x="$1"'>count1.sh  
  3. echo 'echo "$2" $(date)'>>count1.sh  
  4. echo 'while [ $x -gt 0 ]; do x=$(( x-1 ));done'>>count1.sh  
  5. echo 'echo "$2" $(date)'>>count1.sh  

To edit an existing file, run the vi command and use the file name as a parameter. Refer to the manual in the reference documents for details on more topics. Now, you only need to use the command without parameters:

Vi count1.sh

This command will open the count1.sh file. You should see something similar to listing 2. If you are using vim, some words or letters may have colors. Vim has a syntax highlighting model which is not part of the original vi Editor). By default, it should be open in your system.

List 2. Use vi to edit count1.sh

 
 
  1. x="$1" 
  2. echo "$2" $(date)  
  3. while [ $x -gt 0 ]; do x=$(( x-1 ));done  
  4. echo "$2" $(date)  
  5. ~  
  6. ~  
  7. ~  
  8. ~  
  9. "count1.sh" 4L, 84C 1,1 All 

When the vi editor is developed, not all terminal keyboards have a cursor move key. Therefore, you can only use keys found on the standard keyboard, as well as keys such as Esc and Insert in vi. However, you can configure vi to use other keys if they are available. Most keys on the keyboard can be useful in vi. Because of this legacy situation and the slow connection nature of early terminals, vi naturally has a reputation for simplicity and mystery. Let's first check the key used for navigation in the file.

Mobile

The following command can help you move files:

  1. H. Move one character from the current row to the left.
  2. J. move down to the next row
  3. K. Move up to the previous row.
  4. L move one character from the current row to the right
  5. W moves to the next word in the current row
  6. E. Move to the end of the next word in the current row.
  7. B: Move it to the beginning of the previous word in the current row.
  8. Ctrl-f scroll one page forward
  9. Ctrl-B scroll back one page

If you type a number before these commands, the command executes the number of times this number represents. This number is called the number of repetitions or count. For example, 5 h will move 5 characters to the left. Many vi commands can use repeated counts.


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.