Essential tool for linux/unix -- vi usage

Source: Internet
Author: User

Basic vi operations:
1. Make sure you are in the main directory. Create a file named vitext in your home directory.
>>$ Cd
>>$ Pwd
> Vi vitext
2. When a vi file is opened, it is automatically in command mode. Press the I key (insert) or the key (append) to switch to the text mode. Using a or I only determines whether typing starts before or after the cursor. There is no indication that you are in input mode.
Press ESC to switch from input mode to command mode. Press 1 second. If you press ESC twice, you will hear "beep" (some terminals will not ). This sound indicates that you are in command mode. Now press I again to return the input mode. Continue to the next step.
-I
-ESC
-ESC (beep)
-I
3. Enter the following text correctly and display it one line after another. Then, type the alphabet with one letter per line. Next, a-d is skipped until z is displayed. It is easy to add an alphabet to fill in the screen information that will be used later.
This is a training session about the usage of the vi editor. we need some more lines to learn the most common commands of the editor. we are now in the entry mode and we will switch right after this to the command mode.
A
B
C
D
...
Z
4. Return to the command mode. Write and exit the file. Note that when you press: (colon), it appears under the last row of your input area. Once the cache is empty and the file is closed, you will see a message showing the number of rows and letters in the file.
> ESC (enable you to enter command mode)
>>: Wq (<shift-zz> or: x is another write or exit method)
 
Vi editor Introduction
1. There are three working modes: Command mode, text mode, and extended mode.
1) command mode: open a file in vi and directly enter command mode (this is the default mode ). In this mode, you can move the cursor by pressing the upper, lower, and lower buttons. You can delete characters, delete the entire line, or copy or paste file data.
2) text mode: In normal mode, you can delete, copy, and paste files, but cannot edit the file content. After you press any letter, such as "I, I, o, O, a, A, r, R", you will enter the editing mode.
3) Extended Mode: In general mode, enter ":,/,?" Any of the three buttons can move the cursor to the bottom line. In this mode, you can find data, and read, save, and replace a large number
Changing Characters, leaving vi, and displaying row numbers are also completed in this mode.


 
Button description
Use vi to open vitext. Note that the bottom line of the file displays the name and number of letters of the file.
>>$ Vi vitext
Use the arrow keys and h, j, and k keys to move the cursor down a row, move the cursor up a row, move the cursor up two letters to the right, and move the cursor back two letters.
> J (next line)
> K (top row)
> L (one letter to the right)
> H (one letter to the left)
> Select an appropriate direction key to redo
You may not want to move a letter or line with the cursor from start to end. Exercise using the cursor to move the key to flexibly change pages and rows. Use the cursor movement key from the previous steps to place your cursor in the first line of the file. In command mode, perform the following steps:
Move one page forward
>>< Ctrl-f> or press the Page Down (PgDn) Key. (There is no Page Down key on the ASCLL terminal and this key on your terminal may not be used)
Move one page backward
>>< Ctrl-B> or press the Page Up (PgUp) Key. (There is no Page Up key on the ASCLL terminal and this key on your terminal may not be used)
Move the cursor to the last line of the file
>>< Shift-G>
Move the cursor to the first line of the file
> Gg
Or
> 1 <shift-G>
Or: 1 Enter
Move the cursor to the first line of the file
> 4 <shift-G> or: 4 Enter
Move the cursor to the end of the file
>>$
Move the cursor to the beginning of the file
> 0

8. Move your cursor to the top of the file. Search for the word entry. Your cursor should be on e. Turn to input mode and add the word "text ". Do not forget the spaces behind the word.
> 1 <shift-G> or: 1
>/Entry
> I
> Text
9. move the cursor to the space behind the word mode in the same line. Enter a comma. Remember, you are still in the inbound and outbound mode.
> Esc
> Place the cursor at the space behind the mode
> I,
10. Enter the command mode. Place the cursor at any position in the line with the "me more lines" header. Insert a blank line to form two paragraphs.
> ESC
> Place the cursor on the line starting with "some more lines"
> O
11. Open a blank line as in the previous step and automatically enable you to enter the input mode. Therefore, return to the command mode. Save your changes so far, but do not exit the editor.
> ESC
>>: W
12. When it is still in command mode, clear the characters c, e, g, but leave blank lines between them. In other words, do not delete the entire line, only delete letters. Then return and delete the blank row. This will enable you to use two types of deletion functions.
> Move the cursor to c. Press x
> Move the cursor to e. Press x
> Move the cursor to g. Press x
> Move the cursor over each blank line. Press dd
13. now use the character z to replace h
> Move the cursor over h
> Press r
> Z
14. You just decided that you do not want to save the changes to the characters. Exit the edit session and do not save the changes after the last save.
>>: Q!
15. Edit vitext again. First, copy each line from the first segment (including blank lines) to the end of the file one by one. After that, copy from the second segment to the end of the file.
>>$ Vi vitest
> Place the cursor on the first line. Press yy
>>< Shift-G>; press p
> 2 <shift-G>; press yy
>>< Shift-G>; press p
> 3 <shift-G>; press yy
>>< Shift-G>; press p
> 4 <shift-G>; press 2yy
>>< Shift-G>; press p
16. You just decided that the line you added to the end of the file does not look very accurate. Use a command to delete all of them.
> Move the cursor to the first copied row at the bottom of the deleted object.
> Number of rows to be deleted
> 5dd (if you move blank rows, the number may be different)
17. Now, before you do anything else on this file, you decide that you need to insert the current time to the first line of the file. Do not exit the vi editor here.
>> :! Date> datelife
> Do not Press enter when you see the information Press return to continue.
>>: 0r datelife
> Press enter twice to continue
③ Use settings to customize the editing session.
18. You can use the set command to temporarily set options in an editing session. The top layer of the returned file. Make sure that you are in command mode and set the following command:
A. Set automatic line feed at 15 intervals on the right
B. When the INPUT MODE is in the INPUT MODE, the INPUT MODE information is displayed.
C. Enable row tagging
> 1 <shift-G>
> ESC
>>: Set wrapmargin = 15
>>: Set showmode
>>: Set number
19. test each option set in previous commands.
> The row number should be
> Use I or a to enter the input mode. You will see the input mode information at the right bottom of the display.
> Type two lines of text to test the automatic line feed.
> Press ESC to enter the command mode. The input mode information will disappear from your display.
20. Write the file and exit the editor.
④ Edit the command line
21. Now that you are familiar with the vi Mode and commands, practice the command line editing. Use the set-O vi command to create your session and use the command line to edit it.
>>$ Set-o vi
22. Now you can review previously executed commands, edit them, and submit them again. Let's create a command history to work. List (simple rather than long) the contents of/usr. Displays the content of the file/etc/filesystems. Echo hello.
>>$ Ls/usr
>>$ Cat/etc/filesystems
>>$ Echo hello
23. Suppose you want to edit a command you just executed. Press ESC to enter the vi command mode. Try to press k several times to move up in the command list. Try to press the j key several times to move down the command list. This review of commands is essentially looking for the cache of the commands you have previously executed. The command is actually stored in the. sh_history file in your main directory.
> ESC
> K (move up in the cached command list)
> J (move down in the cached command list)
24. Retrieve the ls command. Use the l key to move your cursor to/in/usr. (Note: The direction keys tend to clear the entire line. You must use the l key on the right and h key on the left) use the I key to insert text and change this command to a long list. Run it.
> K (ls/usr command)
> L (Arrival /)
> I (enter the input mode. You can also use a for appending, if the cursor is on the space before)
>-L
> Enter
25. Review cat commands. List the content of the file/etc/passwd this time
> ESC
> K (reaching the previous cat command)
> L (move the cursor to f in filesystems)
> D (clear the remaining lines, or clear words using the dw command)
> A (additional text)
> Passwd
> Enter
26. Review cat commands. Go to the end of the row (remember $ ). Add wc at the end of the command for pipeline output and calculate the number of rows.
> ESC
> K (reaching the last cat command)
>>$
>
>>| Wc-l
> Enter

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.