Linux Text Editor-vi
I. VI simple description
In Linux, we always recommend that you use the text status to handle Linux system settings, because it not only makes it easier for you to understand the running status of Linux, it is also easier to understand the basic spirit of the entire setup, and more "ensure" that your modifications can be smoothly run (here we will re-emphasize that different Linux distribution have different additional software, for example, Red Hat's userconf, linuxconf, ntsysv and setup, and Caldera has COAs. Therefore, if you only use this type of software to control your Linux system, when taking over different
Linux distribution, haha! That can be annoying! ) In Linux, you can use a text editor to edit your Linux parameter configuration file. Therefore, the system administrator should be familiar with at least one text processor!
In the introduction to Linux, we know that the parameter files in Linux and UNIX systems are almost all plain text files with ASCII codes! Therefore, you can use a simple text editor to immediately modify the Linux parameter file! However, unlike windows, if you are familiar with Microsoft Word or Corel Word Perfect, the editor in xwindows (such as xemacs) is still usable, in the Linux text status, the file editor does not have windowsProgramSo convenient. (Note: What is an ASCII plain text file? To put it bluntly, the file you open can show the content to the file you see, rather than the garbled file, it is a plain text file! When you use
When Windows Word stores a file, it uses type in DOS, um! I don't know what the content is? Because there will be a lot of garbled characters, it is not a plain text file, but if you use word to save the file, select "plain text type", well! Then you can use type to view the content of the file! In short, the plain text file is quite easy to modify! )
In any case, to manage the Linux system, manual setting of plain text is still required! Therefore, please do not bother yourself. Please try again later! The following describes text editing operations under Linux using the most common VI Text software. (Note: VI is a UNIX preset text editor. Of course, it is also a Linux preset text editor! )
Basically, VI is divided into three states: "General State", "Edit State", and "command column command state!
• "Normal" status: After a file is processed by VI, the file enters the normal state. In this status, you can use the top, bottom, and left buttons to move the cursor. You can use the delete character or delete the entire line to process the file content, you can also use "copy and paste" to process your files.
• "Edit" status: operations such as deletion, copying, and pasting can be processed in normal status, but they cannot be edited! You must press the letter "I, I, O, O, a, a, R, R" to enter the editing status. Note! In Linux, when you press the above letter, "insert or replace" appears at the lower left of the screen to input any words to your file! To return to the normal status, you must press the "ESC" button to exit the editing status.
• "Command column command" status: Enter ":" or "to move the cursor to the bottom line. In this status, you can search for files. Reading and saving files, replacing a large number of characters, leaving VI, and displaying row numbers are achieved in this status!
Ii. VI examples
How can we use VI to create a file named test.txt? Let's illustrate it with a simple example!
2. 1. Use VI to enter the General State;
[Root @ test root] # vi test.txt
Enter "VI file name" to enter Vi! As shown in, the current status of the file is displayed in the lower left corner! If a new file is created, [new file] is displayed. If it is an existing file, the current file name, number of lines, and number of characters are displayed, for example, "/etc/man. config "145l, 4614c 』
2. Press I to enter the editing status and start editing the text;
In the normal status, you only need to press the I, O, A and other characters to enter the editing status! In the editing status, you can find the "-insert-" Screen in the lower left corner, which means you can enter any characters! At this time, except for the [ESC] button on the keyboard, other buttons can be regarded as general input buttons, so you can edit any of them! (Note: In VI, the results obtained by the [Tab] button are different from those obtained by blank characters, with special emphasis on this !)
2. 3. Press the [ESC] button to return to the normal state;
Okay. Now that I have edited the style above, how can I exit? Yes! That's right! Just press the [ESC] button for him! Immediately you will find that the insert-in the lower left corner of the screen is missing!
. Press: WQ to save and Leave Vi!
OK, we want to save the file. The command to save the file and exit is simple. Enter ": WQ" to save the file and exit! (Note: Press: The cursor will move to the bottom line !) Enter "ls-L" after the prompt character to see the test.txt file we just created! The entire figure is a bit like below:
In this way, your file test.txt has been created! Easy! Note that if your File Permission is incorrect, such as-r --, you may not be able to write data. Can you use "Force write? Yes! Use ": WQ !』 Add an exclamation point! But pay special attention to it! That can be set up only when your permissions can be changed! For the concept of permissions, see http://www.chinaitlab.com/www/news/article_show.asp? Id = 8313
Iii. Command instructions
As mentioned above, the so-called command column or command status is when the "-- insert --" or "-- replace --" is not displayed in the bottom line. The commands in the Command column usually include the following: (Note that when you press ":", the cursor will automatically move to the bottom line of the screen !)
In general, in addition to the commonly used instructions described above, the VI commands do not need to be backed up. You can make a simple script table on your screen wall. If you have any questions, you can query it immediately.
4. Some Complex exercises
Test whether you are familiar with the VI command? Follow the instructions below (note that this file is the content of the file attached to Mandrake 9.0. If it is another distribution, the results may be different! ). Then, let's take a look at what is displayed in front of the screen?
4.1 exercise requirements
1. Create a directory named vitest under the/root directory;
2. Go to the vitest directory;
3. Copy/etc/man. config to the local directory;
4. Use VI to open the man. config file in the local directory;
5. Set the row number in VI;
6. Move the row 62nd and move it 40 characters to the right. What is the directory in the double quotation marks that you see?
7. Move to the first line and search down for the string "tetex". What is the number of lines does he have?
8. Next, I want to change man between 50 and 100 rows to man, and choose whether to modify them one by one. How can I issue instructions?
9. After the modification, I suddenly regretted it. what methods should I do to restore it all?
10. I want to copy the content from 51 to 60 rows and paste it to the last row;
11. Delete 20 rows between 11 and 30;
12. Save the file as a file name of man. Test. config;
13. Go to the 29th line and delete 15 characters;
14. How many lines and characters does the current file have?
15. Save and leave!
4.2 The procedure can be as follows:
1. mkdir/root/vitest
2. CD/root/vitest
3. CP/etc/man. config.
4. VI man. config
5.: Set nu
6. Press 62 GB first, then right-click 40 +, and you will see "/DIR/bin/foo 』
7. Press 1g first, and then press/tetex to search. The page will show that he is in line 51st!
8. 50,100 S/man/GC
9. (1) A simple method can always return to the original state by pressing U.
(2) do not store and exit: Q! And then read the file again.
10. 51g, then press G to the last line after 10yy, and paste 10 lines to his p!
11. After 11 GB, you can delete 20 rows after 20 DD;
12.: W man. Test. config
13. After 29 GB, 15 characters can be deleted after 15X;
14. 135 lines, 3903 characters in total
15.: WQ!
If all your results can be found, there should be no major problems with using Vi! The remaining problems will be ..... Typing exercises ....
V. FAQs
I want to move 15 characters to the right of a file in line 5. What commands should I give in normal state?
A: After 11 GB to 11th rows, right-click 15 +, or click 15l!
In VI, which combination keys can be used to replace the Pagedown button?
A: [CTRL] + F. You can flip the page backward.
How do I go to the top or end of the file Vi?
A: The top is 1 GB and the end is G!
How to move a row to the header and end of the row?
A: Move to the row header, press 0, and move to the end of the row and press $!
Button to enter the editing status?
A: I, I, O, O, a, a, R
In VI, what functions does R have?
A: Replace the character where the cursor is located!
How can I save another new file on the current page?
Answer: W filename
In Linux, the most commonly used text editor is vi. How can I enter the editing status?
A: O enters I, I, A, and a in the General State. (-insert -)
O in the General State: O, O is to enter new characters in a new line;
O in the General State, enter "R" and "R" as the replacement character! (-Replace-appears in the lower left corner -)
How does one switch from the editing status to the normal status?
A: [ESC]
If the upper and lower right keys cannot be used, how can I move the cursor in normal state?
Answer: H, J, K, L
If [Pagedown] [Pageup] is unavailable in normal state, how can I flip one page forward or backward?
A: [CTRL] + [F]
[CTRL] + [B]
How to get to the last line and the first line of this file; the first character and the last character of this line?
Answer: G, 1g, 0, $
How do I delete one row and N rows, and delete one character?
A: DD, NDD, X, or X (DG and d1g indicate deletion to the top and end of the page respectively)
How do I copy and paste one or N rows?
A: yy, nyy, P, or P
How to search for the string?
A: O? String (Forward search)
O/string (search later)
How can I replace word1 with word2, and what if I need a user validation mechanism?
A: O: 1, $ S/word1/word2/g or
O: 1, $ S/word1/word2/GC (subject to user confirmation)
How do I read a file filename to the current file?
A: R filename
How to save a new file as newfilename?
A: W newfilename
How do I save the file, exit, save the file and exit, and forcibly save the file and then exit?
A: W;: Q: WQ;: WQ!
How do I set and cancel a row number?
A: Set nu
: Set Nonu