How to Use Vim more efficiently ?, How to Use Vim more efficiently?

Source: Internet
Author: User

How to Use Vim more efficiently ?, How to Use Vim more efficiently?
1. Vim operation mode-use it first

If you do not know the two Vim modes (Command modeAndInsert mode), You can't even save the most basic edits, let alone the efficiency. Vim can be proud ~ Don't let Emacs hear) It depends on this design idea.

Command mode After Vim is started, it is in command mode. All the essence of Vim is in this mode. Put it in the next section.
Insert mode PressIKey, "-- Insert --" is displayed in the lower-left corner of the editor to enter the Insert mode. You can edit the text like notepad.
Switch between two modes PressIEnter InsertMode, pressESC Return to command mode
You also need to know some basic commands to use Vim normally (but not efficient ):
:wq Save disk + exit (:wStorage,:qExit)
ZZ It is also the disk storage + exit (I usually use this, it is a few milliseconds faster than: wq)
x Delete the character of the current cursor
dd Delete the current row and save the row to the clipboard.
p Paste clipboard
Hjkl Move the cursor (although the "up/down" key can also be used, but it is still nitpicking that hjkl does not need to move its hand to the direction key. It will take more than dozens of milliseconds)

 

2. Command mode-with your installation X with your flying command mode, if you want to further segment it, it can be divided Command mode(Enter various shortcut keys) and Last Row Mode(Enter various commands. 2.1 command mode-common shortcut keys in command mode, as long as you do not press ":", it is the command mode. Almost every key is a shortcut key. You can see the following shortcut keys, you can use Vim normally.
Insert mode   Delete (CUT)/copy/paste
I Insert at the cursor   D Delete (CUT) must be used with the cursor movement command, for example, dw: delete a character to the start of the next word
A Insert after the cursor   Y Copy, same as d
O Insert a new row after the current row   Dd Delete (CUT) The current row
O (uppercase) Insert a new row before the current row   Yy Copy current row
Cw Delete the cursor to the end of the word and enter the insert mode.   P (lower case) Paste the post after the cursor
 Move cursor   P (uppercase) Paste the post before the cursor
0 (number zero) To the line Header   Undo/Redo/do multiple times
^ To the position of the first non-null character in the row   U Undo
$ To the end of a row   <Ctrl-r> Redo
G _ To the position of the last non-null character in the row   . Repeat the previous command
B To the beginning of the previous word   Number n + command Execute the command n times, such as 3dd: delete 3 lines
W To the start of the next word   Case-sensitive replacement
E To the end of the next word   GU Change to uppercase
Gg To 1st rows   Gu Small write
G To the last line    Visualization
NG,: n To line n, such as "13G" or ": 13", move the cursor to line 13   V
After entering visual mode, you can select a code block and then perform operations. A great feature is automatic code alignment. You only need to press the "=" key after selecting the code block.
2.2 Line End mode-Common commands press the ":" key in command mode to enter the line end mode. You can enter various commands at the end of the line. The following are the most common commands:
Open/Save/exit/change the file
:q Exit
: W Save
: X,: wq, ZZ Save and exit (:xSave only when needed. ZZ does not need to enter the colon or press Enter)
: Saveas <filePath> Save
:q! Exit and do not save
:qa! Forcibly exit all files being edited and do not save
:e <filePath> Open a file
:bnAnd:bp After opening multiple files through: e, use these two commands to switch between the next or previous files.
: N Move the cursor to line n, for example, ": 13" to line 13.

 

3. Advanced Skills

If you don't think it's enjoyable, there's more advanced content here.(Note: although it is an advanced technique, it is very practical and enjoyable. Skills not used for centuries will not be written here)

3.1 search and replace

(1) Search

Intra-row search
F Backward Search ("fa" locates at the next "a" position)
F Forward search ("Fa" locates at the previous "a" position)
T Search backward ("ta" locates a character before the next ")
T Forward lookup ("Ta" locates a character before the previous ")
Full-text search
/ Supports regular expressions. "N" continues searching, "N" continues searching in reverse direction
? Search up, same as above
* Search down to match the word (string) where the cursor is located)
# Search up, same as above
% Search for matching parentheses, including (,[,{. Move the cursor to the brackets first. Bunker!

    

(2) Replacement

Syntax:[Addr] s/source string/destination string/[option]
[Addr] Indicates the search range. If it is omitted, it indicates the current row.
For example, "1st" indicates the number of rows from to 20;
"%": Indicates the entire file, which is the same as "1, $ ";
"., $": From the current row to the end of the file;
S Replacement Operation
[Option] Operation Type
For example, g indicates global replacement;
C Indicates confirmation;
P indicates that the replacement result is displayed row by row (Ctrl + L restore the screen );
If option is omitted, only the first matching string in each line is replaced;
If special characters appear in the source string and target string, escape them "\"
Example
: % S/source string/destination string/g Global replacement
: % S/\ (That \) or \ (this \)/\ u \ 2 or \ l \ 1/g Replace That or this with This or that
(1) Put the Regular Expression in \ (\), and access the content in \ (\) with \ 1 and \ 2;
(2) uppercase letters of \ u and lowercase letters of \ l
: % S/* // g Replace multiple spaces with one space.
: % S/\ ([:.] \) */\ 1/g Replace multiple spaces after the period or colon with one space.
: % S/^ // Insert two blank spaces at the beginning of each line

 

3.2 Automatic completion

  <Ctrl-p>Search up<Ctrl-n>Search down. Use <Ctrl-p> or <Ctrl-n> to list the words you have previously typed.

  

3.3 block operations

In command mode, press <Ctrl-v> to enter block mode. The most common function is to operate multiple lines at the same time. The steps are as follows:

(1) press <Ctrl-v> to enter the block mode.

(2) Select the area block to be processed by the cursor

(3) Press "I" to insert the block before the block, and press "A" to insert the block after the block.

(4) After the insertion is complete, press <ESC> to make each row take effect

3.3 split screen

Split screen
Vim-on file1 file2... Up/down split screen (lowercase o, n is a number, indicating how many screens are divided)
Vim-On file1 file2... Left/right split screen (uppercase O)
: Sp filename Up/down split screen
: Vsp filename Left/right split screen
<Ctrl-w> s Split up and down the current file
<Ctrl-w> v Split the current file left and right
Disable Split screen
<Ctrl-w> c Closing the current window. The last window cannot be closed.
<Ctrl-w> q Close the current window. You can close the last window.
Move cursor
H, j, k, l Move the cursor in a screen
<Ctrl-w> h, j, k, l Switch Screen
Screen Size
<Ctrl-w> = Split screens
<Ctrl-w> + Move up split line
<Ctrl-w>- Move down split line
<Ctrl-w> Left shift split line
<Ctrl-w> < Right Shift split line
3.4 code formatting

(1) Press "v" to enter the view mode.

(2) Select the code block to be formatted

(3) press the "=" key and shake it ~ Earth people ~

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.