In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Linux file editing command vi what is useful, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
1. There are three basic working modes of vi editor
The first thing to know is that the vi editor has three basic working modes: command mode, text input mode, and last line mode.
First: command line mode: this mode is the default mode after entering the vi editor. Any time, no matter what mode the user is in, press the ESC key to enter the command mode. In this mode, users can enter vi commands and manage their own documents. At this point, any characters entered from the keyboard are interpreted as editing commands. If the character entered is a legitimate vi command, vi completes the corresponding action after accepting the user's command. It is important to note, however, that the commands entered are not echoed on the screen. If the character entered is not a vi command, vi will ring the alarm.
Second: text input mode: enter command I in command mode, add command a, open command o, modify command c, replace command r or replace command s can enter text input mode. In this mode, any characters entered by the user are protected by vi as the contents of the file and displayed on the screen. During text entry, if you want to return to command mode, press ESC.
Third: the last line mode: the last line mode is also called ex escape mode. In command mode, the user presses the ":" key to enter the last line mode, where vi displays a ":" on the last line of the display window (usually the last line of the screen) as a specifier for the last line mode, waiting for the user to enter a command. Most file management commands are executed in this mode (such as writing the contents of the edit buffer to a file). After the last-line command is executed, vi automatically returns to command mode. If you want to switch from command mode to edit mode, you can type an or I. If you need to return from text mode, press ESC. Enter ":" in command mode to switch to the last line mode, and then enter the command.
To sum up, when we use a command to open a file, we enter the command mode. In the command mode, you can switch to the text input mode and the last line mode, but the text input mode and the last line mode cannot be switched directly to each other, so the text input mode can be switched to the last line mode, you need to return to the command mode and then switch, and vice versa.
In addition, editing text can be edited in text input mode, keyboard input can edit the text, or using vi command in command mode can also play an editing effect.
Example demonstration
First of all, we use the command vi filename to open a file, and this time we enter the command mode
Next, we press I, and then enter the write content casually on the keyboard.
Then press ESC to return to command mode.
In the case of command mode, we press: to enter the last line mode.
We type wqstores, then enter, and forcibly save and exit.
The next time we open the corresponding file (which can be opened with the less filename command), we can see that the content has changed.
Add:
(1) in the last line mode: Q! [force exit do not save] Q [exit do not save] wq [exit and save can also be added after! ]
(2) if you do not want to save direct exit, you can use the "ctrl+z" shortcut key or hold down the "shift" key in command mode, enter two z to exit.
More commands
Command to enter vi
Vi filename: open or create a new file and place the cursor at the beginning of the first line
Vi + n filename: open the file and place the cursor at the beginning of line n
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: system crash occurred the last time you were editing with vi, restore filename
Vi filename... .filename: open multiple files and edit them in turn
Screen tumble command
Ctrl+u: turn half the screen to the first part of the file
Ctrl+d: flip half the screen to the end of the file
Ctrl+f: flip a screen to the end of the file
Ctrl+b; flipped through the first screen of the file.
Nz: scroll the nth line to the top of the screen, or the current line to the top of the screen if n is not specified.
Insert text class command
I: in front of the cursor
I: at the beginning of the current line
A: after the cursor
A: at the end of the current line
O: create a new line under the current line
O: open a new line above the current line
R: replace the current character
R: replace the current character and subsequent characters until you press the ESC key
S: replaces the specified number of characters with the entered text, starting at the current cursor position
S: delete the specified number of lines and replace them with the entered text
Ncw or nCW: modifies a specified number of words
NCC: modifies a specified number of rows
Delete command
Ndw or ndW: delete the word NMY at the beginning and after the cursor
Do: delete to the beginning of the line
Delete: delete to the end of the line
Ndd: delete the current line and the subsequent nmur1 line
X or X: delete a character, x after the cursor, and X before the cursor
Ctrl+u: deletes text entered in input mode
Search and replace command
/ pattern: search for pattern from the beginning of the cursor to the end of the file
Pattern: search for pattern from the beginning of the cursor to the beginning of the file
N: repeat the previous search command in the same direction
N: repeat the last search command in the opposite direction
: s/p1/p2/g: replace all p1 with p2 in the current line
: N1 Magazine, n2sqqp1, p2Unix, g: replace all p1 with p2 in lines N1 to N2.
: g/p1/s//p2/g: replace all p1 in the file with p2
Option settin
All: lists all option settings
Term: setting terminal type
Ignorance: ignore case in search
List: displays the tab stop (Ctrl+I) and the end of line flag ($)
Number: display line number
Report: displays the number of modifications made by line-oriented commands
Terse: displays a short warning message
Warn: displays NO write information if the current file is not saved when transferring to another file
Nomagic: allows special characters not preceded by "\" to be used in search mode
Nowrapscan: forbids vi to start from the other end when the search reaches both ends of the file
Mesg: allows vi to display information written to their terminals by other users using write
Last line mode command
: N1 co N2 n3: copy the content between lines N1 and N2 under line n3
: N1 meme N2 m n3: move the content between lines N1 and N2 to the next line n3
: N1 and N2 d: delete the content between N1 lines and N2 lines
W: save the current file
: e filename: open the file filename for editing
X: save the current file and exit
: Q: exit vi
: Q quit: do not save the file and exit vi
:! command: execute the shell command command
: N1 recording command: take the contents of lines N1 to N2 in the file as the input of command and execute them. If you do not specify N2, it means that the entire file content is input as the input of command.
: ringing command: puts the output of the command command to the current line
Register operation
Nyy: saves the contents of the current line and its next n lines to a register, where? is a letter and n is a number
"nyw: saves the current line and its next n words to a register, where? is a letter and n is a number
"nyl: saves the current line and its next n characters to a register, where? is a letter and n is a number
"? P: take out the contents of the register and place it at the cursor position. here? it can be a letter or a number.
Ndd: delete the current line and its total n lines of text, and put the deleted contents in the No. 1 delete register.
Thank you for reading this article carefully. I hope the article "what is the use of Linux document Editing Command vi" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.