Linux VI command details, linuxvi details

Source: Internet
Author: User

Linux VI command details, linuxvi details

Vi is short for "Visual Interface", and its position on Linux is as if the Edit Program is on DOS. It can perform output, delete, search, replace, block operations, and many other text operations, and users can customize it according to their own needs, which is not available in other editing programs.

Vi is not a typographical program. Unlike Word or WPS, it can orchestrate fonts, formats, paragraphs, and other attributes. It is just a text editing program.

Vi has no menu, only commands, and many commands. This article only describes common commands.

Vi has three basic working modes: command line mode, text input mode, and last line mode.

Command Line Mode:

At any time, regardless of the user's mode, you just need to press the "ESC" key to enable the vi to enter the command line mode. When you enter the vi command to start the vi editor in the shell environment, it is also in this mode.

In this mode, you can enter various valid vi commands to manage your own documents. In this case, any character entered on the keyboard is interpreted as an editing command. If the entered character is a valid vi command, then vi completes the corresponding action after receiving the USER command (but note that the entered command is not displayed on the screen ). If the entered character is not a valid command of vi, vi will ring an alarm.

Text input mode:

In command mode, Enter command I, attach command a, open command o, modify command c, replace command r, or replace command s to enter the text input mode. In this mode, any character entered by the user is saved as the file content by vi and displayed on the screen. During text input, if you want to return to command mode, press "ESC.

Last line mode:

In command mode, you can press the ":" key to enter the last line mode. At this time, Vi will display a ": as the last line prompt, waiting for the user to enter the command. Most file management commands are executed in this mode (such as writing the content in the editing buffer to a file in Medium Size). After the last command is executed, vi automatically returns to the command mode.

If you have changed your mind when entering the command in the last line mode, you can press the "ESC" key or use the return key to delete all input commands, and then press the return key, the vi can be returned to the command mode.

Vi Entry and Exit

In shell mode, Type vi and the file name to be edited to enter vi. For example:

Vi example.txt

You can edit the example.txt file. if the file exists, the content of the file is displayed on the editing page, and the cursor is positioned on the first line of the file. If the file does not exist, no content exists on the editing page. If you need to place the cursor in the nth line of the file after entering the vi editing interface, add the "+ n" parameter after the vi command. For example, if the following command is used to display the result from line 1 of the example.txt file, run the following command:

Vi + 5 example.txt

When exiting vi, enter the exit command "q" in the last line mode ". In text input mode, press the "ESC" key to enter the command mode, and then enter ":" to enter the last line mode. In the last line mode, run the following command to exit:

: Q exits directly. If you modify the document content in text input mode, you cannot exit.

: Wq is saved and then exited.

: X is the same as "wq ".

: Q! -Force exit if the content is not saved.

Show row number in vi

In the last line mode, enter the following command.

Set number

Allows vi to display the row number on the editing page.

In the last line mode, you can use the following "nu" command (abbreviated as number) to display the row number of the cursor and the content of the row.

Move cursor

In the full-screen text editor, moving the cursor is undoubtedly the most frequently used operation. Users can quickly and accurately edit the cursor at the desired position only by skillfully using these commands.

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

In text input mode, you can use the four arrow keys on the keyboard to move the cursor. In command mode, there are many ways to move the cursor. Not only can we use four direction keys to move the cursor, but we can also use the h, j, k, and l keys to move the cursor instead, this can avoid conflicts caused by different keyboard definitions on different machines, and you can complete all operations without leaving the letter and keyboard location after you are proficient, thus improving work efficiency.

The following commands move the cursor in command line mode:

H move the cursor to the left. If you enter the number n before the h command, move the cursor to the left n characters;

L move the cursor right. If you enter the number n before pressing the l command, the cursor moves n characters right;

J. move the cursor up. If you enter the number n before the j command, move the cursor up to n characters;

K: move the cursor down. If you enter the number n before the k Command, the cursor xia moves n characters;

0 (0) move the cursor to the beginning of the line;

$ Move the cursor to the end of the line;

H move the cursor to the first line shown on the screen (not necessarily the file header );

L move the cursor to the last line shown on the screen (not necessarily the end Of the file );

M move the cursor to the middle line of the screen;

NG cursor moves to line n;

W or W move the cursor to the beginning of the next word;

E or E if the start position of the cursor is inside the word (that is, not the end of the word), the command moves the cursor to the end of the word; if the start position of the cursor is at the end of the word, this command moves the cursor to the end of the next word.

B or B if the cursor is in the word (not the first word), the command moves the cursor to the first word of the word; if the cursor is in the first word of the word, this command moves the cursor to the beginning of a word;

Ctrl + g status Command to display the status of the currently edited document. Including the file name being edited, whether it has been modified, the current row number, the number of lines in the file, and the percentage of rows before the cursor in the entire file.

In addition, you can use the following command in the last line mode to complete the cursor in the entire file

Mobile Operation:

: N move the cursor to the nth line of the file;

: $ Move the cursor to the last row of the file;

Text insertion

In command mode, any character entered by the user is interpreted and executed by vi as a command. If the user wants to treat the entered character as text content, switch the vi operating mode from command mode to text input mode. The following command is used for switching:

1. Insert command

Vi provides two insert commands: I and I.

(1) The. I command inserts text starting from the cursor's position, and you can use the key to delete incorrect input during the insertion process. At this time, the vi is inserted, and the "-- INSERT --" INSERT is displayed at the bottom of the screen.

(2). I command this command is to move the cursor to the beginning of the current line, and then insert text before it.

2. append Command

Vi provides two additional insert commands: a and.

(1 ). command a: This command is used to append new text after the current position of the cursor. After the new input text is placed after the cursor, the original text after the cursor is moved backward accordingly, the cursor can be anywhere in a row.

(2 ). the A command is different from the command. The A command moves the cursor to the end of the row where the cursor is located and inserts new text from there. When the command is entered, it is automatically moved to the end of the row.

Commands a and A are the only way to insert text to the end of a line.

3. open the open command

Whether it is an Insert command or an append Command, the inserted content starts from a location in the current row. If you want to insert new rows before or after a row, use the open command.

Vi provides two open commands: o and O.

(1). o command this command will open a new line under the row where the cursor is located, and place the cursor at the beginning of the line, waiting for the input text. Note that only characters after the start of the insert mode can be deleted when the delete character is used, which does not work for previous characters.

(2) The. O command is opposite to the o command. The O command inserts a line above the line where the cursor is located, places the cursor at the beginning of the line, and waits for the input text.

Text Modification

In command mode, you can use various commands provided by vi to modify the text, including deleting, copying, replacing, and replacing the text content.

1. delete/move text

When editing text, you often need to delete unnecessary text. You can use a key to delete incorrect or unnecessary text. However, there is a limit that when you delete the text to the line header, it is impossible to delete the content of the above line.

In command mode, vi provides many delete commands. Most of them begin with d. Commonly used:

(1). delete a single character

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

X deletes the character before the cursor. If n is added before X, the n characters starting from the character before the cursor to the left are deleted.

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

(2). Delete multiple characters

Dd deletes the entire row where the cursor is located. Add a number n before dd to delete the content of the current row and the next n-1 row.

The functions of the D or d $ commands are the same. They delete the content starting from the cursor to the end of the line.

D0 deletes the content starting from the first character of the cursor to the beginning of the line.

Dw deletes a word. If the cursor is in the middle of a word, it is deleted from the cursor position to the end of the word. Like the dd command, you can add a number n before dw. Deletes n specified words.

If the user accidentally deletes the file by mistake, it does not matter vi. The command for restoring the misoperation is provided, and the recovered content can be moved to any part of the text. The recovery command uses "np", where n is the number of times to be restored. For example, if you use the dd command to delete a line of content and then use the "2 P" command, the deleted content will be inserted twice.

By combining the dd and p commands, you can easily move text lines.

2. Text Copy command

Yy copies the entire row where the cursor is located. Add a number n before yy to copy the content of the current row and the next n-1 row.

After copying a text line, you can paste the text line anywhere by using the "p" command described above.

Dd, yy, and p commands are used in vi. They are similar to the "cut", "copy", and "Paste" commands of the graph editing program in Windows.

3. Cancel the previous command (Undo)

The Undo command, also called the restore command, is a very useful command that can cancel the impact of previous misoperations or inappropriate operations on files, it is returned to the status before this misoperation or inappropriate operation is executed.

There are two methods to cancel the previous command. In command mode, the function of typing the characters u and U is to cancel the command just entered and restore to the original situation. The lower-case u and upper-case U have different details. The difference between the two is that the upper-case U command is used to restore the case before the misoperation command, that is, if you use the U command after the command is inserted, delete the content you just inserted. If you use the U command after the DELETE command, it is equivalent to inserting the content you just deleted at the cursor. All commands for modifying text are considered as insert commands, that is, the U command can only cancel the previous step. If the U command is used to cancel the previous step, when you press the U key again, it is not to undo the previous step, but to undo the operation just executed by the U command, that is, to restore to the State before the first use of the U command, the result is nothing. The lower-case u command restores the current row to the status before editing, regardless of the number of times this row is edited.

4. Redo)

Repeated commands are also very common commands. In text editing, repeated commands are often required. It allows you to easily execute a complex command just completed.

Repeated commands can only work in command mode. In this mode, press the "." key. When executing a duplicate command, the result depends on the current position of the cursor.

Text Search and replacement operations

During text editing, you can search for or locate a part of a specific word or word. The vi editor allows you to search for the specified mode either forward or backward.

"/" Is the mode SEARCH Command of the vi editor. After you type "/", a line will appear at the bottom of the screen, and a diagonal slice will appear at the beginning of the line at the bottom, and the cursor will be placed after the diagonal slice. In this case, you can enter the search mode at the cursor position. After entering and pressing ENTER, the editor starts searching for the mode you typed at the end of the file from where the cursor is located. If the input mode is found, the cursor stays in the found mode. After finding a matching mode, if you need to search for the next matching mode, run the "n" command. If you need to find the previous matching mode, use the "N" command.

In addition, "?" The command is also a mode SEARCH Command. The difference with "/" is that/is from the cursor when

In front of the file, search for the end of the file, and "?" The command searches the file header at the current position of the cursor.

The text replacement operation requires the last line mode. That is, all text replacement operation commands start.

The format of the text replacement command is:

: S/text1/text2 is used to replace the first "text1" found in the section where the cursor is located with "text2 ";

: S/text1/text2/g is used to replace all "text1" in the section where the cursor is located with "text2 ";

: M, ns/text1/text2/g is used to replace all searched "text1" from m row to n row with text2. Here, you can use $ to represent the last line, that is, "1, $" to replace all matching characters in the document.


How to use linux vi commands

Basic concepts of vi
Basically, vi can be divided into Command mode, Insert mode, and Last line mode ),
The functions of each mode are as follows:
1. Comand mode: controls the movement of the screen cursor, the deletion of characters or the cursor, the replication of a specific segment, the entry into the Insert mode, or the Last line mode.
2. Insert mode: only in Insert mode can text data be input. Press Esc to return to Comand mode.
3. Last line mode: Save the file or leave the editor. You can also set the editing environment, such as searching strings and listing row numbers.
However, you can simplify vi into two modes, that is, the Last line mode is also counted into Command mode, and vi is divided into Command and Insert mode.
Basic operations of vi
? Go to vi
After the system prompts you to enter the vi and file name, you can enter the vi full screen editing screen:
$ Vi testfile
Note that after entering vi, you are in "Command mode". You must switch to "Insert mode" to enter the text.
Users who use vi for the first time will want to move the cursor first with the upper and lower left keys. As a result, the computer keeps calling, so do not tamper with yourself after entering vi,
Convert the data into Insert .? Switch to Insert mode to edit the file. Press 'I', 'A', or 'O' in Command mode to enter Insert mode.
At this time, you can start to enter the text.
I: insert. Insert the entered text from the current cursor.
A: Add. Input text starts from the next word where the cursor is currently located.
O: Insert a new row and enter text from the beginning of the line.
? Insert switchover → Command mode, Press Esc
If you are currently in Insert mode, you can only keep typing. If you find a typo, you can use the mouse button to move it back and delete it,
Press ESC to switch back to Command mode, and then delete the text.
? Exit vi and save files
In Command mode, you can press the colon ":" to enter the Last line mode. For example:
: W filename (enter "w filename" to save the article to the specified filename)
: Wq (enter "wq". Because the file name testfile has been specified at the time of entry, it will write testfile and leave vi)
: Q! (Enter "q !", Force exit and discard edited files)
Command mode function key list
When introducing the command mode command, the command is followed by a function key with the word "commonly used" to indicate commonly used vi commands,
Please be sure to learn and remember.
(1) switch I, a, and o to Insert mode. [Super common]
(2) move the cursor
Vi can be moved up, down, left, right, and left directly using the optical key on the keyboard, but the regular vi is a lowercase English letter.
H, j, k, and l, respectively, control the left, down, up, and right shifting of the cursor.
Press Ctrl + B: Move the screen back one page. [Common]
Press Ctrl + F: Move the screen one page forward. [Common]
Press Ctrl + U: Move the screen back half a page.
Press Ctrl + D: Move the screen half page forward.
Press 0 (number zero): to move the beginning of an article. [Common]
Press G to move to the end of the article. [Common]
Press w: the cursor to jump to the beginning of the next word. [Common]
Press e: move the cursor to the end of the next word.
Press B: The cursor returns to the beginning of the previous word.
By $: Move to the end of the row where the cursor is located. [Common]
Press ^: to move to the first non-blank character of the row.
Press 0: Shift... the remaining full text>

In linux, how does one use the copy command of vi?

"Yw": copy the character at the end of the cursor to the buffer zone.
# Yw: Copy # words to the buffer zone
"Yy": copy the row where the cursor is located to the buffer zone.
"# Yy": for example, "6yy" indicates copying 6 lines of text from the row where the cursor is located "down.
P: place the characters in the buffer to the cursor position. Note: All copy commands related to "y" must work with "p" to complete the copy and paste function.
 

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.