VI usage (powerful full-screen editor)

Source: Internet
Author: User

VI-powerful full-screen editor

VI Introduction

Text editing is probably the most frequent operation in the system.
There is a standard and powerful Editor, namely visual edtior ).

For Linux beginners, VI may be extremely unfriendly.
Good-all operations should be controlled by special keys or key combinations. It seems that these commands have nothing to do with the operations to be executed, and it is very hard to remember, compared to notepad in windows.
It's more "horrible. In fact, this is just a normal phenomenon in the work environment. After a lot of practices, you will find that VI is very reasonable for the use of the keyboard. If you are skilled, Vi can not only become
Your text editing tool is helpful for system configuration and programming.

VI practical operation guide

As a standard for Unix systems
The full-screen text editor, Vi can easily insert, modify, copy and paste three basic editing operations. But unlike other word processors, VI runs in the command mode as the default mode.

More
Complex operations can be completed with a series of buttons.

[Go to VI and exit]

VI has three modes: inserting mode and command mode.
Mode) and ex mode (ex mode ). In the input mode, you can enter text, while in the command mode, you can execute commands such as file editing and archiving.
To execute other
Functions, such as searching, replacing text, and multi-file operations. To enter Vi, you can directly Type VI at the shell prompt
[File name]. If the file does not exist in the current directory, VI creates it.
There are several ways to enter the input mode:

Add (append)
A:
Add content after the cursor position. The content after the cursor moves backward with the new character.
A: add content at the end of the column where the cursor is located.

Insert
(Insert)
I: insert content from the front of the cursor. The content after the cursor moves backward with the added content.
I:
Insert content before the first non-blank character in the column where the cursor is located.

Start (open)
O: Add a column under the column where the cursor is located and enter the input mode.
O:
Add a new column to the column where the cursor is located and enter the input mode.

You can use the ESC key to switch to the command mode at any time. It is enough to press it once or twice in a row. Then, the system will beep to confirm the entry.
Command mode. Then there are multiple exit methods in the command mode:
ZZ save and exit

Save and exit

: WQ save and exit

: W
Save and return command mode

: 30, 60 W [new file] write 30 to 60 rows of content into [new file]

: 30, 60 W>
[File] writes lines 30th to 60 to the end of the [file]

: Q exit

: Q! Exit and discard all unsaved Information

Q: Exit VI and activate ex.

: E file2
Edit file2 without leaving vi

: N: edit the next file

: E! Returns to the last saved status of the current edited file.

VI
Basic editing

Move the cursor]

Moving the cursor is naturally a basic skill in the editing process. You must be familiar with this step.

Common shift
The command keys include:

H shifts one character to the left

J. Move one row down

K move one row up

L move one character to the right

W, W
Jump to the next word (W ignores punctuation)

B and B Jump to the previous word (B ignores punctuation)

$ To the end of a row

^
To the first non-empty character in the row

0 to the beginning of the line

G to the starting position of the buffer

Ng n is an integer to line N.

CTRL-d
Half page down

CTRL-u up half page

CTRL-F to next page

CTRL-B to the previous page

H
Move to the first column of the current window

M moves to the middle column of the current window

L move to the last column of the window │

E
Move to the last letter of the next word

N-Minus signs move to the first non-blank character in the previous column. You can add a number to the front to specify to move to the N Column

N +
The plus sign moves to the first non-blank character in the next column. You can add a number before it to specify to move to the following n columns.

Move FX to the right to x characters

FX
Move to the left to X characters

TX to the right before X

TX to the left before X

; Works with F
Used with T, repeat once

, Used with F and t, repeat once in the opposite direction

[Edit]

X
Delete the character where the cursor is located, and move the text behind the cursor to the left. If the last character of a row is deleted, move the cursor to the left.

D
Is a DELETE command that acts on text objects and can be moved across text blocks. For example, W can be used to jump to the start of the next word, then DW will delete the word from the cursor position to the start of the next word; 6 W will delete
The start of the Sixth word behind the word where the cursor is located, etc.

Dd deletes the row where the cursor is located.

R: Modify the character where the cursor is located.

R
Enter the substitution status. The new content will overwrite the original content until you press [ESC] to return to the 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.

U undo the last modification to the text. Standard VI can only perform Level 1 undo, while Vim can perform Level 1 undo

U
The undo command is used to cancel all changes made to the row where the cursor is located from the last time the cursor was moved to the row.

N (
Move the left brackets to the beginning of the sentence. You can move n sentences forward by adding numbers! .? Three symbols to define

N)
Move the right parenthesis to the beginning of the next sentence, and add a number to the front to specify to move n sentences backwards.

N {
Move the left arc to the beginning of a paragraph. You can move n paragraphs forward by adding a number. The section is defined as a blank column between paragraphs.

N}
Move the right arc to the beginning of the next paragraph, and add a number to the front to specify to move n paragraphs

I am afraid some readers have begun to speak out: so many"
It is too complicated to edit the text by using commands! Or that sentence, "Practice makes perfect ". If you are skilled in using these commands, you are not in the control of your brain.
Skilled enough to do everything you want

[Command Composition]

Now you can refer to the instructions in VI. As you can see above, many commands are controlled.
Keys, numbers, and so on. That's right! Many of them are roughly composed of commands and scopes. For example, DW is a group with D and W.

To delete
D (elete) W (ORD ).

Command list:

D. Delete)

Y
Copy (Yank)

P placement (Put)

C Change)

The range can be:

E
Cursor position to the last letter of the word

W cursor position to the first letter of the next word

B. Place the cursor on the first letter of the last word.

$
Cursor position to the last letter in the column

0 cursor position to the first letter in the column

) Place the cursor to the first letter in the next sentence.

(
Place the cursor in the first letter of the sentence

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

{Place the cursor in the first letter of the paragraph

In fact, the combination of commands is far more than that, and more needs to be realized in practice.

[Search for samples]

When moving in text
Usually, you need to find the text composed of a certain pattern. VI also provides the corresponding command key

/Move pattern to the next row containing Pattern

? Move pattern to the previous line that contains Pattern

/Search again

? Repeat Lookup

N
Duplicate search in the Same Direction

N repeated searches in the opposite direction

/Pattern/+ n move to the nth row after the row where the next pattern is located

? Pattern? -N
Move to the nth row before the row where the previous pattern is located

/Pattern/Z-re-paint the screen so that the next line containing pattern is located at the bottom of the screen

%
Move to the arc or large arc matching the cursor's arc

Mark text]

You can also mark a text segment to facilitate future jump.

Mchar
Mark the position of the current cursor with the letter char

'Char moves to the position marked by char

'Char moves to the beginning of the row where the char mark is located

"Move to the last position of the current row (after the cursor moves)-a double quotation mark

''
Move to the beginning of the row where the first row is located (after the cursor moves)-two single quotes

[Buffer zone]

The buffer function of VI is also
It has a major feature. It can be divided into several categories

1. Famous buffer zone

If you want to save several different texts in different buffers for storage at the same time, you need to use
Buffer zone.

The famous buffer zone can open up to 26 independent storage areas for storing the text for deletion or sampling operations. It is named after double quotation marks (") and lowercase letters.
To "Z. You can add a buffer name before a command to perform sampling or deletion.

To a buffer. The following are some commands about the famous buffer zone:

"Azdw
Delete the last two words and put them into buffer.

"JD deletes the content from the cursor to the end of the row and places it in the buffer J

"JP
Place the content of buffer J behind the cursor

"By) sampling from the cursor to the end of the sentence and placing it in buffer B

"A2yy
Sample two rows to buffer A and delete the original content of buffer.

"Ad4w deletes the last four words of the cursor and attaches them to buffer.

"Ay)
Sample the content from the cursor to the end of the row and append it to buffer

2. Delete the buffer

In addition, VI also has a numbered deletion buffer. The undo command can only be restored.
Only when this operation is the last operation. However, Vi can put all the content deleted in the last nine times into a buffer with nine numbers ranging from 1 to 9. Most

Last deleted warranty
There is buffer 1, and the next time in the buffer 2, and so on. For example, "2 P" can be placed in the second deletion buffer.

[With Unix
Shell Combination]

As a standard configuration for various Unix systems, VI also provides good interfaces with shell. The following are some common commands:

: Sh
Run shell until you exit with Ctrl-D.

:! CMD
Run the shell command CMD and return to VI. In addition, the CMD here can use some special extended characters (defined in VI), such as # which can be extended to the backup file name; % which can be extended to the current file
Name ;! Can be expanded to the previous

Shell commands

:!! Repeat the previous shell command

: Object cmd
Run the shell command to replace the text object with the CMD input. If cmd Accepts standard input, a specific text object must be used.

N !! CMD
Run the shell command cmd. Replace n lines of text starting from the current line with the CMD input. The default value of N is 1. If cmd receives the standard input, a specific line must be used.

[Ex
Function Summary]

VI is one of the most widely used text editors in Unix-like operating systems. Both Unix, Solaris, and Linux are provided by default.
This tool. As long as you are familiar with Vi, you can freely access any UNIX operating system platform.
To edit the text. There are many materials on the basic usage of VI.
It is not to repeat these basic usage, but to provide some practical skills for people who have a preliminary understanding of the basic usage of VI, so that they can use the VI editing text more quickly and efficiently.
.

VI
There are command mode and insert mode. VI is in command mode after it is started. In command mode, you can move the cursor, copy and paste text at will. Use "A", "I", "C ",
"C", "O", and "O"

To switch from command mode to insert mode. In insert mode, you can enter text and Press ESC to switch back to command mode. To differentiate different models
In this article, we will show you the operation modes.

In addition, all the operations in this article are in red hat
9.0 in vim. Most of the techniques described in this article are also feasible in various VI versions.

Replacement Operation (some simple replacement commands that are frequently used include :)


S replaces the current character with one or more characters. For example, 5 s indicates replacing the five characters starting from the current character with one or more characters;

◆ S
Replace the current row with one or more characters;

◆ Replace a single character in R.

Text Format Conversion

Set
Convert the DOS text to UNIX text. The command is as follows:

: 1, $ S/^ m/g

^ M is the same
Press Ctrl + V + m to return the carriage return.

Add a string at the beginning and end of a row

Use the following command to add a string to the beginning of each line of the text
"Newstring". Note the existence of spaces.

: G/^/S // newstring/g

: % S/^/newstring/g

Use the following command to add the string "newstring" at the end of each line ":

: G/$/S // newstring/g

: % S/$/newstring/g

Insert strings from the first row of the text to the first row of the last row.
"Newstring ":

: 3, $ S/^/newstring/

Replace

Replace
For a specific string, the command is as follows:

: % S/old_string/new_string/g

If only 3rd rows in the file are
Replace "old_string" in row 7th with "new_string", and use the following command:

: 3, 7 S/old_string/new_string/

Note: "S" indicates the replacement action, "%" indicates all rows, "G" indicates the full text, "^" indicates the first row, and "$" indicates the end of the row.

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.