VI Command fool to explain

Source: Internet
Author: User
DirectoryText Open File Enter edit note current mode cursor Move Insert content Delete undo operation copy Paste save/Exit copy paste search string Replace string in two files Body

The general use of VI below, this article is suitable for beginners, but not suitable as a manual query, if you need to command-oriented manuals, please use $ man vi|more

open file into edit

Edit a file:

$ VI Example.txt

Edit multiple files at the same time:

$ VI file1 file2 ... filen

If you use VI to edit a file does not normally save the exit, will be left behind an implied file:. FILENAME.SWP, use the following command to restore the edits made from the last save to the Non-normal exit:

$ vi-r filename

Note the current mode

After entering file editing, VI has four modes, one is insert mode (press keyboard I enter), in this mode keyboard input will be added as characters to the file.

The other is the editing mode, this state can directly input commands to move the cursor, delete characters, and other operations, do not need to enter the confirmation, each command button will directly take effect. Pressing the ESC key in insert mode returns to edit mode.

The third is the command mode, in edit mode by the colon ":" to enter the command mode, when the keyboard input characters will appear under the screen, enter the command to execute. In this mode, you can enter commands such as save, string search, exit edit, and so on. In command mode, press ENTER or ESC to return to edit mode.

The fourth is the search mode, in edit mode, enter the slash "/" or question mark "?" That is, enter the search mode.

The above four modes are the names I have used to describe the convenience, which will be referenced below.

Cursor Movement

Move the cursor up, down, left, and right, corresponding to the key: K, J, H, L (edit mode).

Navigate to the last line and enter capital G (edit mode).

Back to the first line, enter lowercase g (edit mode).

Jump to row N, enter number n in command mode, carriage return.

Displays the current number of rows and columns, and enter CTRL+G in edit mode.

Insert Content

Insert the contents before the current character, enter I in edit mode, enter the Insert mode, enter the contents, and then press ESC to return to edit mode after the input is complete.

Insert the content after the current character, enter a in edit mode, and the other actions are the same as above.

At the top of the line, in edit mode, enter capital I. Other operations are the same as above.

At the end of the line append content, in edit mode, enter capital A. Other operations are the same as above.

Remove

Delete the character of the current cursor, enter x in edit mode, and enter a capital X if you want to delete the character before the current cursor.

Delete a row and enter D twice in edit mode. Delete a word, and then enter the DW in edit mode.

Delete the current cursor to the end of the line, and then enter d$ in edit mode.

Undo Action

Undo just one change, edit mode, enter U, undo this whole row of changes, enter capital U.

If you want to undo all changes to the file, in command mode, enter E! , carriage return (Enter a colon ":" Enter command mode,: e!).

Copy Paste

Copy one line of content, and enter two consecutive times in edit mode y.

Pastes a row from the Clipboard after the current line, and enters p in edit mode.

Copy n line content, edit mode Enter the number n (n is to copy the number of rows from the current line), and then enter Y, copy complete, input p paste multiple lines.

Save/Exit

If you save and close the file, enter x in command mode, and return.

If you save, and continue with the file editing, enter W and return in command mode.

If you discard all changes, exit directly, and enter q! in command mode Enter.

The input Wq in command mode is equivalent to X.

copy and paste in two files

You need to paste a row of file2 into the file1.

Open two files at the same time, separated by spaces in the middle:

$ VI file1.txt file2.txt

When opened, the current editor is file1, enter N in command mode, and switch to File2.

Find the related row, enter YY in edit mode, enter the command mode, input capital N, switch back to the previous file (in order to open), find the relevant location, edit mode input p, paste finished.

Search String

To search for the string ABC, in search mode (enter a slash "/" or a question mark in edit mode), enter the ABC, return entry.

If found, the cursor is positioned from the current position to the first occurrence of the string ABC, and input n jumps to where the next ABC appears, entering uppercase N, and jumping to where the previous ABC appeared.

The difference between a slash "/" and a question mark "?" is a backward forward search and a reverse search.

Replace string

To replace all of the string ABC in the current line with EFG, enter s/abc/efg/g in command mode, carriage return.

The above command will only replace ABC and will not replace ABC or ABC. If you want to ignore case, add parameter I, S/abc/efg/gi, enter.

If you want to make a full file range substitution, enter%s/abc/efg/g in command mode and return.

If a replacement string contains special characters such as Slash "/", use a backslash "\" to escape.

In command mode, enter%s/\/image/\/img/gi, carriage return

The above command replaces all string/image in the file with/img and does not distinguish between the case of image.

Article from: http://www.aiview.com/2005/08/vi_simple_guide.html

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.