原文:https://www.ibm.com/developerworks/library/l-lpic1-103-8/index.html
In this article, learn the basic use of the vi editor, which is almost always available on any Linux or UNIX system. Learn to:
This article helps you prepare for Objective 103.8 in Topic 103 of the Linux Professional Institute‘s Junior Level Administration (LPIC-1) exam 101. The objective has a weight of 3.
To get the most from the articles in this series, you should have a basic knowledge of Linux and a working Linux system on which you can practice the commands covered in this article. Sometimes different versions of a program will format output differently, so your results may not always look exactly like the listings and figures shown here.
The vi editor is almost certainly on every Linux and UNIX system. In fact, if a system has just one editor, it‘s probably vi, so it‘s worth knowing your way around in vi. This article introduces you to some basic vi editing commands, but for a full vi tutorial, check out our tutorial on vi "vi intro -- the cheat sheet method", or consult the man pages or one of the many excellent books that are available.
Most Linux distributions now ship with the vim (for Vi IMproved) editor rather than classic vi. Vim is upward compatible with vi and has a graphical mode available (gvim) as well as the standard vi text mode interface. The vi
command is usually an alias or symbolic link to the vim program. There are several versions of vim: tiny, small, normal, big, and huge. You can find out what version of vim you are running and what features are included by using the command:
1
|
vi --version |
If you recall the section on changing priorities in a previous article "Learn Linux, 101: Process execution priorities," we wanted to change the priority of our running count1.sh shell script. Perhaps you tried this yourself and found that the command ran so fast that you didn‘t have enough time to accomplish the priority change with renice
. So let‘s start by using the vi editor to add a line at the beginning of the file to sleep for 20 seconds so we have some time to change priorities.
If you don‘t still have the count1.sh program around, open a terminal window in your home directory and paste in the commands from Listing 1. This will create count1.sh in a directory called lpi103-8 and place you in that directory.
1
2
|
mkdir -p lpi103-8 && cd lpi103-8 && {echo ‘x="$1"‘>count1.shecho ‘echo "$2" $(date)‘>>count1.shecho ‘while [ $x -gt 0 ]; do x=$(( x-1 ));done‘>>count1.shecho ‘echo "$2" $(date)‘>>count1.sh } |
To edit an existing file, use the vi
command with a filename as a parameter. See the man pages orRelated topics for details on the many options that are available. For now, just use the command without options:vi count1.sh
This should open the count1.sh file. You should see a display similar to Listing 2. If you are using vim, some of the words or characters may be in color. Vim has a syntax highlighting mode (which was not part of the original vi editor), and it may be turned on by default on your system.
1
2
3
4
5
6
7
8
9
|
x="$1" echo "$2" $(date) while [ $x -gt 0 ]; do x=$(( x-1 ));done echo "$2" $(date) ~ ~ ~ ~ "count1.sh" 4L, 84C 1,1 All |
The vi editor dates from the time when not all terminal keyboards had cursor movement keys, so everything you can do in vi can be done with the keys typically found on a standard typewriter plus a couple of keys such as Esc and Insert. However, you can configure vi to use additional keys if they are available; most of the keys on your keyboard do something useful in vi. Because of this legacy and the slow nature of early terminal connections, vi has a well-deserved reputation for using very brief and cryptic commands. Let‘s start by looking at the keystrokes for navigation around your file.
These commands help you move around in a file:
If you type a number before any of these commands, then the command will be executed that many times. This number is called a repetition count or simply count. For example, 5h will move left five characters. You can use repetition counts with many vi commands.
The following commands help you move to specific lines in your file:
Practice these commands until you are comfortable moving around the file. If you get stuck and things aren‘t working as expected, read on and learn how to get out of the file.
One of the most useful things to know about a new editor is how to get out of it before you do anything you shouldn‘t do, such as destroying an important configuration file. You can get out of vi by saving or abandoning your changes, or by restarting from the beginning. If these commands don‘t seem to work for you, you may be in insert mode, which you will learn about in a moment. If in doubt, pressing Esc will leave insert mode and return you to command mode where these commands should work.
Notes:
The vi editor has two modes of operation:
These two modes determine the way the editor behaves. Anything you type in insert mode is considered text to be inserted into the file. If you are trying to type a command and nothing happens, or the character appears under the cursor, then you probably forgot to press Esc to escape from insert mode.
Now that you can open a file in vi, move around it and get out, it‘s time to learn how to edit the text in the file.
Use the following commands when you need to insert, delete, or modify text. Note that some of these commands have an uppercase form that is similar to the lowercase form; see the descriptions below.
You can search for text in your file using regular expressions:
You may precede any of the above search commands with a number indicating a repetition count. So 3/x will find the third occurrence of x from the current point, as will /x followed by 2n. Similarly, 2/^e will find the second line from the current position that starts with e.
Note that search will wrap around to the top once the bottom of file is reached.
Another useful command in vi is the help command, which you invoke by typing :help
. Help will open inside vi; use the :q
command to leave help and go back to your work. If you want help on some particular topic, say wrapping of lines, try adding a word after the :help command, for example: :help wrap
.
We began by wanting to add a line to our count1.sh file. To keep the original and save the modified version as count2.sh, we could use these vi commands once we open the file with vi
. Note that <Esc> means to press the Esc key.
1
|
1GOsleep 20< Esc > :w! count2.sh :q |
These commands do the following:
Simple when you know how.
This is the last article for Exam 101 - Topic 103: GNU and UNIX commands. See our series roadmapfor a description of and link to other articles in this series.
原文:https://www.cnblogs.com/144823836yj/p/9053658.html