Home > Operation and Maintenance > Linux Operation and Maintenance > Summary and sharing of vi in ​​Linux commands (collection)

Summary and sharing of vi in ​​Linux commands (collection)

黄舟
Release: 2017-07-24 15:02:51
Original
2070 people have browsed it

This article mainly introduces the detailed arrangement (summary) of Linux file editing commands. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

I’m new to Linux. I applied for a free trial Alibaba Cloud server a few days ago. I chose the Ubuntu system. When configuring the jdk environment variables, I need to edit the file.

vi command to edit files, I searched on Baidu, and many of the answers were not very comprehensive, so it took some time to edit the file.

The blogger here has sorted it out and shared it with everyone.

1. The vi editor has 3 basic working modes

First of all, you need to know that the vi editor has 3 basic working modes, namely : Command mode, text input mode, and last line mode.

First: Command line mode: This mode is the default mode after entering the vi editor. At any time, no matter what mode the user is in, pressing the ESC key can enter command mode. In this mode, users can enter vi commands and users manage their own documents. Any characters entered from the keyboard at this time are interpreted as editing commands. If the entered characters are legal vi commands, vi will complete the corresponding action after accepting the user command. However, it should be noted that the entered commands are not echoed on the screen. If the characters entered are not vi commands, vi will ring the alarm.

Second: Text input mode: In the command mode, enter the command i, append the command a, open the command o, modify the command c, replace the command r or replace the command s to enter the text input mode. In this mode, any characters entered by the user are protected by vi as file content and displayed on the screen. During text input, if you want to return to command mode, just press the ESC key.

Third: Last line mode: Last line mode is also called ex escape mode. In command mode, the user presses the ":" key to enter the last line mode. At this time, vi will display a ":" on the last line of the display window (usually the last line of the screen) as the specifier of the last line mode. Wait for the user to enter a command. Most file management commands are executed in this mode (such as writing the contents of the edit buffer to a file, etc.). After the last command is executed, vi automatically returns to command mode. If you want to switch from command mode to edit mode, you can type a or i. If you need to return from text mode, just press ESC. Enter ":" in command mode to switch to last line mode, and then enter the command.

To sum up, generally when we use a command to open a file, we enter the command mode. In command mode, you can switch to text input mode and last line mode, but text input mode and last line mode cannot be switched directly to each other. Therefore, to switch text input mode to last line mode, you need to return to command mode first. Switch again and vice versa.

In addition, you can edit text in text input mode by keyboard input, or use the vi command in command mode to achieve the editing effect.

Example demonstration

First we use the command vi filename to open a file. At this time we enter the command mode

Next we Press i, then enter whatever you want on the keyboard.

Then press ESC to re-enter command mode.

In the command mode, we press: to enter the last line mode.

We enter wq!, then press Enter to force save and exit.

Next time we open the corresponding file (which can be opened with the less filename command), we will see that the content has changed.

Supplement:

(1) In the last line mode: q! [Force to exit without saving] q[Exit without saving] wq[Exit and save, you can also add one later! ]

(2) If you don’t want to save and exit directly, you can use the “ctrl+z” shortcut key in command mode or hold down the “shift” key and enter two z’s to exit.

More commands

Commands to enter vi

##vi filename: Open or create a new file, and Place the cursor at the beginning of the first line

vi +n filename: Open the file and place the cursor at the beginning of the nth line
vi + filename: Open the file and place the cursor at the beginning of the last line
vi + /pattern filename: Open the file and place the cursor at the first string matching pattern
vi -r filename: The system crashed when editing with vi last time, restore filename
vi filename…. filename: Open multiple files and edit them in sequence

Screen scrolling commands
##Ctrl+u: Scroll half-screen to the beginning of the file

Ctrl+ d: Scroll half a screen to the end of the file

Ctrl+f: Scroll one screen to the end of the file
Ctrl+b; Scroll one screen to the beginning of the file
nz: Scroll the nth line to the top of the screen, when n is not specified Scrolls the current line to the top of the screen.

Insert text command


i: Before the cursor
I: At the beginning of the current line
a: After the cursor
A: At the end of the current line
o: Open a new line below the current line
O : Open a new line above the current line
r: Replace the current character
R: Replace the current character and the following characters until the ESC key is pressed
s: Start from the current cursor position and enter the Text replaces the specified number of characters
S: Delete the specified number of lines and replace them with the entered text
ncw or nCW: Modify the specified number of characters
nCC: Modify the specified number of lines

Delete command

ndw or ndW: delete the n-1 words starting at the cursor and following it
do: delete to the beginning of the line
d$ : Delete to the end of the line
ndd: Delete the current line and n-1 lines after it
x or X: Delete one character, x deletes the one after the cursor, and X deletes the one before the cursor
Ctrl+u: Delete the text entered in the input mode

Search and replace command

/pattern: Search for pattern from the beginning of the cursor to the end of the file
?pattern : Search for pattern from the beginning of the cursor to the beginning of the file
n: Repeat the last search command in the same direction
N: Repeat the last search command in the opposite direction
: s/p1/p2/g: Move the current Replace all p1 in the row with p2
: n1,n2s/p1/p2/g: Replace all p1 in the n1 to n2 row with p2
: g/p1/s//p2/g: Replace all p1 in the file with p2

Option settings

all: List all option settings
term: Set the terminal type
ignorance: Ignore case in searches
list: Show tab stops (Ctrl+I) and end-of-line flags ($)
number: Show line numbers
report: Show modifications by line-oriented commands Number of passes
terse: Display a brief warning message
warn: Display NO write message if the current file is not saved when switching to another file
nomagic: Allow in search mode, use without preceding The special character "\"
nowrapscan: prohibit vi from searching from both ends of the file and start from the other end
mesg: allow vi to display information written by other users to their own terminals using write

Last line mode command

:n1,n2 co n3:Copy the content between line n1 to line n2 to line n3
:n1,n2 m n3: Move the content between lines n1 to n2 to the n3th line
: n1,n2 d: Delete the content between lines n1 to n2
: w: Save the current file
: e filename: Open file filename for editing
: x: Save the current file and exit
: q: Exit vi
: q!: Do not save the file and exit vi
: !command: Execute the shell command command
: n1, n2 w!command: Use the contents of lines n1 to n2 in the file as the input of the command and execute it. If n1 and n2 are not specified, it means that the entire file content will be used as the input of the command.
: r!command: Put the output of the command command into the current line

Register operation

"?nyy: Put the current line and its Save the contents of the next n lines to the register? , where ? is a letter, n is a number
"?nyw: Save the current line and the next n words to the register?, where ? is a letter, n is a number
"?nyl : Save the current line and its next n characters to a register? , where ? is a letter and n is a number
"?p: Take out the contents of the register? and place it at the cursor position. Here? can be a letter or a number
ndd : Delete the current line and n lines of text below it, and put the deleted content into the deletion register No. 1

.

The above is the detailed content of Summary and sharing of vi in ​​Linux commands (collection). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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