Linux file editing command vi

  • 2020-06-15 10:57:13
  • OfStack

I just contacted Linux. A few days ago, I applied for a free aliyun server. I chose Ubuntu system.

vi command to edit the file, Baidu 1, many answers are not very comprehensive, so edit the file for a while.

Here the blogger sorted out 1, share to everyone.

1. The vi editor has three basic working modes

The first thing you need to know about the vi editor is that there are three basic modes of operation: command mode, text input mode, and end-line mode.

Command line mode: This mode is the default mode when entering the vi editor. Whenever the user is in any mode, press the ESC key to enter command mode. In this mode, users can enter the vi command and manage their own documents. Any character entered from the keyboard is interpreted as an edit command. If the character entered is a valid vi command, vi completes the action after accepting the user command. Note, however, that the commands entered are not echoed on the screen. If the character entered is not the vi command, vi will ring an alarm.

Text input mode: Enter the command mode by entering the command i, additional command a, open command o, modify command c, substitute command r or substitute command s. In this mode, any character entered by the user is protected by vi as the contents of a file and displayed on the screen. To return to command mode during text input, press the ESC key.

3: Last line mode: Last line mode is also known as ex escape mode. In command mode, the user presses the ":" key to enter the last line mode, and vi displays a ":" as the specifier in the last line of the display window (usually the last line of the screen), waiting for the user to enter the command. Most file management commands are executed in this mode (such as writing the contents of the edit buffer to a file). vi automatically returns to command mode after the final command is executed. To switch from command mode to edit mode, type a or i. If you need to return from text mode, press ESC. Type ":" in command mode to switch to the last line, and then type the command.

To sum up, 1 normally when we open a file using a command, we enter command mode. In command mode, you can switch to the text input mode and the final line mode, but the text input mode and the final line mode can not directly switch between each other, so the text input mode to the final line mode, need to go back to the command mode before switching, and vice versa.

In addition, editing text can be done in text input mode, on the keyboard, or in command mode using the vi command.

Examples demonstrate

First we use the command vi filename to open a file, and then we enter command mode

Next, press i, and then type whatever you want on the keyboard.

Then press ESC to re-enter command mode.

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

Let's type wq! , press enter, force save and exit.

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

Supplement:

(1) q! q "exit and save" wq "exit and save can also be added!

(2) If you do not want to save the exit directly, you can use the "ctrl+z" shortcut in command mode or hold down the "shift" key and type two z to exit.

More orders

Command to enter vi

vi filename: Open or create a new file and place the cursor at the beginning of line 1
vi +n filename: Open the file and place the cursor at the beginning of n 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 that matches pattern
vi-r filename: Restore filename after last system crash while editing vi
vi filename... .filename: Open multiple files and edit them one by one

Screen scroll class command

Ctrl+u: Scroll to the first half screen of the file
Ctrl+d: Flip to the end of the file half screen
Ctrl+f: Scroll to end of file 1 screen
Ctrl + b; First scroll to file 1 screen
nz: Rolls the n line to the top of the screen and the current line to the top of the screen if n is not specified.

Insert text class commands

i: In front of the cursor
I: At the beginning of the current line
a: Behind the cursor
A: at the end of the current line
o: Open a new row below the current row
O: Open a new row above the current row
r: Replaces the current character
R: Replaces the current and subsequent characters until ESC is pressed
s: Replaces a specified number of characters with the text entered, starting at the current cursor position
S: Removes the specified number of lines and replaces them with the text entered
ncw or nCW: Modifies the specified number of words
nCC: Modifies the specified number of rows

The delete command

ndw or ndW: Delete n-1 words at the beginning of the cursor and after
do: to the beginning of the line
d$: to end of line
ndd: Delete the current line and es185EN-1 line after it
x or X: removes 1 character, x after removing the cursor, and X before removing the cursor
Ctrl+u: Deletes text entered under input mode

Search and replace commands

pattern: Search pattern from the beginning of the cursor to the end of the file
The & # 63; pattern: Search pattern from the start of the cursor to the top of the file
n: Repeat the search command 1 time in the same direction
N: Repeat the previous search command in the opposite direction
: s/p1 / p2 / g: will all the p1 p2 alternative in the current line
: n1 n2s/p1 / p2 / g: will be the first n1 to n2 line all the p1 p2 instead
: g/p1 / s / / p2 / g: will file all the p1 p2 replacement

Option is set

all: Lists all option Settings
term: Set the terminal type
ignorance: Ignore case in the search
list: Display TAB (Ctrl+I) and end-line flag ($)
number: Display the line number
report: Displays the number of changes made by line-oriented commands
terse: Displays a short warning message
warn: Displays NO write information if the current file is not saved when moving to another file
nomagic: Allows special characters not preceded by \ in search mode
nowrapscan: vi is prohibited from starting at the other end of the file when the search reaches both ends
mesg: Allows vi to display messages written by other users to their terminals using write

Last line mode command

: n1,n2 co n3: Copy the contents between n1 and n2 under n3
: n1,n2 m n3: Move the contents between n1 and n2 to below n3
: n1,n2 d: Delete the contents between lines n1 and n2
: w: Save the current file
e filename: Open the file filename for editing
: x: Save the current file and exit
: q: Exit vi
: q! : Do not save the file and exit vi
:! command: Executes the shell command command
: n1 n2 w! command: The contents of the file from n1 to n2 are executed as input to command. If n1 and n2 are not specified, the entire file content is used as input to command
: r! command: Puts the output of the command command on the current line

Register operation

"The & # 63; nyy: Saves the contents of the current line and the n line below to register? Where ? Is 1 letter, n is 1 number
"The & # 63; nyw: Save the current line and n words to register? Where ? Is 1 letter, n is 1 number
"The & # 63; nyl: Save the current line and the n characters below to register? Where ? Is 1 letter, n is 1 number
"The & # 63; p: Take out register? And place it at the cursor position. Here? It could be a letter, it could be a number
ndd: Deletes the text of the current line and the n line under it, and places the deleted contents in delete Register 1.


Related articles: