VI Editor for the veteran, one of the biggest advantages is that there are many options to use, and for the novice, this is also a big difficulty. This chapter describes the basic vi text editing commands. This article mainly describes how to move the cursor, and how to add and change text.
As previously mentioned, the VI editor has two modes, command mode and insert mode. In command mode, everything you enter is treated as an input command by the VI editor, and in insert mode, everything you enter is updated to the file buffer as appropriate text. When the VI editor is opened, the default is command mode, there are many ways to convert from command mode to insert mode, such as input "I". If you want to convert from Insert mode to command mode, simply press the "ESC" key. So if you don't know which mode is currently in VI, you can press the ESC key to return to command mode.
To move the cursor:
The first thing you need to do when editing text is to move the cursor to where you want to edit it. When you move the cursor, you first need to confirm that you are in the VI command mode, and move the cursor like this command:
A single move:
H: Move one character to the left;
J: Move down one line;
K: Move up one line;
L: Move one character to the right;
Although it is possible to move by four buttons up or down or through "BACKSPACE" and "enter" keys, these four commands are more commonly used because it allows our fingers to move without leaving the center of the keyboard. When you move to the limit in one direction, you can't move on. You can add a number to the front of these move commands to achieve the function of command repetition. For example, entering "4l" is equivalent to pressing the L command 4 times, that is, you can directly move the cursor to the right four characters. This command repetition function can give us more choices and enhance the functionality of the command, which should be kept in mind.
There are two fast-moving commands in a row:
0: Move to the beginning of the line;
$: Move to the tail of the line;
Note In the VI editor, a line that is visible on the screen is not necessarily a true line, and the VI editor is the end of the line with a newline character, so it may appear as multiple lines on the screen, but it is actually a line, as shown, although the screen appears to occupy three rows, but is actually a row:
Can be moved by text block, text block can be word, sentence or paragraph.
W: Moves the cursor to the next word, where the symbol and punctuation are also considered to be a word;
W: Moves the cursor to the next word, where punctuation and symbols are not treated as a single word;
B: Move the cursor backwards a word, where punctuation and symbols are treated as a word;
B: Move the cursor backwards one word, where punctuation and symbols are not treated as a single word.
Of course, these words can be moved by the command to add numeric parameters, so that you can move multiple words at the same time, such as 2w,5b.
You can use the "G" command to move directly to a specific row. The direct input "G" command, is moved to the end of the file, 1G is moved to the first line of the file, that is, the top of the file, and 10G can be moved directly to the tenth line of the file.
Edit text:
Insert new text: Using the "I" Command for text insertion, the i command causes the VI editor to convert from command mode to insert mode, and everything entered will be updated into the buffer as input text. The details of the "I" Command are worth saying: when the "I" command is entered, the cursor does not move and the text is inserted at the position before the cursor.
Add text: Add text using the "a" command. The a command is basically similar to the I command, except that when the "a" command is entered, the cursor moves backwards by one character, so it is called "Add" text after the original cursor is entered.
Change text: If you need to change the text, you need to use the "C" command. The change command can be thought of as deleting the text that needs to be replaced, and then waiting for you to enter new text, and the length of the text entered is not required. With the C command, you can use the C command with the cursor Move command in order to tell the C command how much text needs to be changed:
CW: From the position of the cursor to the end of the word;
C $: From the position of the cursor to the end of the line, you can also use the commands directly ;
C0: From the position of the cursor to the beginning of the line;
C2B: Two words from the cursor position, and so on similar usage;
If you need to modify a whole line, you can enter the CC command directly. the CC command will replace an entire line of text directly with the new text entered, regardless of where the cursor is at that line. Both the CC command and the C command mentioned above are shorthand for other commands.
In fact, more than the C command, other commands, such as the Delete (d) command, the drag (y) command, and so on, have the following general form: (command) (text object). the command here is our c,d,y, and the text object is the cursor Movement command, which indicates the range of texts that the command affects. Because the cursor move command can use numeric parameters, you can use "d2w" in this way, and "2DW" is the exact same effect.
the "r" command can replace one character with another. and after the substitution is directly the command line mode, does not need to press the ESC key from the input mode to return to the command mode.
The R command can also be substituted for characters relative to the "R" command, but the "R" key can only replace one character at a time, and when the R key is pressed, it goes into overwrite mode. That is, the characters you type will overwrite the original characters one at a-until the ESC key is pressed. However, the R command can only cover a full line at most.
"S" command: also used for character substitution, it can specify a numeric parameter, which represents how many characters to replace. If you use only the "s" command, the symbol is replaced with the current cursor. The "s" command first removes the character that needs to be replaced, and then waits for the new character to be inserted, and then manually press the ESC key back into command mode.
s command: The s command replaces a whole line of text directly, regardless of where the current cursor is located in the line, to delete the row first. so the S command is equivalent to the CC command, but differs from the C command.
Change case: The "~" Command completes the case conversion of the current character , that is, if it is currently uppercase, it is converted to lowercase, and if it is lowercase, it is converted to uppercase. The cursor automatically moves to the next character after the change.
This chapter VI editor commands summarize:
h,j,k,l,w,b,g,i,a,c,cc,c,r,r,s,s,~.
Learn VI and VIM editor (2): Simple text editing (1)