Linux vi commands

Source: Internet
Author: User
Tags add numbers

From: http://jinux.hit.edu.cn/courses/computing/linux-viMingLing77_1.html

Linux VI commands

VI command

VI Introduction

VI is a universal full-screen document editor in the Unix world. It can be said that almost any unix machine will provide this software. There are also Linux. Its VI is actually ELVIS (copyright issues), but they are similar. After being familiar with the file processing in DOS, you may feel that VI is not easy to use. On UNIX, many newer and more useful file editors have been developed, however, these additional software may not be installed on every unix machine. Therefore, it is good to learn the basic operations of VI, so that you can be handy on different machines.
----------------------------------------------------------------------------
VI concepts

UNIX provides a series of ex editors, including ex, edit, and VI. Compared with the full-screen editor, it may be hard to imagine how to use the ex and edit columns Editor (have you ever used the edlin file attached before DOS 3.3 ?). VI is originally intended to be "visual", which is an edited program that immediately responds, that is, the operation result can be seen immediately.

Because VI is a full-screen editor, it must control where the entire terminal screen should be displayed. There are many types of terminals with different features. Therefore, it is necessary for VI to know which terminal is used now. This is set by the TERM environment variable. for setting the environment variable, see the description of s hell. (You do not need to set it unless you respond to the unknow terminal type when executing VI .)

You can simply execute VI to enter the VI editing environment. It is better to have a rough understanding of it before the actual operation. VI has two modes: input mode and command mode. The input mode is used to input text materials, while the command mode is used to issue operation instructions for some orchestration files, archives, and exit VI. After executing the VI command, the command mode is started. At this time, any words entered are considered as commands.

You can directly enter VI <File Name> in the system prompt. Vi can automatically help you load the file to be edited or open a new file. After entering VI, A Tilde sign will appear on the left side of the screen. If this symbol is at the beginning of a column, it indicates that this column is currently empty. To exit Vi, you can enter: Q and: WQ in command mode to archive the command before leaving (note the colon ). To switch to the command mode, use the [ESC] key. If you do not know the mode in, you can press [ESC] several times. the system will beep to confirm entering the command mode.
----------------------------------------------------------------------------
VI input mode

How do I enter materials? Several commands can enter the input mode:

Add (append)
A adds data from the end of the cursor, and the data after the cursor moves backward with the new data.
A. add data from the end of the cursor column.

Insert)
I insert data from the front of the cursor, and the data after the cursor moves backward with the new data.
I. Data is inserted before the first non-blank character in the column where the cursor is located.

Start (open)
O adds a column under the column where the cursor is located and enters the input mode.
O adds a new column to the column where the cursor is located and enters the input mode.

The text description may seem complicated, but you can understand these operations as soon as you perform the actual operations. Practice is very important, especially for computer applications. You can try and verify the results at any time. We strongly recommend that you use it instead of simply using K files to get twice the result with half the effort. (Note: This section is nonsense .)
----------------------------------------------------------------------------
VI Basic editing

Use the function keys on the keyboard, such as the direction keys, [Insert], and [Delete]. Now you can use vi to process text. Of course, vi also provides many other functions to make text processing more convenient. If you are interested, please continue.

Click BCC before proceeding. Computers have many brands, different hardware and operating systems. PCs are not just IBM PCs and Their compatible products. In fact, keyboards and terminals often have different specifications. What does this mean?

This problem may occur in the document editing software. Some computers do not have a few function keys on their keyboards! Isn't some functions unavailable? This problem is also the same in Unix systems. Almost all major computer manufacturers have their own Unix systems, and the vi operation methods will also be somewhat different. Although we can use the PC keyboard to describe vi operations, we still need to raise this question.

Delete and modify

What is editing? Here we consider it as the addition, modification, and deletion of text, and even the moving and copying of text blocks. Here we will first introduce how to delete and modify vi. (Note: In the original concept of vi, input and editing are two things. Editing is performed in the command mode. You can use the command to move the cursor to locate the place to be edited before editing the command .)

X deletes the character of the cursor.

Dd deletes the column where the cursor is located.

R: Modify the character of the cursor, and then modify the character of the cursor.

R enters the replacement status. The new data will overwrite the original data until [ESC] is pressed back to the command mode.

S deletes the character of the cursor and enters the input mode.

S: Delete the column where the cursor is located and enter the input mode.

Actually, there is no such trouble on PC! Both input and edit operations can be completed in input mode. For example, if you want to Delete a character, simply press [Delete. However, you can directly use [Insert] to switch between the Insert status and the replace status, so that you cannot use any command mode to edit the command. However, as mentioned above, these commands can be used on almost every terminal, rather than just on a PC.

In command mode, the basic commands for moving a cursor are h, j, k, and l. I think you should be able to guess that you only need to use the direction keys of the PC, and you can use them either in the command mode or in the input mode. Not easy.

Of course, the PC keyboard also has some shortcomings. A good command u can restore deleted data, while U can restore all changes in the column where the cursor is located. This is the same as the [Undo] button function on some computers.
----------------------------------------------------------------------------
Vi advanced applications

I believe that we already have a considerable understanding of vi. It is not difficult to process text. If you are interested in using other functions of vi to further simplify the operation process, you may wish to continue to take a look.

Move cursor

Because a lot of editing work is to locate through the cursor, vi provides many ways to move the cursor. This is described in a few simple tables (these are of course commands in the command mode ):

Certificate ----- certificate ------------- certificate ----- Certificate
│ Instruction │ description │ function key │
Certificate ----- certificate ------------- certificate ----- Certificate
│ 0 │ move to the front of the column where the cursor is located │ [Home] │
Certificate ----- certificate ------------- certificate ----- Certificate
│ $ │ Move to the End of the column where the cursor is located │ [End] │
Certificate ----- certificate ------------- certificate ----- Certificate
│ [CTRL] [d] │ halfway down │
Certificate ----- certificate ------------- certificate ----- Certificate
│ [CTRL] [f] │ to the next page │ [PageDown] │
Certificate ----- certificate ------------- certificate ----- Certificate
│ [CTRL] [u] │ up half page │
Certificate ----- certificate ------------- certificate ----- Certificate
│ [CTRL] [B] │ forward to previous page │ [PageUp] │
Certificate ----- certificate ------------- certificate ----- Certificate
Certificate -- certificate ----------------- Certificate
│ Instruction │ description │
Certificate -- certificate ----------------- Certificate
│ H │ move to the first column of the window │
Certificate -- certificate ----------------- Certificate
│ M │ move to the middle column of the window │
Certificate -- certificate ----------------- Certificate
│ L │ move to the last column of the window │
Certificate -- certificate ----------------- Certificate
│ B │ move to the first letter of the next word │
Certificate -- certificate ----------------- Certificate
│ W │ move to the first letter of the previous word │
Certificate -- certificate ----------------- Certificate
│ E │ move to the last letter of the next word │
Certificate -- certificate ----------------- Certificate
│ ^ │ Move to the first non-blank character in the column where the cursor is located │
Certificate -- certificate ----------------- Certificate
Certificate -- certificate ------------------- Certificate
│ Instruction │ description │
Certificate -- certificate ------------------- Certificate
│ N-│ minus signs move to the first non-blank character in the previous column │
│ You can specify to move the preceding n columns with numbers. │
Certificate -- certificate ------------------- Certificate
│ N + │ plus sign move to the first non-blank character in the next column │
│ Add a number to the front to specify to move to the following n columns │
Certificate -- certificate ------------------- Certificate
│ NG │ directly use the number n plus the capital G to move to column n │
Certificate -- certificate ------------------- Certificate
Certificate ---- certificate --------------- Certificate
│ Instruction │ description │
Certificate ---- certificate --------------- Certificate
│ Fx │ move to the right to x characters │
│ Fx │ move to the left to x characters │
Certificate ---- certificate --------------- Certificate
│ Tx │ move to the top right of x characters │
│ Tx │ move to the left to the front of x characters │
Certificate ---- certificate --------------- Certificate
│; │ Use with f & t, repeat once │
│, │ Use with f & t, and repeat once in the opposite direction │
Certificate ---- certificate --------------- Certificate
│/String │ move to the right to a place with a string │
│? String │ move to the left to where a string exists │
Certificate ---- certificate --------------- Certificate
│ N │ /&? Use, repeat once │
│ N │ /&? Use, repeat once in the opposite direction │
Certificate ---- certificate --------------- Certificate
Certificate ---- certificate ------------------- certificate ---------- Certificate
│ Instruction │ description │ remarks

Certificate ---- certificate ------------------- certificate ---------- Certificate
│ N (│ move the left parenthesis to the beginning of the sentence │ the sentence is

│ You can move n sentences forward by adding numbers. │! .? Three symbols to define │
│ N) │ move the right brackets to the beginning of the next sentence │

│ Add numbers to the front to specify to move n sentences forward │

Certificate ---- certificate ------------------- certificate ---------- Certificate
│ N {│ move the left arc to the beginning of the paragraph │ the section is

│ Add a number to the front to specify to move n paragraphs forward │ blank column definitions between paragraphs

│ N} │ move the right arc to the beginning of the next section │

│ Add a number to the front to specify to move n paragraphs forward │

Certificate ---- certificate ------------------- certificate ---------- Certificate

Do not recite these commands. Otherwise, you are solely responsible for the consequences. They seem to be numerous and messy. In fact, this is an obstacle in the narrative itself. Once again, simply using it a few times can directly launch strange commands without the brain, which is far more vague than rote memorization. (Note: If I do not really miss my brain, it will be irrelevant to me. In addition, this section is nonsense .)

Advanced editing commands these editing commands are very flexible. They are basically composed of commands and ranges. For example, the dw command list is as follows:

D. delete)

Y replication (yank)

P placement (put)

C Change)

The range can be:

E. The cursor is located at the last letter of the word.

W cursor location to the first letter of the next word

B. The cursor is located at the first letter of the previous word.

$ Cursor location to the last letter in the column

0 cursor location to the first letter in the column

) Cursor location to the first letter of the next sentence

(The cursor is located at the first letter of the sentence.

} The cursor is located at the end of the paragraph.

{The cursor is located in the first letter of the paragraph.

To be honest, it is a little artistic to combine these commands to edit files. In any case, they provide more text editing capabilities. It is worth noting that the deletion and replication will put the content within the specified range in the temporary storage area, and then you can use the command P to paste it to other places, this is how VI processes copying and moving segments.

Some VI versions, such as Elvis for Linux, can greatly simplify this instruction. If you take a look at these editing commands, you will find that the problem is actually a bit complicated by the fixed range method. In fact, there are only four commands.

Command V is very easy to use. As long as you press the V key, the position of the cursor will be reversed, and then you can move the Set range, and then directly edit the command.

For the entire column operation, VI also provides more convenient editing instructions. As mentioned above, the command DD for deleting the entire text column is one of them. CC can modify the entire text column, while YY is to copy the entire text column; command to delete all text from the cursor to the end of the column.

Archive instruction

Archive commands start with:, which is a little different from editing commands. For example, the instruction to end editing is Q. Now I will briefly describe the end of this story:

Q end editing (quit)

If you do not want to archive an archive and want to discard the edited archive, Use Q! Force exit.

W archive (write)

The file name to be archived can be added later. Archive commands can be combined. For example, wq means archiving and leaving.

Zz functions are the same as: wq.

It is also worth mentioning that part of the vi archiving function. You can use: n, mw filename to store the text of row n to row m in the specified filename. The times are changing, the world is changing, and vi is changing, but that's the case in general.

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.