VI is a very commonly used text editor that is widely used in Linux systems. It has powerful features and flexible operation methods that are able to meet the needs of all kinds of editing text files. This article will expand the usage of Linux VI commands in detail, including the start, exit, move, edit, and save operations of VI. It focuses on the tips and precautions for using vi commands for file editing. Through this article, the reader will be able to proficiently use the vi command to edit text files.
1. Start, exit and basic operations.
1.Start vi:
Enter the vi command in the Linux terminal followed by the file name to start vi. If the file exists, it will be opened;If the file does not exist, a new file with the same name is created. For example: vi filetxt
2.Exit vi:
Enter the colon (:) in command mode and then Q to exit VI. If the content of the file has been modified, it will prompt whether to save the changes. To force quit and not save your changes, enter a colon followed by q!. For example: :q or :q!
3.Move the cursor:
Use the H, J, K, and L keys to move the cursor left, down, up, and right, respectivelyUse the gg and g commands to move the cursor to the beginning and end of the file, respectivelyUse the number + n to move the cursor forward n lines;Use the number + ng to move the cursor to the nth line of the file.
2. Edit the file.
1.Insert text:
Press the i key to enter the insert mode, where you can freely edit and insert text.
2.Delete and copy text:
In command mode, you can use the x command to delete the characters at the cursor locationUse the dd command to delete the line where the cursor is locatedUse the yy command to copy the line where the cursor is located.
3.Undo Action:
In command mode, enter the u command to undo the last operation.
3. Save and exit files.
1.Save the file:
In command mode, enter the colon (:) followed by w to save the file. For example: :w
2.Save As:
In command mode, enter a colon (:) followed by w, followed by a file name, to save the current file as the specified file name. For example: w newfiletxt
3.Save & Exit:
In command mode, enter the colon (:) and then wq to save the file and exit. For example: :wq
4. Find and replace.
1.Find in the file:
In command mode, enter a slash ( ) followed by what you want to find, and press enter to start the search. Press the n key to continue looking for the next match.
2.Alternative Text:
In command mode, enter the colon (:) and then enter s What needs to be replaced Replaced Content , and then press Enter to replace the operation. For example: to replace all apples with orange, enter the command: %s apple orange g
5. Extended functionality.
1.Multi-window mode:
In command mode, enter colon (:) and then split to split the current window into two windows, and you can view and edit different file contents at the same time.
2.Recording with macros:
In command mode, enter q, then enter a letter (indicating the name of the macro), then edit it several times, and finally enter q to end the recording. Then enter the name of the @ followed by the macro, and you can repeat the previously recorded action.
3.Autocomplete:
In command mode, press the Ctrl and N keys to automatically complete the current word.
Summary: This article introduces the usage of Linux VI commands in detail, including start, exit, move, edit, save, find and replace. Proper use of the vi command allows us to edit text files more efficiently. Mastering the basic operations and shortcuts of vi commands, coupled with rich editing skills, can greatly improve our editing efficiency. It is recommended that the reader practice each of the operations mentioned in the text to deepen their understanding and memory of the vi command, and to become proficient in practical use. Through continuous practice and practice, I believe that you will be able to flexibly use vi commands in the Linux system to handle various text editing tasks with ease.
If you have any questions, you can leave a message or private message me, welcome to follow me [click to follow], together**.
Search Topic Full Time Challenge December