Linux basic command summary --vi editor

  

Speaking of the command, you have to mention this special command "vi" it is not only a command, but also a Linux editor. Due to the time relationship, here is not an example, and some practical applications of vi will be introduced when configuring network services in the future.

The vi command is full-screen text editing under linux, and the vi editor provides rich editing functions. This editor is very rudimentary and very unfriendly to beginners. However, the vi editor in Linux is very useful, especially in the future when we configure various servers to modify the configuration file is very useful.
The vi editor has three modes: command mode, input mode, and last line mode. It is very important to master these three modes:

Command mode: After vi starts, the default is to enter the command mode. From this mode, you can switch to the other two modes by using the command, and you can click on it in any mode. ,null,null,3],The Esc] key can return to the command mode. Enter the subtitle "i" in the command mode to enter the input mode editing file of vi.

input mode: In this mode, we can edit, modify, input and other editorial work, the last line shows a "--INSERT--" marks vi into the input mode in the editor. When we finish modifying the input and other operations, we need to save the file. In this case, we need to return to the command mode and save it in the last line mode.

Last line mode: Enter ":" in the command mode to enter this mode. There are many useful commands in the last line mode.

1.vi start and exit

Directly enter the command vi does not specify the file name, because this is an empty file without name, showing the version information of vi.


vim is an improved version of the vi editor, which extends a number of useful features based on the vi editor. Most linux/unix versions use vim instead of the original vi editor. Or use vi to open the file directly. If the file exists, open the existing file; if it does not exist, we will use the parameter we specified as the file name.

Enter q! It is forced to exit without saving the current file contents. If the file has not been modified, or if it has been saved, you can use q to quit. Save and exit using wq, save forced to exit wq!

2. Save the file

If the file already has a file name, we enter w stored directly in the last line mode, if no file name, type w filename to save. For example: w test where test is the file name.

then vi can also be saved as, after saving is finished, we'll use w as a file name.

3. Cursor movement

In the command mode, you can use the command to perform the following operations.

In the movement of the cursor direction, in addition to the direction keys, you can also use the following command
to move the cursor up: k
Move the cursor to the left: h Move the cursor to the right: l
Down Move the cursor: j
Page turning command
Ctrl+F to turn the page forward Ctrl+U to turn forward half page
Ctrl+B to turn the page backwards Ctrl+D to turn backwards half page
Intra-line fast jump
^ Move to the beginning of the line
$ Move to the end of the line
Display line number and cancel line number (last line mode use)
set nu display line number
set nonu cancel line number
In command mode, use the following command to quickly jump between lines
1G jump to the first line of the file
G jump to the end line
#G Jump to file #row
4.Edit operation
Enter input mode command
i insert command a additional command o open command c modify command
r replace command s replace command Esc exit command
Input mode operation
Home cursor to Line head
End cursor to the end of the line
Page Up and Page Down page up and down
Delect deletes the character at the cursor position
Delete operation (command mode use)
x deletes a single character at the cursor
dd delete the line where the cursor is located
dw delete the current character to the end of the word including all the characters of the space
#x For example, 3x delete the three characters to the right at the cursor
#dd, for example, 3dd from the current line Delete three lines of text downward
Undo operation
u command cancel the last operation, you can use multiple times to restore the original operation
U cancel all operations
Ctrl+R can restore the use of u Operation of the command
Copy operation
The yy command copies the contents of the current entire line to the vi buffer.
yw copies the current cursor position to the end of the word to the vi buffer, which is equivalent to copying a word
y$Copy the position of the cursor to the end of the line to the buffer area
y^Copy the position of the cursor to the beginning of the line to the buffer area
#yy For example: 5yy is to copy 5 lines
#ywexample: 2yw Is copying Words
If you want to copy the contents between the mth line and the nth line, you can enter m in the last line mode, for example: 3, 5y copy the contents of the third line to the fifth line to the buffer area
5 Finding and replacing
vi's find and replace functions are mainly done in the last line mode:
top-down lookup
/the character to be searched, where /represents the search from the cursor position, for example :/work
Bottom-up search
? The character to be looked up, for example: /work
Replace
:s/old/new Replace the first occurrence of the old
in the line with new: s/old/new/g Replace all occurrences in the line with new Old
:#,# s/old/new/g Replace the old
from the ## line to the ## line with new: % s/old/new/g replace the whole one with new Old
If the range of substitution is large, add a c command at the end of all commands, forcing each replacement to require user confirmation, for example: s/old/new/c or s/old/new/gc
6Recovering files
When editing a file, vi generates a temporary file that starts with . and ends with .swp. The file is automatically deleted when it exits normally. If it is unexpectedly exited, for example, the power is suddenly turned off, the file will not be deleted. We can select the command processing in the next edit:
O read-only open, do not change the file content
E continue editing File, does not restore the contents of the .swp file saved
R will restore the contents of the file after the last edit is not saved
Q exit vi
D delete the .swp file
or use the vi -r file name to recover Unsaved content
The vi command generally introduces you to this. In fact, its function is very powerful. We constantly accumulate the use of commands in the daily use process. I believe that everyone will master this command very quickly.

Copyright © Windows knowledge All Rights Reserved