Tutorial on VI

Source: Internet
Author: User

VI usage
The VI editor is a standard editor for all UNIX and Linux systems. It is not inferior to any of the latest text editors. Here we just briefly introduce its usage and a few instructions. The VI editor is identical for any version of UNIX and Linux systems, so you can learn more about it in any other section about VI. VI is also the most basic text editor in Linux. After learning it, you will be able to enjoy the free access in the Linux World.
1. Basic concepts of vi
Basically, VI can be divided into three states:
Command mode)
Insert mode)
Last line mode)
1) Command Line Mode)
Controls the movement of the screen cursor, the deletion of characters, words, or rows, the movement of copying a segment and entering the insert mode, or to the last line mode.
2) insert mode)
Only in insert mode can text input be made. Press the [ESC] key to return to the command line mode.
3) Baseline Mode)
Save the file or exit VI. You can also set the editing environment, such as searching for strings and listing row numbers ?? .
However, we generally simplify VI into two modes in use, that is, the last line mode is also included in the command line mode ).
2. Basic operations of vi
A) Go to VI
After the system prompts you to enter the VI and file name, you will be taken to the VI full screen editing screen:
$ VI myfile
Note that after entering Vi, you are in [command mode]. You must switch to [insert mode] to enter the text. People who use VI for the first time will want to move the cursor first with the upper and lower right keys. As a result, the computer keeps beeping and getting angry with themselves. So after entering VI, do not tamper with the mouse, switch to [insert mode!
B) switch to insert mode to edit the file
In [command mode], click the letter I to enter [insert mode]. Then you can start to enter the text.
C) Insert switchover
If you are currently in [insert mode], you can only enter text all the time. If you find that you have entered an error! To move the word back with the light mark key, you need to press the [ESC] key to [command mode] and then delete the text.
D) Exit VI and save the file
In [command mode], click [:] colon to enter [last line mode]. For example:
: W filename (enter [W filename] To save the article with the specified filename)
: WQ (enter [WQ] To save the disk and exit VI)
: Q! (Enter Q !, Force exit VI without saving the disk)
3. Command mode function key
1). insert mode
Press switch to enter the insert mode, press "I" to enter the insert mode, and then enter the file starting from the current position of the cursor;
After you press [a] to enter the insert mode, the text is input starting from the next position where the cursor is currently located;
After you press [O] to enter the insert mode, a new row is inserted and text is entered from the beginning of the line.
2) switch from insert mode to Command Line Mode
Press the [ESC] key.
3) move the cursor
VI can be moved up, down, left, and right directly with the cursor on the keyboard. However, regular VI uses lowercase letters [H], [J], [K], and [l]. the cursor is controlled to move one cell to the left, down, up, and right respectively.
Press [CTRL] + [B] to move the screen to the "back" page.
Press [CTRL] + [f]: The screen moves one page to the front.
Press [CTRL] + [u]: Move the screen to the back half of the page.
Press [CTRL] + [d] to move the screen to the front half of the page.
Press [Gg]: to move to the first line of the article. (It may only be valid in VIM)
Press [g] to move to the end of the article.
Press [$] to move to the end of the row where the cursor is located ".
Press [^]: Move to the "first line" of the row where the cursor is located"
Press [w]: The cursor jumps to the beginning of the next word
Press [e]: move the cursor to the end of the next word
Press: The cursor returns to the beginning of the previous word
Press [# l]: move the cursor to the # position of the row, such as 5l and 56l.
VI also provides three commands for moving the cursor across the screen without scrolling the file itself. They are h, M, and l commands.
(1) h command
This command moves the cursor to the first line of the screen (that is, the upper left corner), that is, the first line of the current screen, rather than the first line of the entire file. You can use this command to quickly move the cursor to the top of the screen. If n is added before the H command, move the cursor to the beginning of the N line. It is worth mentioning that the command DH will delete all content from the current row of the cursor to the first line of the displayed screen.
(2) m command
This command moves the cursor to the beginning of the line in the middle of the display file. That is, if the current screen is full, it will be moved to the middle line of the screen; if it is not full, it will be moved to the middle line of the lines of the text. This command can be used to quickly move the cursor from any position on the screen to the beginning of the line in the middle of the screen display file. For example, when the above screen is displayed (no matter where the cursor is on the screen), in command mode, after the command m is input, the optical mark will be moved to the letter A in the Add line. It is also worth mentioning that using the command DM will delete all content from the current row of the cursor to the middle row of the displayed file on the screen.
(3) l command
When the content displayed in the file exceeds one screen, this command moves the cursor to the beginning of the line at the bottom of the screen. When the content displayed in the file is insufficient, this command moves the cursor to the beginning of the last line of the file. This command can be used to quickly and accurately move the cursor to the bottom of the screen or the last line of the file. If n is added before the l command, move the cursor to the beginning of the N line starting from the bottom of the screen. It is also worth mentioning that the command DL will delete all content from the current row of the cursor to the bottom line of the screen.
4). delete text
[X]: each time you press a cursor, the "Next" character of the cursor is deleted.
[# X]: for example, [6x] indicates to delete 6 characters after the cursor is located.
[X]: uppercase X. Each time a cursor is pressed, the "front" character of the cursor is deleted.
[# X]: for example, [20x] indicates to delete 20 characters before the cursor is located.
[DD]: Delete the row where the cursor is located.
[# DD]: delete a row from the row where the cursor is located # Row
5). Copy
[YW]: copy the character at the end of the cursor to the buffer zone.
[# YW]: Copy # words to the buffer zone
[YY]: copy the row where the cursor is located to the buffer zone.
[# YY]: for example, [6yy] indicates copying 6 lines of text from the row where the cursor is located "down.
[P]: place the characters in the buffer to the cursor position. Note: All copy commands related to "Y" must work with "P" to complete the copy and paste function.
6). Replace
[R]: Replace the character at the cursor position.
[R]: Replace the character wherever the cursor goes until you press the [ESC] key.
7). Restore the last operation
[U]: If you mistakenly execute a command, you can immediately press and return to the previous operation. You can perform multiple replies Based on Multiple "U" requests.
8). Change
[Cw]: change the word at the cursor to the ending point.
[C # W]: for example, [c3w] indicates to change three words.
9). Cancel the change
[U]: undo the last change. You can press [u] until the last change is canceled.
[CTRL] + [R]: resumes undo changes. You can always press this key combination to achieve the latest changes.
10). Jump to the specified row
[CTRL] + [g] lists the row numbers of the cursor.
[# G]: for example, [15g] indicates moving the cursor to the first row of the article.
4. Introduction to commands in last line mode
Before using [last line mode], remember to press [ESC] to confirm that you are already in [command mode], and then press [:] enter [last line mode] With a colon.
A) List row numbers
[Set nu]: After you enter [set nu], the row number is listed before each row in the file.
B) jump to a row in the file.
[#]: [#] Indicates a number. Enter a number after the colon and press enter to jump to the row. For example, enter the number 15 and press Enter, this will jump to the second line of the article.
C) Search for characters
[/Keyword]: First press the [/] key and then enter the character you want to search. If the first search keyword is not what you want, you can press [N] until the keyword you want is found.
[? Keyword]: First press [?] Enter the character you want to search for. If the keyword you want for the first time is not what you want, you can press [N] until the keyword you want is found.
D) save the file
[W]: Enter the letter [w] in the colon to save the file.
E) Leave Vi
[Q]: Press [Q] to exit. If you cannot leave Vi, you can follow [Q] with a [!] Force exit VI.
[WQ]: It is generally recommended to use it with [w] When you exit. In this way, you can save the file when you exit. You can use it with [!] Later. Force save and exit.
[Zz]: Save and exit VI.
F) Highlight the syntax
[Syntax on]: The vi editor does not enable the syntax brightening function by default. After opening the VI editor, you can use the [syntax on] command under [last line mode] to enable the syntax brightening function, in this case, the editor highlights the keywords in the file for programming. You can use the [syntax Off] command to disable this function.

Tutorial on VI

Related Article

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.