How to Use Vim

Source: Internet
Author: User
Don't try to write down a lot at a time
Master the two or three skills you need most at a time.
Then I am familiar with it. Well, one day, I suddenly found that you are a Vim guru.
-----------------------------------------------
Preface:
Vim is a very good editor (or beyond the scope of the Editor), and it is very troublesome to use the mouse. You can press ESC in word or Q in ue, which is strange.
The following describes the most common and useful tips of VI. For example, the "FX" Command finds the next letter X in the current line, for example "'. "Return to the Last edited place. For example," Ma "inserts a tag a and uses" 'a "to return the tag, also, "Shift + C" is deleted to the end of the row and directly enters the insert mode ....
---------- I am a split line --------
Vim mode and command
Normal Mode (n) in any other mode, the ESC or Ctrl + C key can be used to return to normal mode.
Insert mode (I) In this mode, VIM is like a common editor. In normal mode, I or a can be used for this mode. Of course, there are other commands that can also be used.
Visual mode (V) in this mode, you can use hjkl to select. Then copy, paste, or other operations. In normal mode, run the V command to enter the visual mode.
Block operation mode (v) This is the block operation mode. In normal mode, press Ctrl + V to enter this mode.
Modify mode (r) This is the rewrite mode. Many software methods use the Insert key to complete this switch. In vim, R can be used in the normal mode to enter the rewrite mode.
Extended command mode (Ex) This is the command execution mode used in Normal Mode: switch to this mode

Hint: it is usually in the normal or insert mode, and the visual mode is used when copy/paste is required. In general, new Vim users prefer the insert mode and move the cursor through the arrow key in the lower right corner of the keyboard. However, the vim function in the insert mode is equivalent to that in Windows notepad, in normal mode, Vim's efficient editing and other functions can be used.

Vim basic editing skills
Remove trailing blank lines: % S/S + $ // G
The tab key is converted into four spaces: % S/T // G (there are four spaces before/g)
H, J, K, and l Vim are used to replace direction keys, so they are more efficient in later editing.

Move the cursor efficiently in the current row
FX: move the cursor to the next X of the current row. Obviously, X can be any letter, and you can use; to repeat your previous F command.
TX: similar to the preceding command, but it is moved to the left of X. (This is really useful)
FX: it is similar to Fx, but you can look back.
W: move the cursor forward to a word.
B: Move a word behind the cursor.
0: move the cursor to the beginning of the current row.
^: Move the cursor to the first letter of the current row.
$: Move the cursor to the end of the row.

Move the cursor efficiently throughout the file
: Move a screen down.
: Move the screen up.
%: Jump to the matching left/right brackets
GG: To the file header
G: to the end of the file
ZZ: Move the current row to the center of the screen
ZT: Move the current row to the top of the screen
ZB: Move the current row to the bottom of the screen
*: Read the string at the cursor and move the cursor to where it appears again.
#: It is similar to the above, but it is looking in the opposite direction.
/Text: search for the string text from the current cursor and reach the place where the text appears. You must press enter to start the search command. If you want to repeat the previous search, press n.
? Text: similar to the above, but in the opposite direction.
MA: Mark a bookmark at the position of the current cursor. Its name is. Only lowercase letters are allowed. You can't see the existence of bookmarks, but it is already there.
'A: To bookmarks. Note that this is not a single quotation mark. It is located on the left side of keyboard 1.
'.: The last time you edited the file. This command is useful, and you do not need to mark it yourself.

Efficient input/Deletion
I: insert it to the left of the Current Character
I: insert at the beginning of the current row
A: insert it to the right of the Current Character
A: insert at the end of the current row
O: Insert a new row under the current row.
O: Insert a new row on the current row.
DD: Delete the current row.
D $/d ^: Delete to the end of a row/the beginning of a row
C {motion}: Delete the characters crossed by the motion command and enter the insert mode. For example: C $, this will delete the characters from the cursor position to the end of the line and enter the insert mode. CT !, This removes the exclamation point from the cursor position to the next one (but not included), and then enters the insert mode. The deleted character is stored in the clipboard and can be pasted out.
Press V to enter the visual mode, select multiple rows, and then press = to automatically format

Search Skills
/XXX (? XXX) indicates searching for strings matching XXX in the entire document,/indicates searching down ,? Indicates to search up. After searching, enter n to find the next match and enter n to search in the opposite direction.
* (#) When the cursor stays on a word, enter this command to search for the next (top) word that matches the word. similarly, enter n to search for the next match, and enter n to search for the opposite direction.
G * (G #) this command is similar to the previous command, except that it does not fully match the word where the cursor is located, but matches all strings containing the word.
GD searches for words that match the word with the cursor, and places the cursor in the non-comment section of the document where the word appears for the first time.
% Search for the parentheses matching the cursor location, including () [] {}
F (f) X is located in the row where the cursor is located. Search for the first X character on the right (left) side of the cursor. After finding the character, enter the character to continue searching. Enter the character to search in the opposite direction.

After the if Condition Statement is inserted, there are three ways to indent the code block:
Use shift-V to select the rows, and then use> to indent to the right.
> NJ, N indicates the number of rows (> indicates indentation, and NJ indicates moving n rows down)
Place the cursor on the first braces {and press % = to enable Vim auto indent.

Vim Advanced Skills
Multi-Window Editor
Create window: New (create a window above the current window) and vnew (create a new window on the left of the current window)
Split Window: Split (horizontal) and vsplit (vertical)
Change the size of the current window:
+ (Increase)
-(Decrease)
_ (Maximum window height)
| (Maximum window width)
= (Restore all windows of the same size)

Switch window direction keys
J. Switch to the window below the current window.
K. Switch to the window above the current window.
H. Switch to the left-side window of the current window.
L switch to the window on the right of the current window
Repeat an action once: Number + action, for example:
2dd Delete two rows
10 + add 10 rows to the current window
Comment on multiple lines of code: press Ctrl + V, select multiple lines, and press SHIFT + I to enter the insert mode, after several characters are inserted, Press ESC to insert the same characters before all selected rows.

Vim code folding
The foldmethod option can be used to set the foldmethod: Set OFDM = *****.
There are 6 Ways to select the fold:
Manual manual definition folding
More indent indicates more fold
Expr uses expressions to define collapse
Syntax uses syntax highlighting to define collapse
Diff folds Unchanged text
Marker folds the mark in the text
Note that each folding method is incompatible. If expr is not used and marker is used, I use indent and marker methods in turn.

Fold command
After the folding method is selected, we can implement the folding we need for some code. Because I use more indent and marker, their usage is used as an example: if the indent method is used, VIM automatically folds the middle part of the braces, and we can directly use these ready-made folding results.
In a foldable place (in the middle of braces ):
ZC folding
ZC folds all nested folding points in the range
Zo unfold and fold
Zo expands all nested folding points in the range
[Z to the start of the current fold.
] Z to the end of the currently opened fold.
ZJ moves down. To the start of the next fold. The closed folding is also included.
ZK moves up to the end of the previous fold. Closed folding is also included

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.