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
- mkdir -p lpi103-8 && cd lpi103-8 && {
- echo 'x="$1"'>count1.sh
- echo 'echo "$2" $(date)'>>count1.sh
- echo 'while [ $x -gt 0 ]; do x=$(( x-1 ));done'>>count1.sh
- 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
- x="$1"
- echo "$2" $(date)
- while [ $x -gt 0 ]; do x=$(( x-1 ));done
- echo "$2" $(date)
- ~
- ~
- ~
- ~
- "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:
- H. Move one character from the current row to the left.
- J. move down to the next row
- K. Move up to the previous row.
- L move one character from the current row to the right
- W moves to the next word in the current row
- E. Move to the end of the next word in the current row.
- B: Move it to the beginning of the previous word in the current row.
- Ctrl-f scroll one page forward
- 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.