vi command detailed explanation
vi command is a commonly used and important command under Unix, which can edit one or multiple files in full screen mode. If no file is specified when vi is executed, the vi command will automatically generate an unnamed, empty working file. If the specified file does not exist, a new file will be created with the specified file name. If the modifications to the file are not saved, the vi command does not change the contents of the original file.
Note: The vi command does not lock the edited file, so multiple users may edit a file at the same time, and the last saved file version will be retained.
The following are some options and their meanings used by the vi command:
-c sub-command Execute the specified command sub-command before editing the specified file.
-r filename Restore the specified file filename.
-R Places the specified file into the editor in read-only mode, so any modifications to the file will not be saved.
-y number Set the size of the editing window to number lines.
The following are the three modes of vi editing:
Command mode The mode in which vi is entered. In this mode, users can enter various subcommands to perform * operations, such as deleting lines, pasting lines, moving to the next word, moving to different lines, etc.
Text input mode In this mode, you can modify the content of a line and add a new line. In command mode, type a, i, or c to enter text input mode, and press the Escape key to return to command mode.
Command item mode In this mode, you can enter more parameters through subcommands. For example: the w subcommand requires the input of a file name, and the "/" subcommand requires the input of a search term. The user uses the Escape key to return to command mode.
The following is executed in the self-command mode. The self-command to move on the same line
h moves the cursor one space to the left.
l Move the cursor one space to the right.
j Move the cursor down one space.
k Move the cursor up one space.
w Move the cursor in front of the next small character.
W Move the cursor in front of the next large character.
b Move the cursor in front of the previous small character.
B Move the cursor in front of the previous big character.
e Move the cursor behind the next small character.
E Move the cursor behind the previous big character.
fc moves the cursor to the next c character on the same line.
Fc moves the cursor to the previous c character on the same line.
tc moves the cursor to the space before the next character c on the same line.
Tc Move the cursor to the space after the previous character c on the same line.
number| Move the cursor to the number column.
The following are subcommands for moving between lines in command mode
+ or Enter to move the cursor to the first non-blank character of the next line.
- Move the cursor to the first non-blank character in the previous line.
0 Move the cursor to the first character of the current line.
$ Move the cursor to the last character of the current line.
H Move the cursor to the top line of the screen.
L Move the cursor to the bottom line of the screen.
M Move the cursor to the middle of the screen.
The following are subcommands to change the screen display in command mode
z- Set the current line as the last line of the screen and redisplay the screen.
z. Set the current line as the middle line of the screen and redisplay the screen.
Ctrl+l redisplays the current content of the screen.
/pattern/z- Find the next position of pattern and set the current line to the last line of the screen.
The following are the subcommands used to display the page in command mode
Ctrl + f scrolls back one page.
Ctrl + d scrolls back half a page.
Ctrl + b scroll forward one page.
Ctrl + u scroll forward half a page.
Ctrl + e scrolls the screen down one line.
Ctrl + y scrolls the screen item up one line.
The following are the subcommands used to find strings in command mode
/pattern searches backward for the specified pattern. If it encounters the end of the file, it starts from the beginning.
? pattern searches forward for the specified pattern. If it encounters the beginning of the file, it starts from the end.
n Perform the last defined search again in the last specified direction.
N Execute the last defined search again in the opposite direction to the last specified direction.
/pattern/+number Stop the cursor on the number line after the line containing pattern.
/pattern/-number Stop the cursor on the number line before the line containing pattern.
% Move to the matching "()" or "{}".
The following are the subcommands used to enter text in text input mode (the user can press Escape at any time to return to command mode):
a Start entering text after the cursor.
A starts inputting text at the end of the line.
iStart typing text before the cursor.
IEnter text before the first non-blank character at the beginning of the line.
oInsert a blank line after the line where the cursor is.
OInsert a blank line before the line where the cursor is.
The following are the subcommands used to change text in command mode (the user can press the Escape key at any time to return to command mode):
cc or S to modify an entire line.
C Change the part after the cursor position of a line.
cw changes the word where the cursor is.
dd deletes the current line.
D Delete the content behind the cursor in the line where the cursor is.
dw deletes the word where the cursor is.
J Add the next line of content to the end of this line.
rc replaces the character where the light symbol is with c .
R overwrites the content of this line.
uRevert the last modification.
x deletes the character where the cursor is.
~ Change the case of the character under the cursor.
. Repeat the last operation.
< >>Move the current row to the right. The following is a word command used to copy text in a file p Get the buffer content to the line below the line where the cursor is. P Get the buffer content to the line above the line where the cursor is. "bd Delete the text to the famous buffer b. "bp Post the content in the famous buffer b. Yy puts the current line into the buffer. Y puts the current line into the buffer. Yw puts the word under the cursor into the buffer. The following is the subcommand for saving files :w writes back the modified file. :w filename When filename does not exist, save the modified file as file filename. When file filename exists, an error is reported. !w filename If file filename exists, save the modified file as file filename. The following is a list of subcommands used to switch between multiple files edited by vi :nStart editing vi activated The next file in the file list. :n filenames specifies the new file list to be edited. The subcommands used to switch between the current file and another file are listed below: :e filename Use filename to activate vi (load another file filename in vi). e! Reload the current file. If there are changes to the current file, discard the previous changes. :e+filename Use filename to activate vi and start editing from the end of the file. :e+number filename Use filename to activate vi and start editing on line number. :e# Start editing another file. The following are the subcommands used to add other file codes to this file: :r filename reads the filename file and adds its content to the current file. :r ! command executes the command file and adds its output to the current file. The following are other subcommands in vi ctrl+g obtains information about the file being edited. :sh starts sh, and exit or ctrl+d can be used to return from sh. :! Command executes the command command . !! Re-execute the last :! Command subcommand. : q exits vi. If the user makes changes to the edited file, the system will not let the user use the q command to exit. :q! Exit vi regardless of whether there are any changes to the file. ZZ or:wq save the modifications to the file and exit vi. Users can define special vi commands in a special file .exrc. When using these commands in vi, you must add a colon (:) before the ——————————————————- vi’s pattern vi is a program written a long time ago. At the time, keyboards didn't have as many function keys as we're familiar with today. Therefore, vi is designed to control entering and modifying text by entering characters and ESC. Maybe this is a pity for some people, but you will find that you don’t need to change the position of your hands on the keyboard to complete all functions. The result is that your typing will be faster. In order to complete interactive full-screen editing work, vi has three modes. Insert mode is used to enter text. In insert mode, any characters you enter will be displayed on the screen and saved in the file. Command mode is used for most editing functions. In command mode, all input will produce certain responses instead of going directly to the text, such as moving the cursor, deleting a piece of text, copying text, etc. The third mode is to perform additional functions such as search, global replacement, processing of multiple files, etc. This mode is based on ex editor. When vi is started, the default mode is command mode. Follow the steps below to give it a try: Enter the program name to start vi: $ vi You will see something similar to the following: ~~~~~~~~~~~~~~~~~~~ ~~~~~~Empty buffer i–Insert Now we enter “i” to enter insert mode. The character "i" will not be echoed. Anything you enter after this will appear in the cache. Now let's enter a piece of text. The words in the example are from the English version of Sun Tzu's Art of War. Note that the cursor position is underlined in the example. If wise, a commander is able to recognize changing circumstances and to act expediently. If sincere, his men will have no doubt of the certainty of rewards and punishments. If humane, he loves mankind, sympathizes with others, and appreciates their industry and toil. If courageous, he gains victory byseizing opportunity without hesitation. If strict, his troops are disciplined because they are in awe of him and are afraid of punishment. Shen Pao-hsu … said: 'If a general is not courageous he will be unable to conquer doubts or to create great plans.'~~~~~~~~~~~~~~~~ Esc–Cancel When you finish typing, press the Esc key to return to command mode. (If you are already in command mode, you will hear a horn sound when you press Esc.) Esc undoes unfinished commands and terminates insert mode. After you press Esc, the cursor will remain under the last character you entered. Unfortunately, there is no obvious sign of what mode you are currently in. But there are easy ways to tell you what mode you are in right now. If you press a key and the corresponding character appears on the screen, then you are in insert mode, otherwise you are in command mode. If you're not sure what mode you're in, press Esc twice to hear the horn sound to make sure you're in command mode. It’s time to take a look at the basic moving cursor commands. Train your fingers and let them automatically execute the commands you want in the future. Let’s take a look at how many commands there are that affect cursor movement. First, press h 5 times to move the cursor 5 positions to the left (if you see h running on the screen, you must have forgotten to press Esc). The cursor should now be under the "p" in "plans" (see example below): If wise, a commander is able to recognize changing circumstances and touch expediently. If sincere, his men will have no doubt of the certainty ofrewards and punishments. If humane, he loves mankind, sympathizes with others, and appreciates their industry and toil. If courageous, he gains victory byseizing opportunity without hesitation. If strict, his troops are disciplined because they are in awe of him and are afraid of punishment . Shen Pao-hsu … said: 'If a general is not courageous he will be unable to conquer doubts or to create great plans.'~~~~~~~~~~~~~~~ kk–Move cursor up Now let’s press k 5 times to move the cursor up 5 lines. Maybe you should think there should be a shortcut. Well, here’s an easy way: put a number in front of the action you want to take. Pressing 5k gives you the same result as pressing k 5 times. The cursor should now be under the "e" of "he". If wise, a commander is able to recognize changing circumstances and to act expediently. If sincere, his men will have no doubt of the certainty of rewards and punishments. If humane, he loves mankind, sympathizes with others, and appreciates their industry and toil . If courageous, he gains victory byseizing opportunity without hesitation. If strict, his troops are disciplined because they are in awe of him and are afraid of punishment. Shen Pao-hsu … said: 'If a general is not courageous he will be unable to conquer doubts or to create great plans.'~~~~~~~~~~~~~~~~ There are some restrictions when using these functions. For example, if you use h or l to move the cursor beyond the beginning or end of a line of text, the cursor will stay at the beginning or end and the horn will sound to remind you. Are there any others similar to h and k? Take a look at Table 3.2. The best way to get familiar with them is to use them a lot. Command movement There are some differences between uppercase and lowercase commands. Lowercase letters generally distinguish "words." with punctuation, while uppercase letters ignore them. Let’s look at the simplest and most commonly used editing process: Revision No one is immune to mistakes. So sooner or later you'll come across a situation where you need to modify the text you entered. Most of the time spent in word processing is actually revising rather than typing new things. Therefore, it is important that you know how to modify it easily. x – delete a character The easiest way to delete text is with x. The result of this command is that the character where the cursor is located disappears, and the following text moves to the left. If the character you delete is the last character in a line, the cursor will move one to the left so that the cursor will not stay under a non-existent character. If there are no more words, the horn will sound. d–Delete object There must also be a certain text object on the right side of this command. A text object is a piece of text. The characters connected to his right are the characters that control the movement of the cursor. For example, w means one word forward, then dw will delete the next word. 5w means go forward 5 words, then d5w will delete them. dd – delete a line One of the most commonly used d series commands. As before, 5dd will delete 5 lines D – delete the whole capital D is used to delete from the cursor to the end of the line. Same effect as d$. u–Recovery Want to regret it? It doesn't just undo deletions, it undoes all your editing work. . – Repeat Repeat editing work.
command.
Start vi
Moving the Cursor and Simple Editing
The most important movement commands
hh – Move the cursor to the left
Table 3.2. Common arrow keys
h left one character
j next line
k previous line
l right one character
w, W previous word (W ignores punctuation)
b, B next word ( B Ignore punctuation)
$ to the end of the line
^ to the first non-empty character at the beginning of the line
0 the beginning of the line
G to the beginning of the buffer
nG to the nth line
The Most Important Editing Programs