Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the vi command in Linux

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to use the vi command in Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Since the vi editor is exactly the same for any version of Unix and Linux systems, you can learn more about it anywhere else that introduces vi. Vi is also the most basic text editor in Linux, and after learning it, you will walk freely in the world of Linux.

1. The basic concept of vi

Basically, vi can be divided into three states: command mode (command mode), insert mode (Insert mode) and bottom line mode (last line mode). The functions of each mode are 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 the Insert mode, or to the last line mode.

2) insert mode (Insert mode)

Text input can only be done under Insert mode, and press the "ESC" key to return to command line mode.

3) bottom line mode (last line mode)

To save or exit vi, you can also set the editing environment, such as finding strings, listing line numbers, etc. Wait.

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

2. Basic operation of vi

A) enter vi

After the system prompts for vi and file name, after entering vi, it is in "command line mode (command mode)", you have 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 upper and lower keys to move the cursor first, and as a result, the computer beeps all the time, making themselves angry half to death, so after entering the vi, don't move, switch to "insert mode (Insert mode)".

B) switch to insert mode (Insert mode) to edit the file

Press the letter "I" in "command line mode (command mode)" to enter "insert mode (Insert mode)" and you can start typing text.

C) switching of Insert

Currently in "insert mode (Insert mode)", you can only enter text all the time, if you find that you have typed the wrong word! To use the cursor key to move back and delete the word, press the "ESC" key to go to "command line mode (command mode)" before deleting the text.

D) exit vi and save the file

Under Command Line Mode (command mode), click the: colon key to 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 qquit, do not save the disk and force to exit vi)

3. Command line mode (command mode) function key

1)。 Insert mode

Press "I" to switch to insert mode "insert mode". Press "I" to enter the file from the current position of the cursor after entering insert mode

After pressing "a" to enter insert mode, the text is entered from the next position where the current cursor is located.

When you press "o" to enter insert mode, insert a new line and enter text at 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 and down, but the regular vi uses lowercase letters "h", "j", "k" and "l" to control the cursor to move left, down, up and right respectively.

Press "ctrl" + "b": move the screen one page back.

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

Press "ctrl" + "u": move the screen to "back" half a page.

Press "ctrl" + "d": move the screen half a page to the front.

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 located

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

Press "e": the cursor jumps to the suffix of the next word

Press "b": the cursor goes back to the beginning of the previous word

Press "# l": move the cursor to the # th position on the line, for example: 5lMagne56l

4)。 Delete text

"x": each time you press, delete the "after" character of the cursor location.

"# x": for example, "6x" means to delete "after" 6 characters where the cursor is located.

"X": uppercase X. each time you press it, one character in front of the cursor is deleted.

"# X": for example, "20X" means to delete the "first" 20 characters where the cursor is located.

"dd": delete the line of the cursor.

"# dd": delete # lines from the line where the cursor is located

5)。 Copy

"yw": copies the character where the cursor is located to the suffix into the buffer.

"# yw": copy # words to the buffer

"yy": copies the line of the cursor to the buffer.

"# yy": for example, "6yy" means to copy "count down" six lines of text from the line where the cursor is located.

"p": paste the characters in the buffer to the position of the cursor. Note: all copy commands related to "y" must work with "p" to complete the copy and paste function.

6)。 Replace

"r": replace the character where the cursor is located.

"R": replaces the character where the cursor goes until the "ESC" key is pressed.

7)。 Reply to the last operation

"u": if you execute a command by mistake, you can press "u" immediately to go back to the previous operation. Press "u" multiple times to perform multiple replies.

8)。 Change

"cw": change the word where the cursor is located to the suffix

"c3w": for example, "c3w" means to change 3 words

9)。 Skip to the specified line

"ctrl" + "g" lists the line number of the cursor.

"# G": for example, "15G" means to move the cursor to the beginning of line 15 of the article.

4. Brief introduction of commands under Last line mode

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

A) list line number

"set nu": after entering "set nu", the line number is listed before each line in the file.

B) skip to a line in the file

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

C) find characters

"/ keyword": first press the "/" key, and then enter the character you are looking for. If the keyword you are looking for for the first time is not what you want, you can press "n" until you find the keyword you are looking for.

"? Keyword ": press the"? "key, and then enter the character you are looking for. If the keyword you are looking for for the first time is not what you want, you can press" n "until you find the keyword you are looking for.

D) Save the file

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

E) leave vi

"Q": press "Q" to quit. If you can't leave vi, you can force you to leave vi after "Q".

"qw": it is generally recommended to use it with "w" when you leave, so that you can save the file when you exit.

5. Vi command list

1. The following table lists the functions of some keys in command mode:

H move the cursor one character to the left

L move the cursor one character to the right

Move the k cursor up one line

J move the cursor down one line

^ cursor moves to the beginning of the line

0 number "0", move the cursor to the beginning of the article

Move the G cursor to the end of the article

Move the cursor to the end of the line

Ctrl+f flips forward

Ctrl+b flips backwards

Ctrl+d turns half the screen forward.

Ctrl+u turns back and halves the screen

I insert a character before the cursor position

A begins to increase at the last character of the cursor.

O insert a new line and type at the beginning of the line

ESC retreats from input state to command state

X Delete the characters after the cursor

# x # characters after the cursor is deleted

X (uppercase X), delete the characters before the cursor

# X Delete # characters in front of the cursor

Dd deletes the line where the cursor is located

# dd removes # lines from the number of lines in which the cursor is located

Yw copies a word at the location of the cursor

# yw copy # words where the cursor is located

Yy copies a line where the cursor is located

# yy copies # lines from the number of lines where the cursor is located

P paste

U cancel operation

Cw changes the position of the cursor by one word

# cw change the position of the cursor in # words

2. The following table lists some instructions in the line command mode.

W filename

Save the file being edited as filename

Wq filename

Save the file you are editing as filename and exit vi

Q!

Discard all changes and exit vi

Set nu

Show line number

/ or?

Find, enter what you are looking for after /

N

And / or? Use together, if the search is not the keyword you are looking for, press n or backward (in conjunction with / with) or forward (with? Keep searching until you find it.

For using vi for the first time, here are a few caveats:

1. After opening the file with vi, you are in "command line mode (command mode)", and you have to switch to "insert mode (Insert mode)" before you can enter text. Switching method: press the letter "I" in "command line mode (command mode)" to enter "insert mode (Insert mode)", and you can start typing text.

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

3. Save and exit the file: type: wq in command mode! (don't forget the one in front of wq:)

Move on the screen

Vi provides three commands for moving the cursor across the full screen without scrolling the file itself. They are the H, M, and L commands, respectively.

H command

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, not 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, move the cursor to the beginning of the nth line.

M command

This command moves the cursor to the beginning of the middle line of the on-screen display file. That is, if the current screen is full, move to the middle line of the entire screen; if not, move to the middle line of those lines of the text. This command allows you to quickly move the cursor from anywhere on the screen to the beginning of the middle line of the on-screen display file. For example, in the case shown on the screen above (no matter where the cursor is on the screen), in command mode, after entering the command M, the cursor moves over the letter an on the line add.

It is also worth mentioning that using the command dM will delete everything from the current line of the cursor to the middle line of the on-screen display file.

L command

When the file display content exceeds one screen, the command moves the cursor to the beginning of the bottom line on the screen; when the file display content is less than one screen, the command moves the cursor to the beginning of the last line of the file. As you can see, this command allows you to quickly and accurately move the cursor to the bottom of the screen or to the last line of the file. If you add the number n before the L command, move the cursor to the beginning of the nth line from the bottom of the screen. For example, in the case shown on the screen above (no matter where the cursor is on the screen), in command mode, after entering the command 3L, the cursor moves over the letter x on the line xdistinct +.

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

Move the cursor by word

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

The use of uppercase commands in Vi generally refers to the treatment of words in a broad sense, and the use of lowercase commands in a narrow sense.

Once we understand the meaning of words in Vi, we can introduce the command to move the cursor by word.

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

W and W commands

Move the cursor right to the prefix of the next word

E and E commands

If the cursor start position is within the word (that is, not at the suffix), the command moves the cursor to the suffix; if the cursor start position is at the suffix, the command moves the cursor to the suffix of the next word.

B and B

If the cursor is within the word (that is, not the prefix), the command moves the cursor to the prefix; if the cursor is at the prefix, the command moves the cursor to the prefix of the previous word.

Move the cursor by sentence

In Vi, a sentence is defined as a sequence of characters ending with a comma (,), a full stop (.), a question mark (?), and an exclamation point (!), followed by at least two or more spaces or a newline character.

Vi provides two commands for moving the cursor 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 begins 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 to the beginning of the previous paragraph

2.} command

This command moves the cursor back to the beginning of the next segment.

Screen scrolling

Screen commands move the cursor on a screen-by-screen basis and are often used for scrolling and paging files. It should be noted that the on-screen command is not a cursor movement command and cannot be used as a text qualifier to delete a command.

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

1. Scroll command

There are two commands for scrolling:

< Ctrl+u >

Roll the screen forward (in the direction of the file header) for half a screen

< Ctrl+d >

Scroll the screen backward (in the direction of the end of the file) for half a screen.

You can add a number n before these two commands, and the screen scrolls forward or backward n lines. And this value is remembered by the system and used later.

< Ctrl+u >

And

< Ctrl+d >

When the command scrolls, it also scrolls the corresponding number of lines.

two。 Paging command

There are also two paging commands:

< Ctrl+f >

Scroll the screen toward the end of the file for a whole screen (that is, a page)

< Ctrl+b >

Scroll the screen toward the head of the file for an entire screen (that is, one page).

You can also add a number n before these two commands, and the screen moves forward or backward n pages.

3. Status command

< Ctrl+G >

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

4. Screen zeroing command

Vi provides three commands for zeroing the screen. Their formats are as follows:

[line number] z [number of lines]

[line number] z [number of lines].

[line number] z [number of lines] _

If the line number and number of lines are omitted, the three commands redisplay the current line of the cursor as the first line, the middle line and the last line of the screen; if the line number is given, then the line corresponding to the line number is displayed on the screen as the first line, the middle line and the last line of the screen; if the number of lines is given, it specifies the number of lines to be displayed on the screen.

[example 13]

8z16: line 8 in the file is used as the first line of the screen, and a total of 16 lines are displayed.

15z. Line 15 in the file is taken as the middle line of the screen display, and the number of lines is the whole screen.

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

Text insertion operation

Any character entered by the user in command mode is interpreted and executed by Vi as a command. If the user wants to treat the input character as text content, he should first switch the working mode of Vi from command mode to text input mode. The way to switch is to use the following command.

Insert (Insert) command

Vi provides two insert commands: I and I.

I command

The insertion text starts in front of the cursor, and you can use the key to delete the wrong input during the insertion process. At this point, the Vi is inserted, and the bottom line of the screen displays the word "--INSERT--" (insert).

I command

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

Additional (append) command

Vi provides two additional insert commands: an and A.

A command

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

A command

This command differs from the a command in that the A command moves the cursor to the end of the line and inserts the new text from there. When you enter the A command, the cursor automatically moves to the end of the line.

The an and A commands are the only way to insert text at the end of a line.

Open (open) command

Whether it's the Insert command or the append command, the insertion starts somewhere on the current line. If we want to insert some new rows before or after a row, we should use the open command.

Vi provides two open commands: O and O.

O command

The command opens a new line below the line of the cursor and places the cursor at the beginning of the line, waiting for text to be entered. Note that when using delete characters, only characters after the position starting from the insertion mode can be deleted, not for previous characters. And you can also enter some control characters in the text input mode, for example, Ctrl+l is to insert a page break, displayed as ^ L.

O command

In contrast to the o command, the O command inserts a line above the line of the cursor and places the cursor at the beginning of the line, waiting for text to be entered.

11.3.4 text modification

In command mode, you can use a variety of relevant commands provided by Vi to modify the text, including deleting, copying, replacing and replacing the text content.

Text deletion

When editing text, we often need to delete some unwanted text, we can use the key to delete the wrong or unneeded text, but there is a limitation that when deleted to the header, it is impossible to delete the content of the line above.

In command mode, Vi provides a number of delete commands. Most of these commands start with d. The commonly used ones are:

1. Delete a single character

X: delete the character at the cursor. If you add a number n before x, delete the n characters to the right from the position of the cursor.

X: delete the character in front of the cursor. If you add a number n before X, delete the n characters that start to the left from the character in front of the cursor.

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

two。 Delete multiple characters

Dd: delete the entire line where the cursor is located. You can add the number n before dd to indicate that the contents of the current line and subsequent nmur1 lines can be deleted.

D or dashes: both commands have the same function, deleting content from the beginning of the cursor to the end of the line.

D0: deletes the content that starts one 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 it to the end of the word from the position of the cursor. As with the dd command, you can add the number n before dw to indicate that n specified words are deleted.

It doesn't matter if the user accidentally deletes it, Vi provides commands to recover the misoperation, and you can move the recovered content anywhere in the text. The restore command uses ²np, where n is the register number. This is because there are nine registers inside Vi that are used to maintain the delete operation, which are represented by the numbers 1, 2, and 9, respectively, and they hold the contents previously deleted with the dd command. These registers form a queue, for example, the contents deleted by the last dd command are placed in register 1; the next time you delete the text content using the dd command, Vi will transfer the contents of register 1 to register 2, and register 1 will be deleted by the last dd command. By analogy, Vi can save content deleted with the last nine times with the dd command, while previous content deleted with the dd command is discarded.

In the last line mode, the contents of the file can also be deleted, but it can only delete the entire line, and all lines in a specified range (starting line number, ending line number) can be deleted at once. It should be noted that when deleting in this way, Vi does not put the deletion into the register, so when an erroneous deletion occurs, it cannot be restored with the ²np command, but can only be recovered with the u command.

Finally, I would like to mention how to delete the entered text in the text input mode. The user can use the key combination, and the cursor returns to the position where the insertion started, and the Vi is still in text input mode.

Cancel the previous command (Undo)

Canceling the previous command (Undo), also known as the undo command, is a very useful command that cancels the impact of the previous misoperation or inappropriate operation on the file and returns it to the state it was before the misoperation or inappropriate operation was performed.

There are two forms of canceling the previous command, typing the characters u and U in command mode. Their function is to cancel the command just entered and return to the original situation. The difference between lowercase u and uppercase U is that the function of the uppercase U command is to restore to the situation before the misoperation of the command, that is, if you use the U command after inserting the command, delete what has just been inserted.

If you delete the command and use the U command, it is tantamount to inserting what you just deleted at the cursor. Here all commands that modify the text are treated as insert commands. In other words, the U command can only cancel the previous operation, and if you undo the previous operation with the U command, when you press the U key again, instead of undoing the previous operation, you undo the operation performed by the U command just now, that is, you return to the state before the first use of the U command, and you end up doing nothing. The lowercase u command restores the current line to the state it was before it was edited, no matter how many times it has been edited.

Note: you can still use the cancel command again for the cancel command. This produces a "negative and positive" effect, and the file state returns to the state it was before the cancel command was executed for the first time, as if nothing had been done. For example, in the previous example, using the command U again, the screen will still display the inserted content.

Repeat command (Redo)

Repeating commands is also a very common command. In text editing, you often encounter the need to repeat some operations mechanically, and then you need to use repetitive commands. It allows the user to easily execute a complex command that has just been completed earlier.

Repeat commands can only work in command mode, where you press "." The key is fine. When you execute a repeated command, the result depends on the current position of the cursor.

Modification of text content

The modification of the text content means that in the process of editing, some characters and lines in the text can be modified, that is, the newly entered text can replace the old text that needs to be modified, which is equivalent to deleting the content that needs to be modified with the delete command. and then use the insert command to insert new content. So after using the modify command, vi enters the text input mode, and when you finish typing 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 anything entered at this time is considered to be the new content of the modification.

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

C command

C is followed by the cursor move command, limiting the scope of the modification from the current position of the cursor to the specified location. The range of modified text in command c is limited by both the cursor position and the cursor movement command.

C, c $command

The C, c $commands can modify content 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 the command C, it indicates that the content of the specified number of lines can be modified. For example, the 3C command means that the contents of the following two entire lines are deleted after the character where the cursor is located (note that it is not the whole line) and replaced by the subsequent input.

Cc command

The function of the cc command is the same as C, except that the scope of modification is different from C, which modifies content from the beginning of the line on which the cursor is located (no matter what column the cursor is in) to the end of the line or to specify the end of a line. All the contents of the line of the cursor are replaced by the newly entered content.

Like command C, you can also add the number n before cc to indicate that you want to modify a total of n lines from the current line of the cursor. For example, 5cc means to delete the line of the cursor and the four lines below it, and then enter what you want to modify.

Replacement of text

The replacement of text is to replace the existing text with the newly entered text. Like the text modification, it also performs the delete operation first and then the insert operation.

The replacement commands provided by Vi are substitution commands, substitution commands, and word substitution commands.

1. Replace commands: r and R

R: replace the character at the current cursor with a character entered later.

R: replace several characters from and after the current cursor with subsequent text, and replace the original character for each character entered until you press

< Esc >

Key to end this replacement. If the number of characters newly entered exceeds the original number of corresponding characters, the extra part will be appended to it.

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

It is worth mentioning that when using R to replace text content, the newly entered text can take up multiple lines, and only the corresponding characters of the line in which the cursor is located are overwritten.

Visible 6 newly entered characters (including spaces, but not including

< Esc >

Replaces 6 characters in the original line

two。 Replace commands: s and S

S (lowercase): this command replaces the character of the current cursor with subsequent text.

If you replace the cursor character with only one new character, the s command functions similarly to the r command, such as sh and rh, which change the cursor character to h. But there is also a difference 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. Therefore, the general way to use the s command is: s is followed by the body to be replaced, and finally, it must end the text input mode and return to command mode.

If you can add a number n before s, it means that the text entered after s replaces 1 character (n characters in total) starting from the character where the cursor is located and after it.

S (uppercase): this command replaces the current line (the whole line) of the cursor with the newly typed text, regardless of the column in which the cursor is located.

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

3. Word replacement cw

If we just want to replace the contents of a word with another text string, we can use the cw command. What cw replaces is a narrow word. After entering this command, Vi will delete the word at the cursor, and then the user can enter any text. Press the key after the input is complete, and Vi replaces everything from the original cursor position to the end of the corresponding word with the input.

Merging of lines of text

Vi provides commands to merge some lines in the text. This command is represented by J (capital letters), and its function is to merge the line of the cursor with the following line into one line.

If a number n, such as 3, is given before the J command, it merges the current line of the cursor and the two lines that follow it (three lines of total) into one line.

The movement of lines of text

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

The left and right movement of the line of text.

Commands for moving text lines left and right are >, >, and cursor movement commands. The range of moving text lines is limited by the line of the cursor and the cursor movement command that is subsequently typed.

(2)

< 命令将限定正文行向左移动。其使用方式与>

The command is the same, except that it moves in the opposite direction. For example, the > command moves the line of the cursor 8 spaces to the right. If a number n, such as 4, is given before the > > command, it means that the current line of the cursor and the three lines below it (4 lines of 4) have moved 8 spaces to the right.

(4) > the command is the same, but it moves in the opposite direction.

The transliteration of text lines

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

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

Press the ndd command. Where n is the number of rows to be moved. At this point, Vi will delete the lines of text to be moved from the file and put them into the No. 1 delete register.

Move the cursor to the destination line

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

At this point, the line of text to be moved appears at the destination.

Although the above method can realize the transliteration of text lines, it is obviously too cumbersome. Vi also provides another quick way to use the command m (Move command) in the last line mode. The method is n m k, which means to move the nth line below the k line.

Now if you want to move line 5 to line 4, type:

: 5 m 3

Before the command m, you can also specify a range of line numbers (starting line number and ending line number) to indicate that you want to move lines of text within the specified range to a specified location. For example:

: 2,5 m 0

Indicates that the contents of the second through fifth lines of the file are moved to the header of the file (below the zero line, the zero exercises a false line).

Line number in Vi

Many commands in Vi use values such as line number and number of lines. If you edit a large file, it is very inconvenient to count it yourself. For this reason, Vi provides the ability to add line numbers to text. These line numbers are displayed on the left side of the screen, while the contents of the corresponding lines are displayed after the line number.

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

: set number

It is important to note that the line numbers added here are only displayed to the user, and they are not part of the contents of the file.

In a larger file, you may need to know which line of the cursor is the current line, where you can use the key combination in command mode, and Vi will display the corresponding information on the last line of the display window. This command can be used at any time.

In the last line mode, we can type the command nu (the abbreviation of the word number) to get the line number and contents of the current line of the cursor.

Cursor movement operation

Cursor movement is undoubtedly the most frequently used operation in a full-screen text editor. Only by skillfully using these commands that move the cursor can the user quickly and accurately reach the desired location for editing.

Cursor movement in Vi can be in either command mode or text input mode, but in different ways.

In text input mode, you can move the cursor directly using the four arrow keys on the keyboard.

In command mode, there are many ways to move the cursor. You can not only use the four arrow keys to move the cursor, but also use the four keys h, j, k, l instead of the four direction keys to move the cursor, which can avoid the contradictions caused by different keyboard definitions on different machines. and after using proficiency, you can complete all operations without leaving the position of the letter keyboard, so as to improve work efficiency. You can also use, and four keys or combinations of keys to move the cursor. And the above three keys are equivalent in function. In addition, there are some commands to move the cursor. Here is a description of how they work:

, ®(all called right keys)

The function of the right arrow key is to move the cursor one position to the right. If you enter a number n before the right button, the cursor moves n positions to the right. For example, 5l indicates that the cursor moves 5 positions to the right. It is important to note that the cursor cannot move beyond the end of the current line. If the given n exceeds the number of characters from the current position of the cursor to the end of the line, if you use the right arrow key, the cursor can only be moved to the end of the line; if used, the cursor can only be moved to the appropriate position of the following line or lines.

H, (left Arrow)

Execute the left arrow key at a time, and the cursor moves one position to the left. Just like the right arrow key, you can enter a number n in front of the left key, and the cursor moves n positions to the left. It is important to note that if you use the left arrow key, the cursor cannot move left beyond the beginning of the line; if used, the cursor moves to the appropriate position on the above line or lines.

J, arrow (down arrow)

Move the down cursor one position at a time (that is, one row), but the column where the cursor is located remains the same. When these commands are preceded by the number n, the cursor moves down n lines.

In addition to using the down arrow 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 in this case the cursor will be at the first character of the line. For example:

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

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

K, ­(up Arrow)

Move the up arrow cursor up one position at a time (that is, one row), but the column where the cursor is located remains the same. Also precede these commands with the number n, and the cursor moves up n lines.

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

L (move to the beginning of the line)

The L command moves the cursor to the beginning of the current line, moving the cursor to the first non-blank place of the current line (non-tab or non-space).

$(move to the end of the line)

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

[line number] G (move to specified line)

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

The cursor moves 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, no matter how many screens the file has, it jumps to the last line

This is the end of this article on "how to use vi commands in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report