Vim tutorial, vim
Vim is a powerful editor with many commands. This document will not be detailed in this tutorial. The purpose of this tutorial is to describe some necessary basic commands. With these commands, you can easily use vim as a universal editor. It takes about 25-30 minutes to complete this tutorial, depending on your training time. The command operations in each section will change this article. We recommend that you copy a copy of this article and then perform training on the copy (if you start the tutorial through "vimtutor", this article is already a copy ). Remember: the design concept of this tutorial is learned during use. That is to say, You need to execute commands to learn their correct usage. If you only read these commands without performing any operations, you may soon forget these commands! Now, please make sure that your Shift-Lock (case-insensitive key) has not been pressed, and then press the keys j on the keyboard enough times to move the cursor, until the content in section 1 is fully filled with the screen.
1. Lecture 1: getting started with 1.1 moving the cursor
※To move the cursor, press the h, j, k, and l keys respectively. ※※
^
K indicates that the key of h is on the left, and is moved to the left every time you press it.
<H l> l's key is on the right. Each press will move to the right.
The j key looks like a downward arrow at the tip.
V
A. move the cursor freely on the screen until you feel comfortable.
B. Press the downstream key (j) until the cursor repeats.
-> Now you have learned how to move to the next lecture.
C. now use the downstream key to move the cursor to lecture 2.
Tip: If you are not sure about the letter you press, press <ESC> to return to Normal mode. Enter the command you want on the keyboard again.
Tip: The cursor key should work properly. However, with the hjkl key, you can move the cursor around the screen quickly.
1.2 enter and exit VIM
!! Note: Please read the entire section before you can execute the commands described below.
A. Press the <ESC> key to ensure that you are in normal mode ).
B. Enter: q! <Press enter>
-> Exiting the editor in this way will never save the changes you have made since entering the editor.
If you want to save the changes and exit, enter:
: Wq <press enter>
C. If you see a command line prompt, enter the command that can bring you back to this tutorial, that is:
Vimtutor <press enter>
In general, you can also use this method:
Vim tutor <press enter>
-> Here 'vim 'indicates entering the vim Editor, while 'tutor' indicates the file you want to edit.
D. If you are confident that you have kept these steps in mind, please exit from step 1 to Step 3 and then go to the editor again. Next, move the cursor to section 3 of the first lecture to continue our tutorial.
1.3
Delete text editing
** In Normal mode, you can press the x key to delete the characters at the cursor position. **
A. move the cursor to the line marked with-> below this section.
B. to correct input errors, move the cursor to the position of the character to be deleted.
C. Press the x key to delete the error characters.
D. Repeat steps 2 to 4 until the sentence is corrected.
-> The ccow jumpedd ovverr thhe mooon.
E. Well, the row has been corrected. The next section is the fourth section of the first lecture.
Note: Do not force your memory when you browse this tutorial. Remember: Learn in use.
1.4 text editing insert
** In normal mode, you can press I to insert text. **
A. move the cursor to the first line marked with-> below this section.
B. To make the content of the first line similar to that of the second line, move the cursor to the position where the first character of the text is to be inserted.
C. Press the I key and enter necessary text characters.
D. All texts have been corrected. Press <ESC> to return to normal mode.
Repeat steps 2 to 4 to correct sentences.
-> There is text misng this.
-> There is some text missing from this line.
E. If you are satisfied with the text insertion Operation, please read the following summary.
Conclusion 1.5
A. move the cursor in the screen text. You can use either the arrow keys or the hjkl keys.
H (left shift) j (downstream) k (upstream) l (right shift)
B. To enter the vim Editor (from the command line prompt), enter the: vim file name <press enter>
C. To exit the vim Editor, enter the following command to discard all modifications:
<ESC>: q! <Press enter>
Alternatively, run the following command to save all the changes:
<ESC>: wq <press enter>
D. Delete the character at the cursor position in normal mode. Press: x.
E. In normal mode, to insert text at the cursor position, Press:
I. Enter necessary text <ESC>
Note: pressing the <ESC> button will bring you back to normal mode or cancel a command that is not expected or partially completed.
Okay. The first step is to end. Next we will continue with the second lecture.
2. Lecture 2 delete and undo
2.1 Delete class commands
** Input dw can be deleted from the cursor to the end of a single word/word. **
A. Press <ESC> to make sure that you are in normal mode.
B. move the cursor to the line marked with-> below this section.
C. move the cursor to the beginning of the word to be deleted.
D. Enter dw to delete the word.
Note: The dw you entered will appear on the last line of the screen at the same time. If the input is incorrect, press <ESC> to cancel the operation and try again.
-> There are a some words fun that don't belong paper in this sentence.
E. Repeat steps 3 to 4 until the sentence is corrected. Next, let's continue with the second lecture.
2.2 Other deletion commands
** Enter d $ to delete the cursor from the current cursor to the end of the row. **
A. Press <ESC> to make sure that you are in normal mode.
B. move the cursor to the line marked with-> below this section.
C. move the cursor to the end of the row (that is, after the first vertex ).
D. Enter d $ and delete it from the cursor to the end of the current row.
-> Somebody typed the end of this line twice. end of this line twice.
E. Continue to study section 3 to understand what is going on.
2.3 about commands and objects
The format of the DELETE command d is as follows:
[Number] d object or d [number] object
The meaning is as follows:
Number-indicates the number of times the command is executed (optional, 1 by default ).
D-Indicates deletion.
Object-indicates the object to be operated on by the Command (which is described below ).
A brief Object List:
W-from the current position of the cursor to the end of a word/word, including spaces.
E-from the current position of the cursor to the end of a word/word, but the space is not included.
$-From the current position of the current cursor to the end of the current row.
Note:
If you are brave in explorer, please press the key that represents the corresponding object in normal mode without using the command, then
The movement of the cursor is shown in the object list above.
2.4 Special Cases of object commands
** Enter dd to delete the entire row. **
Given the high frequency of row deletion, VIM designers decided to simplify the whole row deletion. They only need to hit d twice on the same row to delete the entire row where the cursor is located.
A. move the cursor to the second line in the short paragraph below this section.
B. Enter dd to delete the row.
C. Move to the fourth row.
D. Enter 2dd. (Do you still remember the number-command-object mentioned above ?) Delete two rows.
1) Roses are red,
2) Mud is fun,
3) Violets are blue,
4) I have a car,
5) Clocks tell time,
6) Sugar is sweet
7) And so are you.
2.5 undo class commands
** Enter u to undo the last command and enter U to correct the entire line. **
A. move the cursor to the line marked with-> below this section and place it at the first error.
B. Enter x to delete the first letter you do not want to retain.
C. Then enter u to undo the last executed (once) command.
D. Use x to correct all errors of the row this time.
E. Enter an uppercase U to restore the original state of the row.
F. Enter the u multiple times to undo the U and the previous command.
G. Then enter the CTRL-R multiple times (press the CTRL key not open, then enter the R key), so that you can execute the recovery command, that is, to undo the undo command.
-> Fiix the errors oon thhis line and reeplace them witth undo.
H. These are very useful commands. The following is a summary of the second lecture.
Conclusion 2.6
A. to delete A word from the current cursor to the end of the word/word, enter: dw
B. To delete the cursor from the current row to the end of the current row, enter: d $
C. to delete the entire row, enter: dd
D. The format of the next command in normal mode is:
[Number] command object or command [number] object
It means:
Number-indicates the number of command executions
Command-indicates what to do. For example, d Indicates deletion.
Object-indicates the object to be operated. For example, w represents a single word/Word, and $ represents the end of a row.
$ (To the end of line), etc.
E. To cancel the previous operation, enter u (lower case u)
To cancel the changes made in one row, enter U (uppercase U)
To undo the previous undo command, restore the results of the previous operation, enter: CTRL-R
3. Lecture 3: Modify and replace the 3.1 placement class command
** Input p to place the last deleted content after the cursor **
A. move the cursor to the first line of the demo section below this section.
B. Enter dd to delete the row. This will save the row to the vim buffer.
C. move the cursor over the position to be placed. Remember: It's the top.
D. Then, in normal mode (<ESC> enter), enter p to paste the row.
E. Repeat steps 2 to 4 to place all rows in the correct position in sequence.
D) Can you learn too?
B) Violets are blue,
C) Intelligence is learned,
A) Roses are red,
3.2 replace Class commands
** Enter r and a character to replace the character at the cursor position. **
A. move the cursor to the first line marked with-> below this section.
B. move the cursor to the right position of the first error.
C. Enter r to replace the error.
D. Repeat steps 2 and 3 until the first line has been modified.
-> Whan this lime was tuoed in, someone presswd some wrojg keys!
-> When this line was typed in, someone pressed some wrong keys!
E. Let's continue with section 3 of the third lecture.
Note: Remember to learn in use rather than in memory.
3.3 change class commands
** To change part or all of a single word/word, enter cw **
A. move the cursor to the first line marked with-> below this section.
B. Place the cursor at the u LOCATION OF THE lubw letter.
C. Enter cw to correct the word (in this example, input ine .)
D. Press the <ESC> key and then move the cursor to the first letter of the next error to be changed.
E. Repeat Steps 3 and 4 until the first sentence is exactly the same as the second sentence.
-> This lubw has a few wptfd that mrrf changing usf the change command.
-> This line has a few words that need changing using the change command.
Tip: Please note that the cw command not only replaces a word, but also enables you to enter the text insertion status.
3.4 use other c-instruction change commands
** You can change the object parameters used by class commands by using the same Delete class commands. **
A. Changing the operating method of class commands is the same as deleting class commands. The operation format is:
[Number] c object or c [number] object
B. The same is true for object parameters. For example, w represents a single word/Word, and $ represents the end of a row.
C. move the cursor to the first line marked with-> below this section.
D. move the cursor to the first error.
E. Enter c $ to make the remaining part of the row correct the same as that of the second row. Press <ESC>.
-> The end of this line needs some help to make it like the second.
-> The end of this line needs to be corrected using the c $ command.
Conclusion 3.5
A. to re-insert the deleted text content, enter the lower-case letter p. This operation can place the deleted text content after the cursor. If the last row is deleted, the row is placed in the next row of the current cursor.
B. To replace the characters at the cursor position, enter the lower-case character r and the new character to replace the original character.
C. The change class command allows you to change the specified object from the current cursor position to the end of the object. For example, input cw can replace the content from the current cursor to the end of the word; input c $ can replace the content from the current cursor to the end of the line.
D. Change the class command format:
[Number] c object or c [number] object
Next, let's continue with our next lecture.
4. Search for location 4.1 and file status in Lecture 4
** Enter CTRL-g to display the row position and file status information of the current cursor in the current editing file.
Enter the SHIFT-G to jump directly to a specified line in the file. **
Tip: you must read the content of this section before performing the following steps !!!
A. Press CTRL and press g. Then a status message is displayed at the bottom of the page.
The row that displays the currently edited file name and the total number of lines of the file. Remember the row number in step 3.
B. Press the SHIFT-G key to make the current cursor jump directly to the last line of the file.
C. Enter the row number you have stayed in, and then press SHIFT-G. In this way, you can return the row where you pressed CTRL-g for the first time. Note: When you enter a row number, the row number is not displayed on the screen.
.
D. If you want to, continue with steps 1 to 3.
4.2 search commands
** The input/and trailing strings can be used to search for the string in the current file. **
A. Enter/character in normal mode. You will notice that the character and the optical mark will appear at the bottom of the screen, which is the same as the command.
B. Enter errroor <press enter>. The errroor is the string you want to search.
C. To search for the same string as the previous one, you only need to press the n key. To search for the same string in the opposite direction, Enter Shift-N.
D. If you want to reverse query a string, use? Replace.
-> When the search reaches the end of the file it will continue at the start.
"Errroor" is not the way to spell error; errroor is an error.
Tip: if the search has reached the end of the file, the search will automatically continue from the file header.
4.3 search for matching brackets
** Matching parentheses can be searched by %),], and}. **
A. Place the cursor below this section to mark any of the rows (, [, or.
B. Press % characters.
C. The position of the time mark should be in the matching brackets.
D. Press % again to jump back to the first bracket of the pair.
---> This (is a test line with ('s, ['s] and {'s} in it .))
Tip: During program debugging, this function is useful for finding unpaired parentheses.
4.4 one of the methods for correcting errors
** Input: s/old/new/g can replace old with new. **
A. move the cursor to the line marked with-> below this section.
B. Enter s/thee/the <press enter>. Note that this command only changes the first matching string of the row where the cursor is located.
C. Input: s/thee/the/g to replace the matching string of the entire row.
-> The best time to see thee flowers is in thee spring.
D. To replace each matching string between two rows, enter #, # s/old/new/g (#, # stands for the row number of the two rows ). Enter % s/old/new/g to replace each matching string in the entire file.
Conclusion 4.5
A. Ctrl-g is used to display the current cursor position and file status information. Shift-G is used to jump the cursor to the last line of the file. First, knock in a row number and then press Shift-G to move the cursor to the row represented by this row number.
B. if you enter/followed by a string, search for the string in the document you are editing. Enter question mark? Followed by a string, the string is searched forward in the document being edited. After a search is completed, press the n key to repeat the previous command. You can find the next string in the same direction, or press Shift-N to find the string in the opposite direction.
C. If the current cursor position is parentheses (,), [,], {,}, you can move the cursor to the matching brackets by %.
D. Replace the old string with the new String in one line. Enter "s/old/new ".
Replace all the strings old with the new string new in one line. Enter: s/old/new/g.
Replace all the strings old with the new string new in the two rows. Enter: #, # s/old/new/g.
Replace all the strings old with the new string new in the file. Enter % s/old/new/g.
During full-text replacement, ask the user to confirm that c option is required for each replacement. Enter % s/old/new/gc.
5. Lecture 5: Save and extract the merge 5.1 Method for executing external commands in VIM
** Input :! Then, enter an external command to execute the command. **
A. Press what we are familiar with: the command sets the cursor to the bottom of the screen. In this way, you can enter the command.
B. Enter the exclamation point! This character allows you to execute external shell commands.
C. Take the ls command as an example. Enter! Ls <press enter>. This command will list the contents of your current directory, just as the result of entering the ls command at the command line prompt. If! Ls does not work. You can try :! Dir.
-> Note: all external commands can be executed in this way.
-> Tip: All commands must end with <press enter>.
5.2 more information about saving files
** To save changes to the file, enter w FILENAME. **
A. Enter :! Dir or :! Ls obtains the content of the current directory. You should have known that
<Press enter>.
B. Select a file name that does not exist, such as TEST.
C. Enter w TEST (here TEST is the selected file name .)
D. This command will save the entire file with the file name TEST (VIM tutorial ). To ensure correct saving, enter again :! Dir to view the contents of your directory list.
-> Note: If you exit VIM and enter the file with the file name TEST as the parameter, the file content should be exactly the same as the file content you saved.
E. Now you can enter :! Rm TEST to delete the TEST file.
5.3 a selective saving command
** To save part of the file, enter: #, # w FILENAME **
A. Run it again :! Dir or :! Ls gets the content of the current directory, and then selects a suitable file name without duplicate names, such as TEST.
B. move the cursor to the top of the page, and press CTRL-g to find the row number. Don't forget the row number.
C. move the cursor to the bottom of the page and press CTRL-g again. Don't forget this line.
D. To save only a part of the article, enter: #, # w TEST. Here, #, # Is the row number (the top row number and the bottom row number) that you need to remember, and TEST is the selected file name.
E. Finally, use :! Dir: Check whether the file is correctly saved. But do not delete it this time.
5.4 extract and merge files
** To insert the content of another file into the current file, enter r FILENAME **
A. Enter :! Dir confirm that the TEST file you created earlier is still in.
B. move the cursor to the top of the current page.
Note: After step 3 is executed, you will see section 3 of lecture 5. Please move it back here.
C. Use r TEST to extract the file named TEST.
Note: The extracted file will be placed at the position of the cursor.
E. To confirm that the file has been extracted successfully, move the cursor back to the original position and pay attention to the two
Section 3 of Section 5: one is the original and the other is a copy from the file.
Conclusion 5.5
A .:! Command is used to execute an external command.
See some examples:
:! Dir-displays the content of the current directory.
:! Rm FILENAME-used to delete a file named FILENAME.
B.: w FILENAME can save the file being edited in VIM to the file named FILENAME.
C.: #, # w FILENAME can save the content of row # To row # Of the current edited file to the file FILENAME.
D.: r FILENAME can extract the disk file FILENAME and insert it after the cursor position of the current file.
6. In the sixth lecture, open and insert the 6.1 class open command.
** Input o opens a new line under the cursor and enters the insert mode. **
A. move the cursor to the line marked with-> below this section.
B. Enter the lower-case o at the bottom of the cursor * to open a new line and enter the insert mode.
C. Copy the row marked with-> and press the <ESC> key to exit the insert mode and enter the normal mode.
-> After typing o the cursor is placed on the open line in Insert mode.
D. To open a new line at the top of the cursor *, you only need to input uppercase O instead of lowercase o. Please try it in the next test. When the cursor is over the row, you can press Shift-O to open a new row above the row.
Open up a line above this by typing Shift-O while the cursor is on this line.
6.2 insert a class command after the cursor
** Input a will insert text after the cursor. **
A. In normal mode, move the cursor to the end of the first line marked with-> below this section by entering $.
B. Enter lowercase a to insert text after the cursor. In upper case, A can insert text directly at the end of the row.
Tip: you can insert text at the end of A line by inputting uppercase A to avoid entering I. move the cursor to the last character and enter the text. <ESC> return to normal mode, right-click the arrow to move the cursor and x to delete the characters at which the current cursor is located.
C. You can complete the first line after the operation. Note that the insert text behind the cursor is basically the same as the insert mode, but the position of the insert text is slightly different.
-> This line will allow you to practice
-> This line will allow you to practice appending text to the end of a line.
6.3 version of another replacement command
** You can enter uppercase letters to replace multiple consecutive characters in R. **
A. move the cursor to the first line marked with-> below this section.
B. move the cursor to the beginning of the first word in the second line different from the one marked with->, that is, the last word.
C. then input the uppercase R to start inputting the remaining characters in the first line different from the second line one by one. Then you can replace all the original characters so that the first line is completely the same as the second line.
-> To make the first line the same as the last on this page use the keys.
-> To make the first line the same as the second, type R and the new text.
D. Note: If you press <ESC> to exit the replacement mode and return to the normal mode, the text that has not been replaced will remain unchanged.
6.4 set command-like options
** Set options for case-insensitive search or replacement **
A. to search for the word ignore, enter/ignore in normal mode. To search for the word again, you can press n again.
B. Then set the ic option (ic is the abbreviation of the first letter in Case Ignore Case is ignored in English), that is, enter:
: Set ic
C. Now you can search for the word ignore again by typing the n key. You can type n for repeated searches.
D. Set the hlsearch and incsearch options and enter the following content:
: Set hls is
E. Now you can enter the search command again to see what the result will be:
/Ignore
Conclusion 6.5
A. Input lowercase o to open A new line under the cursor and place the cursor at the beginning of the new line to enter the insert mode.
Enter an uppercase O to open a new line above the cursor and place the cursor at the beginning of the new line to enter the insert mode.
B. Input lowercase a to insert text after the cursor position.
Input uppercase A can insert text after the end of the row where the cursor is located.
C. If you enter an uppercase value, R enters the replacement mode until you press <ESC> to exit the replacement mode and enter the normal mode.
D. Input: set xxx to set the xxx option.
7. Online Help commands
** Use the online help system **
Vim has a comprehensive online help system. To start the help system, select one of the following three methods:
-Press the <HELP> key (if any on the keyboard)
-Press the <F1> key (if any on the keyboard)
-Input: help <press enter>
Enter q <press enter> to close the Help window.
Provide a correct parameter to the ": help" command. You can find help on this topic. Test the following parameters (do not forget to press Enter. :):
: Help w <press enter>
: Help c _ <T <press enter>
: Help insert-index <press enter>
: Help user-manual <press enter>
8. Create a STARTUP script
** Enable vim **
Vim has many more features than vi, but most features are not activated by default. To enable more functions, you must create a vimrc file.
A. start editing the vimrc file, depending on the operating system you are using:
: Edit ~ /. Vimrc this is the command used by the Unix System
: Edit $ VIM/_ vimrc: The Command Used in Windows.
B. Import the vimrc sample file:
: Read $ VIMRUNTIME/vimrc_example.vim
C. Save the file. The command is:
: Write
When you start vim next time, the editor will have the syntax highlighting function. You can continue to add other feature settings you like to this vimrc file.
9. Conclusion
The vim tutorial ends now. This tutorial is just to briefly introduce the vim editor, but it is enough for you to easily learn to use it. There are still many other commands in vim, which are far behind this tutorial. So if you want to be proficient, we hope you can continue to work hard. In the next step, you can read the vim manual and use the following command:
: Help user-manual
For further reference and study, the following book is recommended:
Vim-Vi Improved-Author: Steve Oualline
Press: New Riders
This is the first book to fully explain vim. It is especially useful for beginners. There are also a large number of instances and diagrams. For more information, see
Learning the Vi Editor-Author: Linda Lamb
Press: O 'Reilly & Associates Inc.
This is a good book, through which you can almost understand what all vi can do. The sixth version of this book also contains some vim information.
This tutorial is written by Michael C. Pierce and Robert K. Ware from Calorado School of Minese. Charles Smith from Colorado State University offers a lot of ideas. The editor's mailing address is:
Bware@mines.colorado.edu
This tutorial has been revised by Bram Moolenaar For vim.
From: http://www.linuxpig.com/2011/03/vimshiyongjiaocheng/