Configuration file is one of the distinctive features of the Linux operating system. Its function is somewhat similar to the registry in the Windows operating system, except that the registry is managed centrally. Configuration files adopt a decentralized and free management method.
vi is a powerful full-screen text editing tool. It has always been the default text editor for UNIX-like operating systems. vim is a vi text editor. An enhanced version of the vi editor (referred to as vi editor), which extends many practical functions based on the vi editor. However, vim is also commonly called vi. For ease of use, you can set a command alias to point vi to the vim program. (This article takes the vim program as an example) The specific operations are as follows:
[root@localhost ~]# alias vi='/usr/bin/vim'
[root@localhost ~]# which -a vi vim
vi editor is a full-screen editor that works in a character terminal environment, so the editing interface is relatively simple and does not provide users with a mouse. Instead of operating and menu system, corresponding editing and operating functions are realized through key commands. Three different working modes can be used in the v editing interface, namely command mode, input mode and last line mode. In different modes, you can edit The operations performed on the files are also different.
Command mode: After starting the vi editor, it enters the command mode by default. In this mode, you can perform cursor movement, string search, delete, copy, paste file content and other related operations. The main operation is the input mode for entering file content. You can modify the text file body or add new content. In the input mode of the vi editor, the last line will display the status prompt message "--INSERT--". In the last line mode, you can configure the vi editor environment, save files, exit the editor, and perform operations such as searching and replacing file contents. When in the last line mode, a colon ":" prompt will appear on the last line of the vi editor.
In the vi editing environment, there are three states, namely command mode, input mode and last line mode. You can switch between these states by pressing different keys. For example, from the command mode, press: (colon) key to enter the last line mode, press a, i, o and other keys to enter the input mode: In both the input mode and the last line mode, you can press the Esc key to return to the command mode, as shown in the figure .
After understanding the different editing modes (states) of the vi editor, the following introduces the common operation methods in command mode and last line mode (input mode is used for input Text content, no special introduction).
By executing a separate "vi" command, you can enter the command mode of the v editor and view relevant version information, as shown in the figure below. But the more common usage is to specify the file name to be edited as a parameter. If the file does not exist, vi will open a new empty file based on the file name.
When learning the basic operations of the vi editor, it is recommended to copy a system configuration file with more content for practice instead of modifying the system file directly to avoid mistakes. system error.
For example, the following operation copies the system configuration file /etc/yum.conf to the vitest.file file in the current directory, and then uses the vi editor to open the vitest.file file for editing.
[root@localhost ~]# cp /etc/yum.conf ./vitest.file [root@localhost ~]# vi vitest.file
In the command mode of the vi editor, you can enter specific keys (called vi operation commands, please note that they are different from Linux operating system commands) to perform operations, including mode switching, cursor movement, copying, deleting, For operations such as pasting, searching for file content, saving and exiting, here are only some of the most basic and commonly used key commands.
In the command mode, press a.i.o and other keys to quickly switch to the input mode, and at the same time determine the method and position of the insertion point in order to enter the file content, you need to return to the command mode, just press the Esc key. Several common mode switching keys and their functions are as follows.
a: Insert content after the current cursor position. A: Insert content at the end of the line where the cursor is (end of line). i: Insert content before the current cursor position. I: Insert content at the beginning of the line where the cursor is (beginning of line). o: Insert a new line after the line where the cursor is. O: Insert a new line before the line where the cursor is.
To move the cursor in the direction, directly press the ↑, ↓, ←, → one of the direction keys to complete the corresponding cursor movement.
Move the question page
Press the Page Dewn key or the Crl F key combination to move a whole page of content to the next page.
Technical Pagt lip or Cr1 8 key combination to flip up a full page of content.
Quick jump within the line
Press the Horme key to turn the cursor into the "key, and the 0 key will quickly jump the cursor to the beginning of the line. Press the End key or the 1 key to quickly jump the cursor Go to the end of this line. In the above key operations, PageDown, Pagn Up, homm, End and direction keys can also be used in the input mode of w.
Quick jump between lines
Use the key command 1G or 0 to jump to the first line of the file content. Use the key command G to jump to the last line of the file. Pressing specific keys on the keyboard moves the cursor to a line in the selected file (replace "two" with a number).
In order to easily view the inter-line interpolation effect, here we first learn how to display the line number in the v editor. Just switch to the last line mode and
execute the following "set cu" command to display it. Line number, execute the "set nonu" command to cancel the line number display.
The vi editor interface display format after displaying the line number is as follows (the number at the beginning of each line is the line number No.):
delete operations.
You can delete a single character at the cursor position by pressing the "Multiply" key or the "Delete" key. Use the key command dd to delete the line where the current cursor is. Use the #dd form to delete the content of the # line starting from the cursor (replace the "#" sign with a specific number). Use the d key command to delete all characters from the current cursor to the beginning of the line. Use the ds key command to delete all characters from the current cursor to the end of the line.
Copy operation, use the key command Yy to copy the entire content of the current line to the clipboard. Use the form #Yy to copy the content of the # line starting from the cursor (replace the "#" sign with a specific number ).The copied content needs to be pasted before it can be used.
Paste operation. In the vi editor, the previously deleted or copied content will be saved in the clipboard buffer. Press the p key to paste the content in the buffer to the cursor position. Press the P key to paste it into before the cursor position.
In the command mode, press the / key to enter the specified string and search backward starting from the current cursor (press the ? key to search forward Search). After completing the search, you can press the n and N keys to select among different search results. For example, enter "/yum" and press the Enter key. The "yum" string in the file will be found and highlighted. The cursor Automatically move to the first search result. Press the n key to move to the next search result, as shown in the figure.
When editing the file content, sometimes you need to undo some mistaken editing operations. At this time, you can use the u and U keys. The u key command is used to cancel the latest operation and restore the operation results. You can press the υ key multiple times to restore the multi-step operations that have been performed; the U key command is used to cancel All edits made to the current row. Pressing the ZZ key simultaneously saves changes to the current file and exits the Vi editor.
Press the : key in the command mode to switch to the last line mode. The ": prompt" will be displayed on the last line of the vi editor. User You can enter a specific last line command after the prompt to complete rich functional operations such as saving the file, exiting the editor, opening a new file, reading the contents of other files, and replacing strings.
Save the file. After modifying and confirming the file content, you need to execute the following "w" command to save.
If you need to save it as another file, you need to specify a new file name and file path if necessary.
For example, execute the following ": w/root/newfile
operation will Save the currently edited file to the /root directory with the file name newfile.
Exit the editor. When you need to exit the vi editor, you can execute the ":q command. If the file content has been modified but not saved, just use the ";q" command. The command will not exit successfully. In this case, you need to use the following "q! " command to force exit (exit without saving).
#Save and exit. To save the file and exit the vi editor, you can use a last line command ":wq" Or “:
##Open a new file for editing. When using the current vi editor and executing the last line command such as ":e new file", you can edit (Edit) a new file.For example, executing the following ":e~/initial-setup-ks.cfg" operation will directly open the initial-setup-ks.cfg file in the current user's home directory for editing.
:e ~/initial-setup-ks.cfg
Read the contents of other files in the current file. Execute the last line command in the form of "r other files" to read (Read) other files. The contents of the file and copy it to the current cursor position.
For example, executing the following "r/etc/filesystems operation will copy the contents of the system file /etc/filesystems to the current file.
:r /etc/filesystems
In the last line mode of the vi editor, you can replace the file in Replace specific strings with new content. When the same content needs to be modified in large batches, using the replace function will greatly improve editing efficiency. The last line command format when using the replace function is as follows:
: [replacement range] sub/old content/new content[/g]
In the above replacement format, the main keyword is sb (Substitute, replacement). It can also be abbreviated as s. The replacement range is an optional part, and the default When only the content in the current line is replaced, it can generally be expressed in the following two forms.
%: Find and replace within the entire file content. n.m; Search and replace within the file content within the specified number of lines.
The /g part at the end of the replacement format is also optional, indicating that all matching results in each line within the replacement range will be replaced. When /g is omitted, only the first match in each line will be replaced. result.
For example, to replace the "initdefault" string in lines 5 to 15 of the document with "DEFAULT", you can perform the following operations:
:5, 15 s /initdefault/DEFAULT/g
When you need to perform a search and replace operation on the entire document range, you need to use the "%" symbol to indicate all. For example, the following operation will replace all " initdefault" string is replaced with "bootdefault"
:% s /initdefault/bootdefault/g
The above is the detailed content of How to use vi text editor in Linux. For more information, please follow other related articles on the PHP Chinese website!