1 Introduction
The Vim (Vi[Improved]) editor is a powerful cross-platform text file editing tool, inherited from the Vi editor of the Unix system, supports Linux/Mac OS X/Windows systems, and can be used to create and modify text files. Enter the Vim editing program, you can enter the following command in the terminal:
$vim [filename]
where filename is the pathname of the file to be edited. If the file doesn't exist, it will create a new one for you. The Vim editing program has three modes of operation, which are called editing mode, insert mode and command mode. When running Vim, it first enters the editing mode.
2. Edit Mode
The main use of the Vim editing mode is to move the cursor position in the edited file. Once the cursor is in the desired position, you can cut and paste text blocks, delete text and insert new text. When all editing work is done, you need to save the editor results, exit the editing program and return to the terminal, you can issue the ZZ command, and press the uppercase Z key twice in a row.
2.1 Jump
If there are navigation keys for up, down, left, and right arrows on the keyboard, use these keys to complete the movement of the cursor. Alternatively, the same move by character function can be accomplished with the following keys:
k move up; j move down; h shift left; l Move right.
The above four keys move the cursor position one line or one character at a time. Vim also provides commands to move the cursor in slightly larger ranges:
ctrl+f move forward one page in the file (equivalent to page down); ctrl+b move back one page in the file (equivalent to page up);
Greater range of movement:
* When the cursor stays on a word, the * key will search for the word in the file and jump to the next place; # When the cursor stays on a word, # search for the word in the file and jump to the previous place; (/) move to the beginning of the preceding/following sentence; {/} Jump to the beginning of the current/next paragraph. g_ to the last non-blank character on the line. fa to the next a character, you can also fs to the next s character. t, to the first character before the comma. Commas can become other characters. 3fa Finds the third occurrence of a on the current line. F/T is the same as f and t, but in opposite directions; gg positions the cursor to the beginning of the first line of the file; G Position the cursor to the beginning of the last line of the file; NG or Ngg Position the cursor at the beginning of the Nth line.
When you find the desired page on the screen, you can use the following commands to move the cursor quickly:
H moves the cursor to the first line (or the top line) on the screen; M move the cursor to the middle of the screen; L Moves the cursor to the last line of the screen.
Also pay attention to the capitalization of letters. The H and L commands can also add numbers. For example, 2H means move the cursor to the second line of the screen, and 3L means move the cursor to the last third line of the screen. When moving the cursor to the desired line, moving the cursor within the line can be done with the following command:
w moves the cursor right to the beginning of the next word; e move the cursor right to the end of a word; b move the cursor left to the beginning of the previous word; 0 Number 0, move the cursor left to the beginning of the line; $ move the cursor right to the end of the line; ^ Moves the cursor to the first non-blank character on the line.
2.2 Search for matches
Like many advanced editors, Vim provides a powerful string search function. To find the occurrence of a given word or phrase in a file, you can use Vim to search directly instead of doing it manually. The search method is: Type the characters / followed by the string you want to search for, then press Enter. The editing program performs a forward search (that is, towards the end of the file), and after finding the specified string, stops the cursor at the beginning of the string; type n command to continue the search to find out the next occurrence of the string. Location. Replacing the / with the character ? enables a reverse search (towards the beginning of the file). E.g:
/str1 Forward search string str1; n Continue to search to find the next occurrence of str1 string; N continues the search to find the last occurrence of the str1 string; ?str2 searches the string str2 in reverse.
Regardless of the search direction, when the end or beginning of the file is reached, the search loops to the other end of the file and continues. The most powerful place to perform search matching in Vim is to search in combination with regular expressions, which will be introduced later.
2.3 Replacement and deletion
Vim's regular delete commands are d, x (the former deletes lines, the latter deletes characters). Combined with other features of Vim, the basic delete function can be implemented. After positioning the cursor at the specified position in the file, you can replace the character pointed by the cursor with other characters, or delete one or more characters or one or more lines from the current cursor position. E.g:
rc replaces the current character pointed to by the cursor with c; nrc replaces the first n characters pointed to by the cursor with c; 5rA replaces the first 5 characters pointed to by the cursor with A; x deletes the current character pointed to by the cursor; nx delete the first n characters pointed to by the cursor; 3x delete the first 3 characters pointed by the cursor; dw deletes the word to the right of the cursor; ndw delete n words to the right of the cursor; 3dw delete the 3 words to the right of the cursor; db deletes the word to the left of the cursor; ndb delete n words to the left of the cursor; 5db deletes 5 words to the left of the cursor; dd deletes the line where the cursor is located and removes the gap; ndd deletes (cuts) n lines of content and removes gaps; 3dd deletes (cuts) 3 lines of content and removes gaps;
Other commonly used delete commands are:
d$ deletes characters from the current cursor until the end of the line; d0 deletes characters from the current cursor to the beginning of the line; J Deletes the carriage return (CR) character on this line and merges it with the next line.
The regular replacement commands in Vim are c and s. Combined with other features of Vim, the basic replacement function can be realized. However, after the replacement command is executed, it usually enters the insert mode from the edit mode:
s replaces the character pointed to by the cursor with the input text; S delete the current line and enter edit mode; ns replaces n characters to the right of the cursor with the text entered; nS delete n lines including the current line and enter edit mode; cw replaces the word to the right of the cursor with the input text; cW replaces all characters from the cursor to the end of the line with the input text (same as c$ ); ncw replaces n words to the right of the cursor with the input text; cb replaces the word to the left of the cursor with the input text; ncb replaces n words to the left of the cursor with the input text; cd replaces the line where the cursor is with the text entered; ncd replaces the n lines below the cursor with the text entered; c$ replaces all characters from the cursor to the end of the line with the input text; c0 Replaces all characters from the beginning of the line to the cursor with the text entered.
2.4 Copy and paste
Content removed from the body (such as characters, words, or lines) is not really lost, but is cut and copied into a memory buffer. The user can paste it into the specified position in the text. The command to do this is:
p lowercase p, paste the contents of the buffer after the cursor; P Uppercase P, pastes the contents of the buffer in front of the cursor.
If the content of the buffer is a character or word, it will be pasted directly before or after the cursor; if the content of the buffer is a whole line of text, executing the above paste command will paste the line above or below the line where the current cursor is located. Note the capitalization of the letters in the above two commands. The Vim editor often uses a pair of upper and lowercase letters (like p and P) to provide a pair of similar functions. Typically, lowercase commands operate after the cursor, and uppercase commands operate before the cursor.
Sometimes it is necessary to copy a piece of text to a new location, while retaining the contents of the original location. In this case, the specified content should first be copied (not cut) to the memory buffer. The command to do this is:
yy copies the current line to the memory buffer; nyy copies n lines to the memory buffer; 5yy copy 5 lines to the memory buffer; "+y copies 1 line to the operating system's pasteboard; "+nyy Copy n lines to the operating system's clipboard.
2.5 Undo and redo
In the process of editing a document, in order to eliminate the consequences of a wrong editing command, you can use the undo command. In addition, if the user wants to repeat the previously executed editing command at the new cursor position, the repeat command can be used.
u undo the result of the previous command; . Repeats the last command to modify the text.
3. Insert mode
3.1 Enter insert mode
Once the cursor is positioned correctly in edit mode, switch to insert mode with the following command:
i inserts text to the left of the cursor a Insert text to the right of the cursor o Add a new line to the line below the cursor line O add a new line to the line above the cursor line I insert at the beginning of the line where the cursor is located A inserts at the end of the line where the cursor is located
3.2 Exit insert mode
The way to exit the insert mode is to press the ESC key or the key combination Ctrl+[. After exiting the insert mode, it will enter the edit mode.
4. Command Mode
In Vim's command mode, complex commands can be used. Type: in edit mode, the cursor jumps to the last line of the screen, and a colon is displayed there, and you have entered command mode. Command mode is also known as last line mode. The content entered by the user is displayed on the last line of the screen. Press the Enter key, and Vim executes the command.
4.1 Open, Save, Exit
To open a file in Vim already started, use the :e command:
:e path_to_file/filename
To save the currently edited file, use the :w command (short for the word write):
:w
To save the current file as file_temp then:
:w file_temp
In editing mode, you can use the ZZ command to exit the Vim editing program, which saves the changes made to the text and overwrites the original file. If you just need to exit the editing program without saving the edited content, use the following command:
: q exits unmodified; : q! Aborts all changes and exits the editing program.
To save and exit, you can use two commands in combination (note the order of commands, save first, then exit):
:wq
4.2 Line numbers and files
Each line of text in editing has its own line number, and the cursor can be moved to the specified line with the following commands (the effect is the same as ngg or nG in editing mode):
: n move the cursor to the nth line
In command mode, the range of line numbers for command operations can be specified. The numerical value is used to specify the absolute line number; the character "." represents the line number of the line where the cursor is located; the character "$" represents the line number of the last line of the text; simple expressions, such as ".+5", represent the current line down Line 5. E.g:
:345 move cursor to line 345 :345w file write line 345 to file :3,5w file write lines 3 to 5 to file :1,.w file write line 1 to current line to file file :.,$w file write the current line to the last line in the file file :.,.+5w file Write 6 lines from the current line to the file file :1,$w file writes everything to the file file, equivalent to the :w file command
In command mode, allows reading text from a file, or writing text to a file. E.g:
:w writes the edited content to the original file, which is used to save the intermediate result of the edit :wq writes the edited content to the original file and exits the editing program (equivalent to the ZZ command) :w file Write the edited content to the file file, keeping the content of the original file unchanged :a,bw file write the contents of lines a to b to file file :r file Read the contents of the file file and insert it after the line where the current cursor is located :e file Edit the new file file instead of the original content :f file renames the current file to file :f Print the current file name and status, such as the number of lines in the file, the line number where the cursor is located, etc.
4.3 String Search
In the editing mode, we talked about the search for strings. The command mode here can also perform string searches. Given a string, you can search for the string to reach the specified line. If you want to search forward, put the string to be searched between two /; if you want to search backward, put the string between two ?. E.g:
:/str/ Forward search, move the cursor to the next line containing the string str :?str? Search backwards, move the cursor to the previous line containing the string str :/str/w file searches forward and writes the first line containing the string str to the file file :/str1/,/str2/w file searches forward and writes the line containing the string str1 to the line containing the string str2
4.4 Regular expressions in Vim
When specifying a search string to Vim, you can include characters with special meanings. Search strings that contain these special characters are called regular expressions. For example, to search for a line of text that contains the struct word at the beginning. The following command does not do this:
:/struct/
Because it only finds the first line that contains the struct anywhere in the line, it doesn't necessarily contain the struct at the beginning of the line. The solution to the problem is to prefix the search string with the special character ^:
:/^struct/
The ^ character compares the strings at the beginning of each line. So the above command says: Find the line that starts with the string struct. The word at the end of a line can also be found in a similar way by appending the special character $ to the end of the line after the search string:
:/^struct/
The following table gives most of the special characters and their meanings:
^ is placed before the string to match the word at the beginning of the line; $ is placed after the string, matching the word at the end of the line; \< matches the beginning of a word; \> matches the end of a word; . matches any single body character; [str] matches any single character in str; [^str] matches any single character not in str; [ab] matches any character between a and b; * matches 0 or more occurrences of the previous character; \ Escape the following characters.
4.5 Body Replacement
Use the :s command to replace strings. Specific usage includes:
:%s/str1/str2/ replaces the first occurrence of the string str1 on the line with the string str2 :s/str1/str2/g replaces all occurrences of the string str1 on the line with the string str2 :.,$ s/str1/str2/g Replace all occurrences of the string str1 from the current line to the end of the text with the string str2 :1,$ s/str1/str2/g replaces all occurrences of the string str1 in the text with the string str2 :g/str1/s//str2/g Same as above :m,ns/str1/str2/g will replace str1 with str2 from lines m to n
As you can see from the above substitution command:
-
1. When g is placed at the end of the command, it means that each occurrence of the search string is replaced, not only the first occurrence in each line; if g is not added, it means that only the first occurrence of the search string is replaced; g is placed in At the beginning of the command, it means to replace all lines containing the search string in the text;
-
2. s means followed by a string of replacement commands;
-
3. % means that the replacement range is all lines, that is, the full text.
Another useful command, in Vim, counts the number of occurrences of the string str1 in the current file, which can be replaced by a variant of the command:
:%s/str1/&/gn