Linux VI Command Daquan

Source: Internet
Author: User

http://www.cnblogs.com/88999660/articles/1581524.html. UNIX Editor Overview

Editors are one of the most important tools for using computers, and in various operating systems, editors are essential parts. UNIX and its similar IX operating system family provide a series of ex-editors, including EX, Edit,ed and VI, in order to facilitate the use of various users in various environments. Where ex,edit,ed are line editors and are now rarely used, The reason that UNIX provides them is due to the need to satisfy a variety of users, especially some end users.

Fortunately, UNIX provides a full-screen VI editor, which makes our work a lot easier. Many DOS users complain that the VI editor is not as good as the editor under DOS, because VI takes into account the needs of various users and does not use some common editing keys (their definitions are different on different terminals, even without these keys on some terminals). But the use of state switching method, but this is only a habit of the problem, once you skillfully use the VI you will feel it is actually very useful. Although VI adopts the method of state switching, but the computer hardware and operating system are various, some computer keyboard does not have a specific number of function keys! So there are some features that can't be used? This problem is also the same on UNIX systems, almost every major computer manufacturers have their own Unix system, and VI operation method will be a bit different. Here we use the keyboard of the PC to explain the operation of VI, but in the specific environment also refer to the corresponding data, this is noteworthy.

Ii. Introduction of VI

(i), enter VI

Typing vi under the system prompt character (such as $, #) >,vi can automatically load the file you want to edit or open a new file (if the file does not exist or the filename is missing). After entering VI, there will be a wave symbol on the left of the screen, which indicates that the column is empty at the first sign.

(ii), two modes

As mentioned above, VI has two modes: instruction mode and input mode. Keystrokes entered in instruction mode are handled as instructions: input a,vi is considered to be the insertion of characters at the current position. In input mode, VI treats the input key as an inserted character. instruction mode switch to input mode only need to type the appropriate input command (such as A,a), and to switch from input mode to instruction mode, you need to type in input mode ESC, if you do not know what mode is in, you can press more than a few times [ESC], if the system beeps to indicate that it is in the instruction mode.

instruction mode into the input mode:

New (Append)

A: From the cursor position to start the new data, the data after the cursor with the new data to move forward.

A: Start adding data from the end of the column where the cursor is located.

Inserting (insert)

I: Insert data from the cursor position, and the data after the cursor moves with the new data.

I: Insert data from the first non-whitespace character in the column where the cursor is located.

Start (Open)

O: Add a new column under the Cursor column and enter the input mode.

O: Add a column above the cursor column and enter the input mode.

(iii), Exit VI

In instruction mode type: Q,:Q!,:WQ or: X (note: number), you will exit VI. of which: Wq and: X is to save the exit, and: Q is to exit directly, if the file has a new change, vi will prompt you to save the file and: Q command will also be invalid, then you can use: w command saved the file and then use: Q Exit, or with: Wq or: X command exit, if you do not want to save the changed file, you need command, this command will exit VI without saving the file.

(iv), basic editing

With the function keys on the general keyboard, such as the arrow keys, [Insert], [Delete] and so on, now you should already be able to use VI to edit the file. Of course, VI also provides many other functions to facilitate the processing of text.

What is editing? It is generally believed that the text is added, modified and deleted, even the deletion of text chunks, copying and so on. First here to introduce how VI to do delete and modify. (Note: In the original concept of VI, input and editing are two things.) The edit is operated in instruction mode, and the instruction is used to move the cursor to locate the place to be edited, and then the instruction is edited. )

To delete a command that modifies a file:

x: Deletes the character of the cursor.

DD: Deletes the column where the cursor is located.

R: Modifies the character of the cursor, and R is followed by the characters to be corrected.

R: Enter the replace state and the new text will overwrite the original text until you press [ESC] back to instruction mode.

S: Delete the character of the cursor and enter the input mode.

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

In fact, there is no such trouble at all on the PC! Both the input and the edit can be done in input mode. For example, to delete a character, simply press [delete]. The insertion state and the substitution state can be switched directly with [insert], and the editing instructions for the instruction mode are not used. As mentioned earlier, however, these instructions are available for almost every terminal, not just on a PC.
The basic instructions for moving the cursor in instruction mode are H, J, K, L. I think you should also be able to guess now as long as the direct use of the PC's direction key can be, and both in the instruction mode or input mode can be. How easy it is not.
Of course, the PC keyboard also has shortcomings. There is a good command you can restore the deleted text, and the U command can restore all the changes in the column of the cursor. This is the same as the [Undo] key function on some computers.
Third, annex: VI Detailed instruction table
(a), basic editing instructions:
New (Append)

A: From the cursor position to start the new data, the data after the cursor with the new data to move forward.

A: Start adding data from the end of the column where the cursor is located.

Inserting (insert)

I: Insert data from the cursor position, and the data after the cursor moves with the new data.

I: Insert data from the first non-whitespace character in the column where the cursor is located.

Start (Open)

O: Add a new column under the Cursor column and enter the input mode.

O: Add a column above the cursor column and enter the input mode.

x: Deletes the character of the cursor.

DD: Deletes the column where the cursor is located.

R: Modifies the character of the cursor, and R is followed by the characters to be corrected.

R: Enter the replace state and the new text will overwrite the original text until you press [ESC] back to instruction mode.  

S: Delete the character of the cursor and enter the input mode.

S: Delete the column where the cursor is located and enter the input mode. In the Vi/vim editor, to undo an operation, press ' U ' in the command state, and if you want to restore the undo content, you should use Ctrl + R

(b), Cursor Movement command:
Because many editing jobs are positioned by the cursor, VI provides many ways to move the cursor, which we list
A few short tables to illustrate (these are, of course, instructions in instruction mode):

Instruction description Function key
0 move to the front of the column where the cursor is located [Home]
$ move to the last face of the column where the cursor is located [End]
[CTRL] [d] Half page DOWN [PageDown]
[CTRL] [F] Page Down
[CTRL] [u] up half page
[CTRL] [b] up one page [PageUp]

Instruction description
H moves to the first column of the window
M moves to the middle column of the window
L move to the last column of the window
W Transfer Fine horseshoe crab value Boaster Huan nbsp; ^ Move to the first non-whitespace character in the column where the cursor is locatedb move to the first letter of the next word (go up)
E move to the last letter of the next word (go down)
g Move to the beginning of the documentG move to end of document

Instruction description
The n minus sign moves to the first non-whitespace character in the previous column preceded by a number can be specified to move to the N column above
n+ plus moves to the first non-whitespace character in the next column preceded by a number can be specified to move to the following n columns
NG is moved directly to nth column with the number n plus the uppercase G
Instruction description
Fx
Move right to X character
Fx moves left to X-characters
TX moves to the right before X-characters
Tx moves to the left before X-characters
; Use with F&t, repeat once
, with the use of f&t, the opposition to repeat once
/string to the right, to the place with the string.
The string moves left to where there is a string
n with/&? Use, repeat once
N with/&? Use, the opposite direction repeats once

Instruction description Remarks
N
The left parenthesis moves to the front of the sentence that is preceded by a number can be specified to move the n sentence forward!. ? Three kinds of symbols to define
N) The right parenthesis moves to the front of the next sentence, plus the number can be specified to move the N-sentence!. ? Three kinds of symbols to define
N{left bracket moves to the first paragraph of the paragraph is defined by a blank column between paragraphs
n} preceded by a number you can specify to move the N-paragraph right parenthesis forward to the front of the next paragraph, plus the number can be specified to move the N-paragraph paragraph is defined by a blank column between paragraphs

(iii), more editing instructions
These editing instructions are very resilient and basically consist of directives and scopes. For example, the DW Nbsp;d is composed of a range w, which represents the deletion of a word D (elete) W (ORD).
The instruction list is as follows:
D Remove (delete)
Y copy (Yank)
P Placement (put)
C Modification (change)
The range can be a few of the following:
E-Light Shing Mei Reef meter value Nao Yue Mei Huan Horseshoe?br> w cursor position to the first letter of the next word
b The position of the cursor to the first letter of the previous word
$ cursor position to the last letter of the column
0 The first letter of the column to which the cursor is located
Cursor position to the first letter of the next sentence
(The position of the cursor to the first letter of the sentence
The cursor is positioned to the last letter of the paragraph
{The position of the cursor to the first letter of the paragraph
To be honest, combining these instructions to edit the file has a little bit of artistic flair. In any case, they provide more ability to edit text. It is important to note that both delete and copy will place the contents of the specified range in the staging area, and then you can use the instruction p to paste it elsewhere, which is the way VI is used to deal with section copy and move.
Some vi versions, such as the Elvis used by Linux, can greatly simplify this lump of instructions. If you look at these editing instructions a little bit, you will find that the problem is a little miscellaneous in the way of scoping, actually there are only four instructions. Command V is very useful, just press the V key, the position of the cursor will be anti-white, and then you can move the cursor to set the range, then directly under the command to edit. For the whole column operation, VI also provides more convenient editing instructions. As mentioned earlier, the instructions to delete the whole column of text DD is one of them, CC can modify the whole column of text, and yy is to copy the whole column of text, instruction D can delete the cursor to the end of the column until all the text.
(iv), document operation instructions
File operation instructions to: Start, which is a little different from the editing instructions.
: Q End Edit (quit)
: q! Do not archive and discard the edited file.
: W Saves the file (write) and then adds the filename to be archived.
: Wq is archived and left.
The ZZ function is the same as: Wq.
: X is the same as: Wq Delete SWP files under (v) and VI

When editing a file using the VI editor, a SWP file is often generated due to an abnormal exit, which will prompt you every time you enter. The online collection of solutions is now displayed here:

SWP file causes are divided into: 1. When you use multiple programs to edit the same file. 2. Non-regular exit. Usually the last few changes are not recoverable. Vim synchronizes with the disk for 4 seconds without typing, or after 200 characters in a row. This can be done through ´updatetime´ and ´updatecount´ two
Options to control. So if the system has been changed but the vim has not been synchronized with the outage, then this part of the content will not be restored. In the first case, in order to avoid producing two different versions of the same file (the exact words in vim), it is advisable to choose ReadOnly as well. In the second case,you can use vim-r filename to recover and then delete the SWP file (rm. filename.swp)。 Further learning: Swap-filej about the location and naming of swap files
:p Reservej manually Refresh swap files
: Swapnamej View the name of the original file and its swap file
´updatecount´ consecutive keystrokes How many times after doing a synchronization
´updatetime´ How long does it take to synchronize
´swapsync´ simultaneous disk synchronization when synchronizing swap files
´directory´ lists the directory where the swap files are stored
´maxmem´ memory limit for content that has not been written to the interchange file
´maxmemtot´ Ibid, but for all files

Linux VI Command Daquan

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.