Summary of VI usage
The most powerful Editor-vi
VI is a screen editor provided by all UNIX systems. It provides a Windows device that allows you to edit files. Of course, people who know a little about UNIX systems may feel that VI is super difficult to use, but VI is the most basic editor, so I hope readers can learn it well, in the future, the Unix world will surely be smooth and easy to use, because other types of text processors are not UNIX-standard. Maybe someone else's Linux machine does not install Joe or Pico. If you don't know Vi, you may not have to worry about it.
Basic concepts of vi
Basically, VI can be divided into command mode, insert mode, and last line mode ), the functions of each mode are as follows:
1. Comand mode: controls the movement of the screen cursor, the deletion of characters or the cursor, the replication of a specific segment, the entry into the insert mode, or the last line mode.
2. insert mode: only in insert mode can text data be input. Press ESC to return to Comand mode.
3. Last line mode: Save the file or leave the editor. You can also set the editing environment, such as searching strings and listing row numbers.
However, you can simplify VI into two modes, that is, the last line mode is also counted into command mode, and VI is divided into command and insert mode.
Basic operations of vi
? Go to VI
After the system prompts you to enter the VI and file name, you can enter the VI full screen editing screen:
$ VI testfile
Note that after entering Vi, you are in "command mode". You must switch to "insert mode" to enter the text. Users who use VI for the first time will want to move the cursor first with the upper and lower left keys. As a result, the computer keeps calling and getting angry with themselves. So after entering VI, do not change it into insert.
? Switch to insert mode to edit the file
Press 'I', 'A', or 'O' in command mode to enter the insert mode. At this time, you can start to enter the text.
I: insert. Insert the entered text from the current cursor.
A: Add. Input text starts from the next word where the cursor is currently located.
O: Insert a new row and enter text from the beginning of the line.
? Insert switchover → command mode, Press ESC
If you are currently in insert mode, you can only keep typing. If you find a typo, and want to use the mouse button to move back, delete the word, you need to press ESC to switch back to command mode, and then delete the text.
? Exit VI and save files
In command mode, you can press the colon ":" to enter the last line mode. For example:
: W filename (enter "w filename ",ArticleStore the specified file name filename)
: WQ (enter "WQ". Because the file name testfile has been specified at the time of entry, it will write testfile and leave VI)
: Q! (Enter "Q !", Force exit and discard edited files) Command mode function key list
When introducing the command mode command, add the "commonly used" function key to the command to indicate commonly used VI commands. You must learn and remember them.
(1) switch I, A, and O to insert mode. [Super common]
(2) move the cursor
VI can be moved up, down, left, right, and left directly using the optical key on the keyboard, but the regular VI is a lowercase English letter.
H, J, K, and l, respectively, control the left, down, up, and right shifting of the cursor.
Press Ctrl + B: Move the screen back one page. [Common]
Press Ctrl + F: Move the screen one page forward. [Common]
Press Ctrl + u: Move the screen back half a page.
Press Ctrl + D: Move the screen half page forward.
Press 0 (number zero): to move the beginning of an article. [Common]
Press g to move to the end of the article. [Common]
Press W: the cursor to jump to the beginning of the next word. [Common]
Press E: move the cursor to the end of the next word.
Press B: The cursor returns to the beginning of the previous word.
By $: Move to the end of the row where the cursor is located. [Common]
Press ^: to move to the first non-blank character of the row.
Press 0: Move to the beginning of the row. [Common]
Press #: Move to the # position of the row, for example, 51 and 121. [Common]
(3) Delete text
X: each time the cursor is deleted, the next character is deleted. [Extraordinary use]
# X: for example, the 6X table deletes the next six characters of the cursor position. [Common]
X: X of the big character. Each time the cursor is deleted, the first character is deleted.
# X: for example, in the 20x table, the first 20 characters of the cursor position are deleted.
DD: Delete the row where the cursor is located. [Extraordinary use]
# DD: for example, the 6dd table deletes six lines of text from the row where the cursor is located. [Common]
(4) Copy
YW: copy the character from the cursor position to the end of the word to the buffer zone.
(Unlike # X and # X)
P: paste the characters in the buffer to the cursor position (the command 'yw' and 'P must be used together ).
YY: copy the row where the cursor is located. [Extraordinary use]
P: copy a single row to where you want to paste it. (The command 'yy' and 'p' must be used together)
# YY: for example, 6yy indicates copying six lines of text from the row where the cursor is located. [Common]
P: Copy multiple rows to where you want to paste them. (The command '# yy' and' P' must be used together)
"Ayy: Put the replication row into buffer A. VI provides the buffer function to store common data in the buffer.
"AP: paste the data stored in buffer.
"B3yy: store three rows of data into buffer B.
"B3p: paste data with buffer B
(5) replace
R: Replace the character at the cursor position: [common]
R: Replace the character until you Press ESC.
(6) restore the previous command (UNDO)
U: If you misoperate a command, you can immediately press u to return to the previous operation. [Extraordinary use]
..: You can execute the last command again.
(7) Change
CW: change the word at the position of the cursor to the end of the word $.
C # W: for example, c3w indicates to change three characters.
(8) Jump to specified row
CTRL + G: displays the row number of the cursor.
# G: for example, 15g indicates moving the cursor to the first row of the article. [Common]
Introduction to commands in last line mode
Before using last line mode, remember to Press ESC to confirm that you are in command mode, and then press the colon ":", "/" or "?" One of the three keys enters the last line mode.
1. List row numbers
Set nu: After "set nu" is input, the row number is listed before each row of the article.
2. Jump to a line in the article
#: The well number represents a number. Enter a number before the ":" symbol in last line mode, and press enter to jump to this line, for example: 15 [enter] will jump to the 15th line of the article. [Common]
3. Search for strings
/Keyword: First press/and then enter the word you want to search for. If the keyword you are looking for the first time is not as close as possible, you can press n until you find the keyword you want.
? Keyword: First press ?, Enter the word you want to search. If the keyword you want for the first time is not what you want, you can press n to search for the keyword you want.
4. Replace string
1, $ S/string/replae/G: Input "1, $ S/string/replace/g" in last line mode to replace the full-text string with the replace string, among them, 1, $ s refers to the search range as the meaning of the article from the beginning to the end, G indicates that all replace does not need to be confirmed.
% S/string/replace/C: Replace the full-text string with the replace string, which is different from the preceding command: % s and 1, $ S is the same function, and C indicates that you must confirm whether to replace it before replacement.
1, 20 s/string/replace/G: Replace the string between 1 and 20 lines with the relpace string.
5. save files
W: Press W before the last line mode prompt ":" To save the file. [Extraordinary use]
#,# W filename: If you want to extract a part of the article and save it as another file, use this command # to indicate the row number, for example, 30, 50 w nice. The 30th ~ 50 rows are saved as nice files.
6. Leave
Q: Press Q to exit. Sometimes, if you cannot exit Vi, you can use "! : Force exit VI, such as "Q !"
QW: it is generally recommended that you use it with w when you exit. files can be stored when you exit. [Common]
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