Vi and vim Editor (1): vi text editor, vivim
There are many editors in UNIX systems, which can be divided into two types: Row editor and full-screen editor. The row editor can only display one row of a file on the screen at a time, such as the ed and ex editors. The full-screen editor can display part of the file on the screen.
The vi (read as vee-eye) editor is a standard text editor for the UNIX_Like operating system. Vi is short for visual editor. The vi editor is a full-screen editor. You can modify the file while reading the file and immediately see the modified result. For beginners, the vi editor is still very difficult, but once you are familiar with the vi editor, you may not want to re-use other "simple" editors. When learning the vi editor, you should learn to hand over the editing work to the computer. This is the work of computers.
The vi editor has two modes: Command mode and insert mode. In command mode, everything entered is interpreted as the input command by the vi editor. In the insert mode, all input data is inserted into the editing file as input text. In the vi Editor, commands have the following features:
* The command is case-sensitive, that is, size-sensitive;
* When you enter a command, the command is not displayed on the screen;
* Enter is not required after the command is entered.
At the same time, a group of commands are displayed at the bottom of the screen, and there are special symbols before these commands, such as the colon (:) used to start all the ex commands. The bottom-layer editor used by the vi editor is the ex-line editor, and vi is only its visual mode. Therefore, you can use the ex editor command in the vi editor.
Since it is a text editor, you must first learn how to open and close files. You can use the vi editor to edit any text. Note that vi copies the file content to be edited to the buffer in the memory. Therefore, we only modify the copy of files in the memory, only by storing the file content in the buffer zone can the file editing result be updated to the file on the disk. The file content of the storage buffer is what we usually call "Writing Files" or "saving files ".
Open the file: vi [filename]
If filename is not specified, vi will open an unnamed buffer. When we write the content in the buffer to a file, we need to specify filename. If filename already exists, edit an existing file. If filename does not exist, this is equivalent to creating a new file (a file named filename is generated only when the created buffer is saved). When a new file is opened, the status bar at the bottom of the vi editor has a message ,:
Problems encountered when opening the file:
* You Should edit an existing File, but the vi editor displays "[New File]". The problem is that the file name you entered is incorrect.
* Messages such as [readonly] and "Permission denied" are displayed. This is because you do not have the write permission for the file or use the vi editor view mode (vi-R)
* When the q command is entered, the following warning is displayed:
This is because you have modified the file (You may not know where to modify it). Because the file has changed, the vi editor will definitely allow you to choose whether to save the editing result before leaving, therefore, you can use the ZZ command to save the Modification result and exit, or use q! Do not save the editing result and leave.
Save and exit files:
ZZ command: Save the editing result and leave the vi editor. You can also use the ex command to save the editing result. The w command only saves (writes) the editing result without leaving the editor. : The wq command saves the editing result and leaves the vi Editor (: wq is the same as the ZZ command)
End Without saving the editing result:
: E! Command: This command can delete all the editing results and directly return to the file content during the last storage, that is, it can be "started from scratch ". : Q! The command directly ignores the current editing result, returns to the file content during the last storage, and leaves the vi editor. Why are there exclamation points behind these two commands, because the e and q commands will make the current editing effect disappear, and the vi editor generally won't let you discard the editing result, therefore, after adding an exclamation point, you can make vi ignore this ban and directly discard the current editing result to return to the file content in the last storage.
Problems Occurred When saving the file:
* After writing for half a day, no write permission is found. You can use w filename to write the current result to a new file (because a new file is created in the directory, therefore, you need to write the file to a directory with write permission for you)
* Try to write the file, but find that the file system is full. There are many ways to solve this problem. On the one hand, you can write the file to a file system with the remaining space, and on the other hand, you can directly enter :! Sh returns to the shell environment to delete some large files in the current file system. In the ex command, you can use the exclamation point followed by the UNIX Command to directly execute the corresponding UNIX Command. As shown in, enter :! Pwd displays the current directory:
After the command is entered, the vi editor is temporarily exited and the command execution result is displayed:
Press enter to return to the vi editor.
Summary of the vi editor commands in this chapter:
ZZ,: w,: q,: wq,: q !, : E! .
Learn about a pattern matching problem in vi and vim editor.
If it is a regular expression, because. can match any character, the difference is that the first one must match the character before the comma, and the second one does not.
Therefore, if the comma in a row is at the beginning of a row, the first and second do not match.
However, here: the expression is followed by the first/after g, and SYNTAX is used here. : The general syntax of the g command is: g/pattern/cmd.
The subsequent command should be the move command. The general syntax of the command here is x. The y move z expression moves the text between the address range x and y to the z address. When the address is specified, the current row is indicated. This is the row where SYNTAX is located.
Another method for specifying an address is to use the regular expression. /DESCRIPTION/-1 indicates matching the first line of the string.
This command is complicated. As mentioned above, the command/is written as follows:
: G = SYNTAX =.,/DESCRIPTION/-1 move/PARMETER/-1
It can slightly improve readability. It tells the reader that = is the separator of g, And/belongs to the move command.
How to learn the Chinese version of VI and VIM editors
A good book. You can search for pdf in English. Although it is English, the words in it are simple, and the author wrote very well. I really recommend it! Don't be afraid of English... try it.