Home > Operation and Maintenance > Linux Operation and Maintenance > Organize and share detailed explanations of vi commands in Linux

Organize and share detailed explanations of vi commands in Linux

WBOY
Release: 2021-12-29 18:30:23
forward
2941 people have browsed it

This article brings you a detailed analysis of the vi command. The vi editor is the standard editor under all Unix and Linux systems. Its power is not inferior to any of the latest text editors. I hope it will be helpful to everyone. help.

Organize and share detailed explanations of vi commands in Linux

Here is just a brief introduction to its usage and a small part of the instructions. Since the vi editor is exactly the same for any version of Unix and Linux systems, you can learn more about it wherever vi is introduced. Vi is also the most basic text editor in Linux. After learning it, you will have no problem in the Linux world.

1、The basic concept of vi

Basically vi can be divided into three states, namely command mode (command mode), insert mode (Insert mode) and bottom line mode (last line mode), the functions of each mode are distinguished as follows:

1) Command line mode command mode)

Control the movement of the screen cursor, the deletion of characters, words or lines, move and copy a section and enter Insert mode, or go to last line mode.

2) Insert mode(Insert mode)

Only in Insert mode, Only then can you enter text, and press the "ESC" key to return to the command line mode.

3) Last line mode

Save the file or exit vi, you can also set the editing environment, such as searching for characters String, list line numbers...etc.

However, generally when we use vi, we simplify vi into two modes, that is, the last line mode (last line mode) is also included in the command line mode (command mode).

2、Basic operations of vi

a) Enter vi

After entering vi and the file name at the system prompt, after entering vi, you are in "command mode (command mode)". You need to switch to "insert mode (Insert mode)" before you can enter text. People who use vi for the first time will want to use the up, down, left and right keys to move the cursor first. As a result, the computer keeps beeping, which makes them mad. So after entering vi, don't move around and switch to "Insert mode". !

b) Switch to Insert mode to edit the file

Click the letter in "command mode" "i" will enter "Insert mode", and then you can start entering text.

c) Switching of Insert

Currently in "Insert mode", you can only keep inputting text. If you find that you are losing Wrong word! If you want to use the cursor keys to move back and delete the word, you must first press the "ESC" key to go to "command mode (command mode)" and then delete the word.

d) Exit vi and save the file

In "command mode (command mode)", click the ":" colon key Enter "Last line mode", for example:

: w filename (enter "w filename" to save the article with the specified file name filename)

: wq (enter "wq", save and Exit vi)

: q! (Enter q! to force exit vi without saving)

3, command mode function key

1). Insert mode

Press "i" to switch to insert mode "insert mode", press "i" to enter insert After mode, file input starts from the current position of the cursor;

After pressing "a" to enter insert mode, text input starts from the position next to the current cursor position;

Press "o" After entering insert mode, a new line is inserted and text is entered starting from the beginning of the line.

2). Switch from insert mode to command line mode

Press the "ESC" key.

3). Move the cursor

vi can directly use the cursor on the keyboard to move up, down, left, and right, but regular vi uses lowercase English The letters "h", "j", "k", and "l" control the cursor to move one space to the left, down, up, and right respectively.

  • Press "ctrl" "b": the screen moves "back" one page.

  • Press "ctrl" "f": the screen moves "forward" one page.

  • Press "ctrl" "u": the screen moves half a page "back".

  • Press "ctrl" "d": the screen moves half a page "forward".

  • Press the number "0": move to the beginning of the article.

  • Press "G": Move to the end of the article.

  • Press "$": Move to the "end" of the line where the cursor is located.

  • Press "^": Move to the "beginning" of the line where the cursor is

  • Press "w": The cursor jumps to the next word The beginning of

  • Press "e": the cursor jumps to the end of the next character

  • Press "b": the cursor returns to the previous one The beginning of the word

  • Press "#l": the cursor moves to the #th position of the line, such as: 5l, 56l.

4). Delete text

  • 「x」:Delete each time you press it One character "behind" the cursor position.

  • 『#x』: For example, "6x" means to delete the 6 characters "behind" the cursor position.

  • "X": Uppercase X, each time you press it, delete the character "before" the cursor position.

  • 「#X」:For example, "20X" means to delete the "first" 20 characters at the cursor position.

  • 「dd」:Delete the line where the cursor is.

  • 「#dd」:Delete # lines starting from the line where the cursor is located

5). Copy

  • 「yw」:Copy the characters from the cursor to the end of the word into the buffer.

  • 「#yw」:Copy # words to the buffer

  • 「yy」:Copy the line where the cursor is to the buffer.

  • 『#yy』: For example, "6yy" means copying 6 lines of text "counting down" from the line where the cursor is located.

  • 「p」: Paste the characters in the buffer to the cursor position. Note: All copy commands related to "y" must be combined with "p" to complete the copy and paste functions.

6). Replace

  • 「r」: Replace the character where the cursor is .

  • 『R』: Replace the character where the cursor is until the "ESC" key is pressed.

7).Reply to the last operation

「u」: If you execute a command by mistake, you can immediately Press "u" to return to the previous operation. Press "u" multiple times to perform multiple replies.

8). Change

  • "cw": Change the word where the cursor is to the end of the word

  • 『c#w』: For example, "c3w" means changing 3 characters

9). Jump to the specified line

  •  「ctrl」「g」Lists the line number of the line where the cursor is located.

  • "#G": For example, "15G" means moving the cursor to the beginning of line 15 of the article.

4、Introduction to commands in Last line mode

Before using "last line mode", please remember First press the "ESC" key to make sure you are in "command mode", then press the ":" colon to enter "last line mode".

A) List line numbers

"set nu": After entering "set nu", it will be in front of each line in the file List line numbers.

B) Jump to a certain line in the file

「#」:The 「#」 sign represents a number, enter one after the colon If you enter the number 15 and press Enter again, you will jump to the 15th line of the article.

C) Search for characters

『/Keyword』: Press the "/" key first, and then enter the character you want to find. If it is the first time If the keyword you are looking for is not what you want, you can keep pressing "n" to search further until you find the keyword you want.

"?Keyword": Press the "?" key first, and then enter the characters you want to find. If the keyword you are looking for for the first time is not what you want, you can keep pressing "n" to move forward. Find the keyword you want.

D) Save the file

"w": Enter the letter "w" in the colon to save the file.

E) Leave vi

"q": Press "q" to exit. If you cannot leave vi, you can follow "q" A "!" forces you to leave vi.

『qw』: It is generally recommended to use it with "w" when exiting, so that the file can be saved when exiting.

5、vi command list

1、The following table lists some keys in command mode Function:

  • h Move the cursor one character to the left

  • l Move the cursor one character to the right

  • kMove the cursor up one line

  • ##jMove the cursor down one line

  • ^Move the cursor to the beginning of the line

  • 0Number "0", the cursor moves to the beginning of the article

  • GThe cursor moves to the end of the article

  • ##$ Move the cursor to the end of the line
  • Ctrl f to scroll forward
  • Ctrl b to scroll backward
  • Ctrl d to turn half the screen forward
  • Ctrl u to turn half the screen backward
  • iInsert characters before the cursor position
  • aIncrease the character after the cursor position
  • oInsert a new line and enter
  • from the beginning of the line

  • ESC returns from the input state to the command state
  • xDelete the characters after the cursor
  • #xDelete the characters after the cursor # characters
  • X (capital X), delete the characters before the cursor
  • #X delete # characters before the cursor
  • ddDelete the line where the cursor is
  • #ddDelete # lines from the line where the cursor is
  • ywCopy a word at the cursor position
  • ywCopy a word at the cursor position
  • yyCopy a line at the cursor position
  • #yyCopy # lines from the line where the cursor is
  • pPaste
  • uCancel Operation
  • cwChange one character at the cursor position
  • #cwChange # characters at the cursor position
2

, The following table lists some instructions in line command mode

    w filename
  • Save the file being edited as filename

    wq filename
  • Save the file being edited as filename and exit vi

    q!
  • Abandon all changes and exit vi

    set nu
  • Display line number

    /or?
  • Search, enter what you want to find after /

    n
  • Use together with / or ?, if the search content is not the keyword you want to find, press n or backward (with / (used in conjunction with ?) or forward (used in conjunction with ?) to continue searching until found.

For the first time using vi, there are a few things to note:

1. After opening a file with vi, it is in "command mode". You need to switch to "Insert mode" before you can enter text. Switching method: Click the letter "i" in "command mode" to enter "Insert mode". At this time, you can start inputting text.

2. After editing, you need to switch from insert mode to command line mode to save the file. Switching method: press the "ESC" key.

3. Save and exit the file: Enter: wq in command mode! (Don’t forget wq in front of:)

Move on the screen

Vi provides three options for moving the cursor across the full screen and the file itself does not scroll. Order. They are the H, M and L commands respectively.

HCommand

This command moves the cursor to the beginning of the first line of the screen (that is, the upper left corner), which is the first line of the current screen. one line, rather than the first line of the entire file. Use this command to quickly move the cursor to the top of the screen. If you add the number n before the H command, the cursor will be moved to the beginning of the nth line.

MCommand

This command moves the cursor to the beginning of the middle line of the screen display file. That is, if the current screen is full, move to the middle line of the entire screen; if it is not full, move to the middle line of those lines of text. Use this command to quickly move the cursor from any position on the screen to the beginning of the middle line of the screen display file. For example, in the case of the above screen display (no matter where the cursor is on the screen), in command mode, after entering the command M, the cursor will move to the letter a on the add line.

It is also worth mentioning that using the command dM will delete all the contents from the current line of the cursor to the middle line of the file displayed on the screen.

LCommand

When the file display content exceeds one screen, this command moves the cursor to the beginning of the bottom line on the screen ; When the file display content is less than one screen, this command moves the cursor to the beginning of the last line of the file. It can be seen that using this command can quickly and accurately move the cursor to the bottom of the screen or the last line of the file. If you add the number n before the L command, the cursor will be moved to the beginning of the nth line from the bottom of the screen. For example, in the case of the above screen display (no matter where the cursor is on the screen), in command mode, after entering the command 3L, the cursor will move to the letter x on the x line.

It is also worth mentioning that using the command dL will delete all content from the current line of the cursor to the bottom line of the screen.

Move the cursor by word

First introduce the concept of "word" in Vi. "Word" has two meanings in Vi. One is a broad word, which can be anything between two spaces. Another type of word is a word in a narrow sense. In this sense, English words, punctuation marks and non-alphabetic characters (such as!, @, #, $, %, ^, &, *, (,), -, , {, }, [, ], ~, |, ", <, >, /, etc.) are regarded as one word. Therefore, the above line includes the four characters {, int, k,; .

Using uppercase commands in Vi generally means treating words in a broad sense, and using lowercase commands means treating them in a narrow sense.

After figuring out the meaning of words in Vi, we can introduce word-by-word The command to move the cursor.

Vi provides a total of three sets of commands for moving the cursor word by word, namely:

w and W command

Move the cursor right to the beginning of the next word;

e and E command

if the cursor starts If the position is within a word (that is, not at the end of a word), this command will move the cursor to the end of the word; if the starting position of the cursor is at the end of a word, this command will move the cursor to the end of the next word.

b and B

If the cursor is within the word (that is, not at the beginning of the word), this command will move the cursor to the beginning of the word. ; If the cursor is at the beginning of the current word, this command will move the cursor to the beginning of the previous word.

Move the cursor by sentence

In Vi, A sentence is defined as a sequence of characters that ends with a comma (,), a period (.), a question mark (?), and an exclamation point (!), followed by at least two (inclusive) spaces or a newline character.

Vi provides two commands for moving the cursor sentence by sentence, which are:

1. (Command

Move the cursor to the beginning of the previous sentence.

2. ) Command

This command moves the cursor to the beginning of the next sentence.

Move the cursor by segment

In Vi, a segment is defined as a fragment that starts and ends with a blank line. Vi provides two commands for moving the cursor by segment, which are:

1. { Command

This command moves the cursor forward Move to the beginning of the previous paragraph;

2. } Command

This command moves the cursor backward to the beginning of the next paragraph.

Screen scrolling

The screen command moves the cursor in screen units and is often used for scrolling and paging files. It should be noted that screen commands are not cursor movement commands and cannot be used as text qualifiers in delete commands.

Screen scrolling commands can be used in both command mode and text input mode.

1. Scroll command

There are two scroll commands:

· < Ctrl u > Scroll half the screen forward (in the direction of the file head);

· < Ctrl d > Scroll half the screen backward (in the direction of the file tail).

You can add a number n before these two commands, and the screen will scroll n lines forward or backward. And this value is remembered by the system. When you use the < Ctrl u > and < Ctrl d > commands to scroll in the future, the corresponding number of lines will be scrolled.

2. Paging command

There are also two paging commands:

· < Ctrl f > Scroll a full screen (i.e. one page) toward the end of the file;

· < Ctrl b > Scroll the screen a full screen (i.e. one page) toward the beginning of the file.

You can also add a number n before these two commands to move the screen forward or backward n pages.

3. Status command < Ctrl G >

command displays the vi status information on the vi status line, including the one being edited The file name, whether it has been modified, the current line number, the number of lines in the file, and the percentage of the line before the cursor in the entire file.

4. Screen zeroing commands

Vi provides three commands related to screen zeroing. Their formats are:

· [line number] z [number of lines]

· [line number] z [number of lines] .

·[Line number] z [Number of lines] _

If the line number and line number are omitted, these three commands will use the current line where the cursor is located as the first line, middle line and last line of the screen. The line is redisplayed; if a line number is given, the line corresponding to the line number is displayed as the current line in the first, middle, and last lines of the screen; if a line number is given, it specifies the line displayed on the screen number.

[Example 13]

8z16: Use the 8th line in the file as the first line displayed on the screen, and display a total of 16 lines.

15z .: Use the 15th line in the file as the middle line of the screen display, and the number of displayed lines is the entire screen.

15z 5_: Use the 15th line in the file as the last line displayed on the screen, and the number of displayed lines is 5 lines.

Text insertion operation

In command mode, any characters entered by the user will be interpreted and executed by Vi as a command. If the user wants to treat the entered characters as text content, first The working mode of Vi should be switched from command mode to text input mode. The way to switch is to use the following command.

Insert command

Vi provides two insert commands: i and I.

iCommand

Inserting text starts before the cursor position, and keys can be used to delete incorrect input during the insertion process. At this time, Vi is in the insert state, and the word "--INSERT--" (insert) is displayed on the bottom line of the screen.

ICommand

This command moves the cursor to the beginning of the current line and then inserts text before it.

Append command

Vi provides two additional insertion commands: a and A.

aCommand

This command is used to append new text after the current position of the cursor. The newly entered text is placed after the cursor, and the original text behind the cursor will be moved backward accordingly. The cursor can be anywhere on the line.

ACommand

The difference between this command and the a command is that the A command will move the cursor to the end of the line and start from there. Insert new text. When the A command is entered, the cursor automatically moves to the end of the line.

a and A commands are the only ways to insert text to the end of the line.

Open (open) command

Whether it is the Insert command or the append command, the inserted content starts from a certain position in the current line. If we want to insert some new lines before or after a certain line, we should use the open command.

Vi provides two open commands: o and O.

oCommand

This command will open a new line below the line where the cursor is, and place the cursor at the beginning of the line. Wait for text to be entered. It should be noted that when using delete characters, only characters after the starting position of insert mode can be deleted, and previous characters will not work. You can also enter some control characters in text input mode. For example, Ctrl l inserts a page break, which is displayed as ^L.

OCommand

The opposite of the o command, the O command inserts a line above the line where the cursor is and places the cursor on that line at the beginning of the line, waiting for text to be entered.

11.3.4Text modification

In command mode, you can use various related commands provided by Vi to modify the text Make modifications, including deletion, copying, substitution and replacement of text content.

Text deletion

When editing text, we often need to delete some unnecessary text. We can use keys to delete incorrectly entered or unnecessary text, but there is a limitation at this time. After deleting to the beginning of the line, it is impossible to delete the content of the line above.

In command mode, Vi provides many deletion commands. Most of these commands start with d. Commonly used ones are:

1. Delete a single character

x: Delete the character at the cursor. If a number n is added before x, n characters to the right starting from the cursor position are deleted.

X: Delete the character in front of the cursor. If a number n is added before X, n characters to the left starting from the character before the cursor are deleted.

Obviously these two commands are quick ways to delete a small number of characters.

2. Delete multiple characters

dd: Delete the entire line where the cursor is. You can add a number n before dd to delete the current line and the following n-1 lines.

D or d$: The two commands have the same function, they both delete the content from the cursor to the end of the line.

d0: Delete the content starting from the character before the cursor to the beginning of the line.

dw: Delete a word. If the cursor is in the middle of a word, delete from the cursor position to the end of the word. Like the dd command, you can add a number n before dw to delete n specified words.

It doesn't matter if the user accidentally deletes the text by mistake. Vi provides commands to restore the mistaken operation, and the restored content can be moved and placed anywhere in the text. The recovery command uses ² np, where n is the register number. This is because there are 9 registers inside Vi used to maintain deletion operations, represented by numbers 1, 2, ¼, and 9 respectively. They respectively store the contents deleted by the dd command in the past. These registers form a queue. For example, the most recent content deleted using the dd command is placed in register 1; when the next time the dd command is used to delete text content, Vi will transfer the contents of register 1 to register 2, and register 1 will be the content deleted by the most recent dd command. By analogy, Vi can save the last nine contents deleted with the dd command, while the previous contents deleted with the dd command are discarded.

In the last line mode, you can also delete the file content, but it can only delete the entire line. All lines within a specified range (starting line number, ending line number) can be deleted at one time. delete all. It should be noted that when deleting in this way, Vi does not put the deleted content into the register. Therefore, when an accidental deletion occurs, the ² np command cannot be used to restore, and only the u command can be used for limited recovery.

Finally, how to delete the entered text in text input mode. The user can just use the key combination. At this time, the cursor will return to the position where the insertion started, and Vi is still in text input mode.

Cancel the previous command (Undo)

Cancel the previous command (Undo), also known as the recovery command, is a very useful command. It can cancel the previous command. The impact of a misuse or inappropriate operation on a file, returning it to the state before the misuse or inappropriate operation was performed.

There are two forms of canceling the previous command. Type the characters u and U in command mode. Their functions are to cancel the command just entered and restore to the original situation. Lowercase u and uppercase U differ in specific details. The difference between the two is that the function of the uppercase U command is to restore the situation before the command was mistakenly operated, that is, if the U command is used after inserting the command, the content just inserted will be deleted;

If you use the U command after deleting the command, it is equivalent to inserting the just deleted content at the cursor. Here, all commands that modify text are regarded as insertion commands. In other words, the U command can only cancel the previous operation. If you use the U command to undo the previous operation, when you press the U key again, it will not undo the previous operation, but the operation just performed by the U command. That is to say, it is restored to the state before the first use of the U command, and the result is that nothing is done. The function of the lowercase u command is to restore the current line to the state before it was edited, regardless of how many times the line has been edited.

Note: You can still use the cancel command for the cancel command. At this time, a "negative makes a positive" effect will occur, and the file status will be restored to the state before the first execution of the cancel command, as if no operation was performed. For example, in the above example, if you use command U again, the content displayed on the screen will still be the inserted content.

Repeat command (Redo)

Repeat command is also a very commonly used command. In text editing, we often encounter the need to repeat some operations mechanically. In this case, we need to use the repeat command. It allows the user to easily execute a complex command just completed previously.

Repeat commands can only work in command mode, just press the "." key in this mode. When executing a repeated command, the result is dependent on the current position of the cursor.

Modification of text content

Modification of text content means that during the editing process, certain characters and certain lines in the text can be modified, that is, using The newly entered text replaces the old text that needs to be modified. It is equivalent to using the delete command to delete the content that needs to be modified, and then using the insert command to insert new content. Therefore, after using the modification command, vi enters the text input mode. After entering the new content, be sure to press back to the command mode, otherwise Vi will always think that it is in the text input mode, and any content entered at this time All are considered modified new content.

Vi provides three modification commands, namely c, C and cc. The range of text they modify is limited by both the cursor position and the cursor movement command. These three modification commands are introduced below.

cCommand

c is followed by the cursor movement command to limit the scope of the modified content to start from the current position of the cursor to the specified position. The range of modified text in command c is limited by both the cursor position and the cursor movement command.

C、c$command

C、c$command can modify from the current position of the cursor to the end of the line or from the current position of the cursor to the end of a line.

When the number n is added before command C, it means that the content of the specified number of lines can be modified. For example, the 3C command means to delete the content from the character at the cursor position (note not the entire line) to the following two entire lines, and replace it with the content entered subsequently.

ccCommand

The function of the cc command is the same as C, except that the scope of modification is different from C. It modifies the line starting from the cursor. Content within the range from the starting position (regardless of which column the cursor is in the line) to the end of the line or the end of a specified line. The entire content of the line where the cursor is located is replaced by the newly entered content.

Same as command C, you can also add the number n before cc to indicate that you want to modify the contents of n lines in total starting from the current line of the cursor. For example, 5cc means to first delete the line where the cursor is and the 4 lines below it, and then enter the content to be modified.

Replacement of text

Replacement of text is to replace the original text with the newly entered text. It is the same as text modification. It also performs the deletion operation first and then the insertion operation.

The replacement commands provided by Vi include replacement command, replacement command and word replacement command.

1. Replace command: r and R

r: Replace the character at the current cursor with a character entered subsequently.

R: Replace several characters from the current cursor position and following with the text entered subsequently. Each character entered replaces the original character until the < Esc > key is pressed to end the substitution. If the number of newly input characters exceeds the number of original corresponding characters, the extra part will be appended.

If you add a number before the command R, such as 5R, it means that the newly entered text is repeated 5 times, but only replaces a character sequence covered by the input text, and the characters that are not covered in the current line The content remains, but the position is shifted to the right.

It is worth mentioning that when using R to replace text content, the newly entered text can occupy multiple lines. When replacing, only the corresponding characters in the line where the cursor is located are overwritten.

It can be seen that the newly entered 6 characters (including spaces, but not including < Esc >) replace the 6 characters in the original line.

2. Replacement command: s and S

s (lowercase): This command means to replace the current cursor with the text entered subsequently. character.

If you only replace the character where the cursor is with a new character, the s command has similar functions to the r command. For example, sh and rh both change the character where the cursor is to h. But there are differences between the two. The r command only completes the replacement, while the s command changes the working mode from command mode to text input mode while completing the replacement. Therefore, the general usage of the s command is: s is followed by the text to be replaced, and finally, it must end the text input mode and return to the command mode.

You can add a number n in front of s, which means that the text entered after s will be used to replace n-1 characters starting from the character where the cursor is and following it (n characters in total).

S ​​(uppercase): This command means to replace the current line (whole line) of the cursor with the newly typed text (regardless of which column the cursor is located).

If a number n, such as 3, is given before S, it means that 3 lines (including the current line of the cursor and the 2 lines below) are to be replaced by the text entered after the S command.

3. Word replacement cw

If we only want to replace the content of a certain word with other text strings, we can use the cw command. What cw replaces is a word in a narrow sense. After entering this command, Vi will delete the word at the cursor, and then the user can enter any text content. After completing the input, press the key, and Vi will replace everything from the original cursor position to the end of the corresponding word with the entered content.

Merge text lines

Vi provides commands to merge certain lines in the text. This command is represented by J (capital letter), and its function is to merge the line where the cursor is and the line below into one line.

If a number n, such as 3, is given before the J command, it means that the current line of the cursor and the 2 lines following it (a total of 3 lines) are merged into one line.

Moving text lines

In Vi, we can easily move text lines within a certain range left or right or from one place to another.

Move text lines left and right.

There are four commands for moving text lines left and right: >, <, >> and <<.

(1)> The command will move the limited text line (the first line of the screen) to the right, usually 8 spaces. The format is: >Cursor movement command. The range of moving text lines is limited by the line where the cursor is and the cursor movement command typed subsequently.

(2) The < command moves the restricted text line to the left. It is used in the same way as the > command, except that the movement direction is opposite. For example, <6M moves each line between the current line of the cursor and the middle line of the screen display 8 spaces to the left.

(3)>> command moves the line where the cursor is located to the right by 8 spaces. If a number n, such as 4, is given before the >> command, it means that the current line of the cursor and the 3 lines below it (a total of 4 lines) are moved to the right by 8 spaces.

(4)<< command moves the line where the cursor is located 8 spaces to the left. Its usage is the same as the >> command, except that the movement direction is opposite.

Different line movement of text lines

We can use the following steps to move text lines from one place to another.

Move the cursor to the first line of the text to be moved;

Press ndd command. Where n is the number of rows to be moved. At this time, Vi will delete the text line to be moved from the file and put it into the deletion register No. 1;

Move the cursor to the destination line;

Press² 1p Remove the text line to be moved from the delete register.

At this time, the text line to be moved will appear at the destination location.

Although the above method can realize inter-line movement of text lines, it is obviously too cumbersome. Vi also provides another quick method, which is to use the command m (Move command) in the last line mode. The usage method is n m k, which means moving the nth row below the kth row.

Now if you want to move line 5 to line 4, you can enter in the last line mode:

:5 m 3

Before command m, also You can specify a line number range (starting line number and ending line number), which means that the text lines within the specified range should be moved to the specified position. For example:

:2, 5 m 0

means moving the contents of the second to fifth lines in the file to the file header (below the zeroth line, this zero line is a dummy line ).

Line number in Vi

Many commands in Vi use values ​​such as line number and line number. If the file to be edited is large, it is very inconvenient to count it yourself. For this purpose, Vi provides the function of adding line numbers to text. These line numbers are displayed on the left side of the screen, and the content of the corresponding line is displayed after the line number.

The command used is: Enter the command in the last line mode:

:set number

It should be noted that the line number added here is only displayed to the user. , they are not part of the file content.

In a larger file, the user may need to know which line the cursor is currently on and where it is in the file. You can use the key combination in command mode. At this time, Vi will display the window at the end. One line displays the corresponding information. This command can be used at any time.

In the last line mode, we can enter the command nu (abbreviation of the word number) to obtain the line number of the current line under the cursor and the content of the line.

Cursor movement operation

In the full-screen text editor, the cursor movement operation is undoubtedly the most frequently used operation. Only when users skillfully use these commands to move the cursor can they quickly and accurately reach the desired location for editing.

The cursor movement in Vi can be done in command mode or text input mode, but the operation methods are different.

In text input mode, you can directly use the four direction keys on the keyboard to move the cursor.

In command mode, there are many ways to move the cursor. Not only can you use the four direction keys to move the cursor, but you can also use the four keys h, j, k, and l instead of the four direction keys to move the cursor. This can avoid conflicts caused by different keyboard definitions on different machines. , and after you become proficient in using it, you can complete all operations without leaving your hands on the letter keyboard, thereby improving work efficiency. In addition, you can also use , , and four keys or key combinations to move the cursor. And the above three types of keys are equivalent in functionality. In addition, there are some commands for moving the cursor. The following is an introduction to how they work:

  • , ® (both are called right keys)

The function of the right key is Move the cursor one position to the right. If you enter a number n before pressing the right key, the cursor will move n positions to the right. For example, 5l means the cursor moves 5 positions to the right. It should be noted that the cursor cannot move beyond the end of the current line. If the given n exceeds the number of characters from the current cursor position to the end of the line, if you use the right arrow key, the cursor can only move to the end of the line; if you use the right arrow key, the cursor moves to the appropriate position on one or more lines below.

  • h、,¬ (left key)

Execute the left key once and the cursor will move one position to the left. Like the right key, you can also enter a number n in front of the left key, and the cursor will move n positions to the left. It should be noted that if you use the left arrow key, the cursor cannot move left beyond the beginning of the line; if you use the left arrow key, the cursor moves to the appropriate position on one or more lines above.

  • j,, ¯ (Down Key)

Execute the Down Key once to move the cursor down one position (i.e. one line), but The column where the cursor is located remains unchanged. When these commands are preceded by the number n, the cursor moves down n lines.

In addition to using the down key to move the cursor down, Vi can also use the key and the " " key to move the cursor down one line or n lines (excluding this line), but at this time the cursor will be at the first character of the line. For example:

3j The cursor moves down 3 lines, and the position of the column where the cursor is located remains unchanged.

3 or 3 The cursor moves down 3 lines, and the cursor is at the beginning of the line.

  • k,, (up key)

#Execute the up key once to move the cursor up one position (i.e. one row), but the column where the cursor is located constant. Also add the number n in front of these commands to move the cursor up n lines.

If you want the cursor to be at the beginning of the line after moving up, you can use the command "-".

  • L (move to the beginning of the line)

L command moves the cursor to the beginning of the current line, that is, moves the cursor to the beginning of the current line. The first non-whitespace character (non-tab or non-space character).

  • $ (Move to the end of the line)

This command moves the cursor to the end of the current line and stops on the last character. If you add a number n before the $ command, the cursor moves down n-1 lines and reaches the end of the line.

  • [line number] G (move to the specified line)

This command moves the cursor to the line specified by the specified line number. head. This kind of movement is called absolute positioning movement.

The cursor has moved to the beginning of line 6.

If the line number is omitted, the cursor moves to the beginning of the last line of the file, that is, regardless of how many screens the file has, it jumps to the last line

Related recommendations: " Linux video tutorial

The above is the detailed content of Organize and share detailed explanations of vi commands in Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template