Vi editor (2)

Source: Internet
Author: User
Next, use the vi editor. (1) 1.4 move the cursor. many commands in the vi editor are based on the current position of the cursor. therefore, how to move the cursor to locate the desired position is a very important task. The following is a detailed introduction (unless otherwise stated, the commands described below are all common... information &

Next, use the vi editor (1)

 

 

1.4 Move the cursor

Many commands in the vi editor are based on the current position of the cursor. Therefore, it is very important to move the cursor to the desired position. The following is a detailed description (unless otherwise specified, the commands mentioned below are all executed in normal mode ).

1. move forward characters

To move the cursor forward to a character, run the following command: l, Space, or direction key →. If a number n is added before the command, the cursor is moved n characters forward. for example:

# Include

Assuming that the current cursor is on the character c in include, after 5l is used, the cursor will move to e. However, note that the cursor cannot move beyond the end of the current row. of course, if the number given exceeds the number of characters from the current cursor to the end of the row, it can only move to the end of the row.

2. move characters backward

To move the cursor backward, run the following command: h, space key, or direction key cursor. Similarly, if a number n is added before the command, the cursor is moved n characters backward, and the cursor cannot exceed the first travel.

3. move to the next row

The following commands can be used to move the cursor to the next line: +, Enter, j, Ctrl + n, or direction key cursor. There is a difference between these commands. the + and Enter keys move the cursor to the beginning of the next line, and the other commands only move to the next line, with the columns unchanged. If the next row is shorter than the current cursor position, the subscript ends at the end of the row.

4. move to the previous row

Commands that can be used to move the cursor up a line:-, k, Ctrl + p, or direction key cursor. Similar to the preceding command, The-command moves the cursor to the beginning of the previous line, while the other three remain in the same column.

5. move to the beginning of the line

The commands used to move the cursor to the beginning of the current line are 0 and ^. The difference between the two commands is that command 0 is the first character that moves the cursor to the current line, whether or not it is a blank character, the command ^ moves the cursor to the first non-blank character in the current line.

6. move to the end of the row

The command to move the cursor to the end of the current row is $. After the cursor moves to the end of the line, it stays on the last character. if the number n is added before the command, the cursor moves down to the end of the n-1 line.

7. forward by word

The command used to move the cursor forward by word is w and W. Both commands move the cursor forward to the beginning of the next word. The difference is that the command w search term is defined as a punctuation or blank character (such as a tab, line feed, or space character) A separate string of letters or numbers. The word searched by command W is defined as a non-blank character string. For example, there is a string:

Echo l>/proc/sys/net/ipv4/conf/default/rp_filter

Enter command w consecutively, and move the cursor from the beginning of the line at e, l,>,/, p, s ,... ,/, R, and r.

The W command moves the cursor from the beginning of the line at e, l,>,/, and r.

8. move behind words

The commands for moving the cursor backward are B and B. Both commands move the cursor backward to the beginning of a word. Similarly, they have different definitions of words.

9. move to the end of the word

The commands for moving the cursor to the end of the current character are e and E. Their definitions of words are similar to those above.

10. move to the specified row

To move the cursor to the beginning of a specified line, run the following command:

  • : Row number
  • Row number G

Note: The first command is executed in command mode, not in normal mode. If the row number is not displayed, use the following command to display the row number:

: Set number

: Ser nu

1-11.

-11 display the row number in the vi editor

To remove a line number, run the following command:

: Set nonumber

: Set nonu

1.5 screen scrolling

 

Screen scrolling is often involved during file editing and viewing.

 

In the vi editor, although you can use the Page Up and Page Dawn keys on the keyboard to complete these operations, or even use the direction keys seek and seek, the efficiency is relatively low, next let's take a look at the commands for scrolling the screen.

 

1. scroll back

 

Command: Ctrl + f (retain the last two lines of the previous screen after scrolling)

 

2. scroll back to the half screen

 

Command: Ctrl + d

 

3. scroll forward

 

Command: Ctrl + B

 

4. scroll forward half screen

 

Command: Ctrl + u

 

5. screen positioning

 

Use the command zz to set the current line to the center of the screen, use the command zt to set the current line to the top of the screen, and the command zb will put the current line at the bottom of the screen.

 

Locating the screen directly on the first or last screen of the file is also a common problem. The gg and G commands can complete this function. after using these commands, the cursor is directed to the first line or the last line of the last screen on the first screen.

 

1.6 text input, deletion, and modification

 

Text input, deletion, and modification are the basic operations for file editing. most of the commands change the vi editor from normal mode to edit mode. The following describes these commands.

 

1. Insert command

 

The text insertion commands are I and I. I inserts the output character before the current cursor position. Command I inserts the character entered after it into the beginning of the line where the current cursor is located.

 

2. Additional commands

 

The command for appending text is a and A, where command a inserts the character entered after it into the current cursor position, command A adds the character entered after it to the end of the row where the current cursor is located.

 

3. start a new line

 

The commands for starting a new line are o and O. The Command o starts one line under the current line, and the command O starts one line above the current line. After the new line is created, the cursor stays at the beginning of the new line and waits for the input text.

 

4. delete characters

 

The commands for deleting characters are x and X. Command x deletes the character at which the cursor is located, and command X deletes the character before the cursor. If a number n is given, the n characters starting from the character where the cursor is located are deleted to the right.

 

5. delete text objects

 

The command dd deletes the row where the cursor is located, and the command D deletes all characters starting from the cursor position to the end of the line.

 

The letter d can be combined with the cursor movement command, for example:

 

D ^: removes the cursor from the cursor position to the beginning of the row, excluding the cursor position.

D $: delete the cursor from the cursor position to the end of the row, including the cursor position, which is the same as D.

DG: delete the content from the current row to the end of the file.

Dgg: delete the contents from the current row to the file header.

6. modify the command

 

The commands for modifying text are c, C, and cc. they replace the original text with the new input text, which is equivalent to deleting the original text, use command I to insert new text.

 

For example, there is a string: Hello World!

 

Assume that the cursor is currently at e. after the command cw is input, the screen is displayed as follows:

 

H World!

 

At this point, the cursor is at the space after H, and then enter text I and press Esc. the screen is displayed as follows:

 

Hi World!

 

From the above we can see that cw is only a character that modifies the current position of the cursor to the end of the word. to modify the entire word, you can use the command caw.

 

The C command is used to modify the text from the cursor position to the end of the line. If you add a number n to the front, the content of the n-1 row from the current cursor position to the current row will be deleted.

 

The function of the command cc is the same as that of C, but the modification range is different. it modifies the entire line of content where the cursor is located.

 

7. replace the command

 

Commands that replace text are r and R. The command r replaces the character where the cursor is located with a single character entered after it. if a number n is added before r, then, a single character entered after the cursor is replaced by n characters starting from and backward.

 

The R command uses the text entered after it to replace several characters starting at the place where the cursor is located. each input of a character replaces the original character, and the additional part is appended to the end.

 

1.7 copy and paste

 

The buffer in the vi editor is divided into the untitled buffer and the named buffer. The untitled buffer is numbered by number. There are 9 in total. As mentioned earlier, the delete command x and dd can be used to delete text. In fact, the deleted content is stored in the buffer, and the last deleted content is saved in buffer 1, in the buffer zone 2, and so on, we can use the command to extract them back.

 

1. paste the post

 

The commands for pasting buffer content are p and P. The difference between the two commands is that the command p places the text below the current line or after the current cursor, command P places the text above the current line or before the cursor.

 

For example, open the following file in the vi editor and delete and paste the file.

 

?

Case 5:

Case 4:

Case 3:

Case 2:

Case 1:

Case 0:

Assume that the cursor is currently in line 2nd and the dd command is executed four times consecutively. the screen is as follows:

 

?

Case 5:

Case 0:

Enter the command p, and the screen is displayed as follows:

 

?

Case 5:

Case 0:

Case 1:

Enter the "3 p" command, and the screen is displayed as follows:

 

?

1

2

3

4

Case 5:

Case 0:

Case 1:

Case 3:

Note: "3 is used to reference buffer 3. a single double quotation mark is followed by the buffer number.

 

2. copy

 

The name buffer is a ~ letter ~ Named by z, the naming buffer can be used to store several text segments, so that they can be easily accessed, moved, or rearranged in the future. When accessing these buffers, a single double quotation mark is used as before.

 

The command for copying text can be in the following two formats:

 

Yy

 

Y <光标移动命令>

 

Here, yy indicates copying the entire line of content, while the latter limits the copied text by moving the cursor. If no buffer name is specified, the text is inserted into the unknown buffer. If the buffer zone is written in uppercase letters, the original content in the buffer zone will not be overwritten.

 

3. do not use the replication and movement of the buffer zone

 

The command used is co. Its basic format is as follows:

 

: <开始行> , <结束行> Co <目标行>

 

This command is executed in command mode, where the start line and the end line identify the range of text replication, while the target line is the position of text paste.

 

1.8 search and replace

 

1. search

 

/Str searches for the str string forward and positions the cursor on the first letter of str to facilitate further operations. press the "/" key and the cursor jumps to the last line of the vi window, then, wait for the string to be searched. after the string is entered, press Enter to start searching. then, the editor will highlight the search result. Next, run the command n to move the cursor in the search result.

 

Command? Work in the same way as/. only search is in the opposite direction.

 

Use the commands described above, such as/the, to search for matching words soothe and there. in this case, you need to use the following form:

 

/\

 

Similarly, to match the beginning and end of a line, use the ^ and $ Characters. for example:

 

Big $

 

This command can only match the word "big" at the end of a line.

 

: Set ignorecase

 

If you do not care about the case sensitivity of the target string, you can use the following command to set it.

 

: Set nowrapscan

 

The preceding command is to search for the target string during input, that is, the target can be found after the input is complete.

 

2. replacement command

 

The commands used are s and S. The Command s replaces the character at the cursor with the text entered later. Command S replaces the new text with the current line.

 

3. global replacement

 

The global replacement command has several common formats:

 

G/s1/s // s2/

 

Replace string s2 with s1 for the first time in all rows that contain string s1, that is, replace s1 with S2.

 

For example, the program is as follows:

 

?

Int main ()

{

Int x;

For (x = 1; x <= 10; x ++)

Printf ("% d \ n", x );

Return 0;

}

After entering the command g/x/s // abc/, the above program becomes:

 

?

Int main ()

{

Int abc;

For (abc = 1; x <= 10; x ++)

Printf ("% d \ n", abc );

Return 0;

}

We can see that the first appearance of x in each row is changed to abc.

 

To replace all the strings s1 in the file with the string s2, run the following command:

 

G/s1/s // s2/g

 

For example, the program is as follows:

 

?

Int main ()

{

Int x;

For (x = 1; x <= 10; x ++)

Printf ("% d \ n", x );

Return 0;

}

After entering the command g/x/s // abc/g, the above program becomes:

 

?

Int main ()

{

Int abc;

For (abc = 1; abc <= 10; abc ++)

Printf ("% d \ n", abc );

Return 0;

}

We can see that all x is replaced with abc.

G/s1/s // s2/gc

This command is basically the same as the above function. it only prompts to confirm before replacement. If yes, it will be replaced; otherwise, it will not be replaced.

 

Author IT stupid
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.