vim has two editing modes, one is edit mode and one is normal mode. If you want to save and exit a file after opening a file with the vim command and editing it:
Step 1: You need to press the Esc key to exit the editing mode and return to the normal mode. The esc key is the one in the top left corner of the keyboard.
Step 2: In normal mode, enter the :wq command, save and exit. Note that the :wq here requires three characters. The first one needs to hold down the shift key first, and then press the key to the right of the L key. The second is to press the w key, the third is to press the q key, and after typing :wq (you can see the entered :wq at the bottom of the editor page), then click the enter key to save and exit.
vim often dissuades many newbies because of its extremely high learning threshold, but this is largely due to the lack of good vim tutorials on the market, in fact, as long as you learn in a reasonable order, vim is not difficult, and it is extremely efficient. There are many people who can't even figure out the basic usage of vim, so they rush to learn all kinds of plugins, and finally they can't understand anything, and then complain that it's a garbage tool.
I'm going to divide the vim into four chapters, which are:
The basic operation of vim, i.e. the functions that come with vim, does not require any installation.
Vim comes with advanced operations, configuration of configuration files, such as how to configure various mappings in vim, etc.
installation and use of various plugins for vim.
The cooperation between vim and various tools, this can be understood, and calling vim with vscode comes with this part of the function.
These four chapters are a progressive relationship, and the previous chapter must be learned well in order to learn to use the next chapter.
vim is having a problem
VIM requires muscle memory to be proficient in use, if you have any questions, you can go to the QQ group "361863861" I created to ask questions, some operations are not understood after the experiment, you can discuss.
I use the latest version of vim, vim8, the use of vim has nothing to do with the operating system, ubuntu, centos, etc., you can use vim --version to view the version of vim, if it is vim7, it is recommended to upgrade to vim8, vim7 may not be supported in some features.
1-1 First acquaintance with vim: I started configuring from a bare vim.
1) Enter vim to enter vim, enter vim file name, you can use vim to edit the file. After going in it's normal mode, you can't type anything, use :q to exit vim.
2) To edit a file, you must enter edit mode, enter i (insert) to enter edit mode, and enter a (append) and o (open a line below) to enter edit mode. i means insert at the position of the cursor, a means insert at the position behind the cursor, and o means insert on the next line of the current. In order to improve the editing efficiency, i, a, and o can also enter the editing mode, i means to start editing from the front of the line, and a means to start editing from the back of the line.
So, using i, i, a, a, o, and o can all enter editing mode in different ways.
3) In edit mode, press the esc key to go back to normal mode, enter :q to exit vim, enter :wq to save the file, and then exit.
4) Note: If the file has been modified, you will not be able to exit the file by typing :q, you must enter :wq to save and exit, or type:q!If you don't save, force quit.
Introduction to the mode of 1-2vim
1) Many newbies will think, why does vim have so many modes, and vim should come up with normal mode instead of edit mode?
vim has so many modes in order to improve work efficiency.
vim comes up in nornal mode because most of our work is done in normal mode, and editing is only a small part.
These two points will be more experienced after using vim a lot.
2) vim is in normal mode initially, input: you can enter command mode. Typing :q in normal mode to exit vim is essentially typing q in command mode to exit vim. Press esc in command mode to exit command mode.
In normal mode, press the V key to enter the visualization mode, and press the ESC key to return to normal mode.
1-3vim editing tips
1) In edit mode, we can use shortcut keys to modify characters in the part we edit and modify.
Ctrl + H to remove the characters in front of the cursor.
Ctrl + W to delete the word in front of the cursor.
Ctrl + U deletes everything in front of the cursor.
2) In normal mode, you can use hjkl instead of the arrow keys, hjkl stands for left, down, up, and right respectively, so that you can move the cursor without leaving the keyboard.
3) In normal mode, you can press the gi key to jump to the position where the cursor is when exiting from edit mode, and enter edit mode.
How to move the cursor in 1-4normal mode
1) w w moves to the beginning of the next word, e e moves to the end of the next word, b b moves to the beginning of the previous word, where lowercase indicates that non-letters are divided into units (such as colons, quotation marks, commas, and spaces are dead units), and uppercase indicates that the units are divided by whitespace.
2) Interline character search to move, press f {char}, you can quickly move to char, press; , which can be moved to the next character of char to the previous character. f indicates that the search for characters starts from the file, and f indicates that the search for characters starts at the end of the file.
3) How to quickly move to the beginning or end of a line: 0 to move to the first character of a line, $ to move to the last character of a line.
4) The page move gg g to the beginning and end of the file, you can use ctrl+o to quickly return, ngg can quickly jump to the nth line of the file.
HML can quickly jump to the beginning, middle, and end of the screen.
Ctrl + U, Ctrl + F, flip up and down, zz to set the current line to the middle of the screen.
1-5 vim's built-in fast adding, deleting, modifying, modifying, and querying
1) vim quick delete: in normal mode.
x to quickly remove a character after the cursor.
You can use d with text objects to quickly delete a word, such as daw (delete around word), dw, diw. daw stands for the word where the cursor is removed, including the space next to the word. dw means to delete the word after the cursor, and diw means to delete the word where the cursor is located, but does not include the space next to the word. dt{char} represents the character removed from the cursor position before char.
2) Quick modification of vim, in normal mode.
There are three commonly used, r (replace), c (change), and s (substitute).
R can replace a character, S deletes the current character and enters insert mode.
c can be equipped with a text object to quickly modify a word, cw, ciw, caw is just on the basis of dw, diw, daw delete at the same time into the insert mode.
3) vim, query words are also a common operation.
Use or? To perform a forward or reverse search, use n n to jump to the next previous match, e.g. time to search for the word time, press n to indicate the next one.
Use * to match, cursor over one word, * for the previous and next same word.
1-6 How to replace vim
vim is: [range]s [flags].
range indicates the range to be replaced, for example, 10, 20 represents 10-20 rows, and % represents all.
string1 indicates the string to be replaced.
string2 indicates the string that is replaced.
flags denotes flags, the commonly used ones are g (which means that they are executed globally), and if there is no g, each row will only replace the first match in the row. c indicates confirmation, and we can choose whether or not to replace when making a substitution, and n indicates the number of matches, but does not make a substitution.
For example, %s my you g replaces all my in the file with you. Also, both string1 and string2 support regular expressions.