1. Move commands
(1 ). h j k l, direction key, and memory tips: The left and right directions are the leftmost and rightmost, and j is checked downward, so it is the downward key, the remaining k is the up key.
(2). B, before, last word (first word)
W, word, next word (first word)
E, end, next word (end)
Note that these lower-case commands are divided by letters, dots, numbers, and underscores, And the commands corresponding to these three uppercase letters, B, W, E, and similar meanings, but it is moved by spaces. In short, the upper-case mobile range is large.
(3). gg first line
G tail line. These two commands use more
(4). n % jump to the line of n %
(5). ^/0 to the beginning of the line. Sometimes I like 0 very clearly and concisely.
$ To the end of a row
(6).-Move the beginning of a non-blank row up
+ Move the beginning of a non-blank row downward
(7). Move rows
Copy codeThe Code is as follows: nj // move n rows down
Nk // move n rows up
Nh // move n columns to the left
Nl // move n columns to/from
N _ // move n-1 rows down and move the cursor to the beginning of a non-blank row
N-// move n rows up and move the cursor to the beginning of a non-blank row
(8). move by searching for charactersCopy codeThe Code is as follows: fx/Fx // forward/forward search for character x, which is useful for intra-row jump.
Tx/Tx // forward/forward search for character x
// The difference between fx/Fx and tx/Tx is that the f cursor stays on x, and the t cursor stays on the front of x.
; // Execute the last fFtT
, // Execute fFtT in the opposite direction. These two are interesting.
2. Insert, replace, and delete
(1 ). to mix a and I, do not just use I to enter the insertion mode. Sometimes, it is more convenient for a to enter the insertion mode. For example, if a is used to insert a letter to the right of the keyboard, the combination of left and right hands is more efficient.
(2) learn I and A, and quickly enter the insert mode at the beginning and end of the line. It is super nice to use it at A specific time!
This is easy to understand. The lower case I is before the cursor, the size I is before the whole line, the o/O is similar, and the picture is clear.
(3) Learn to mix o and O. Do not only use o to go to the downstream for editing:
(3.1). Go to the upstream editing page. The upper-case O is faster, not ko. The two hands are faster than the one-handed one!
(3.2). When you want to insert a row before the first line, O is more direct and fast than any other operation (do not return to the beginning <+ insert mode> + press Enter)
(4). r: A quick replacement of characters. The biggest advantage is that you do not need to switch the mode, which saves a lot of time and is very useful! (R, I use a few commands, and sometimes it is very confusing. I won't talk about it)
S. Delete the character after the cursor and enter the insert mode. This command provides the r command + a quick way to enter the insert mode.
(5) The. c command is very powerful. Let's take a look at the classification description.Copy codeThe Code is as follows: cm // m Refers to the previous Mobile Command, similar to dm, ym, g? M, gum, gUm
// Change the content (m defines the change range) and enter the insert mode. The change mentioned here is deletion. For example:
Cw // Delete the word after the cursor and enter the insert mode
Cl // Delete the last letter of the cursor and enter the insert mode
(6). cc and S modify the current row, which means to delete all characters of the row, but retain the first space of the row.
D and C delete all content after the cursor of the row, whether it is a character or space. I use this command many times: When the code reviews and formatting the code, it deletes the space of the blank line and the space at the end of the line.
(7) Learn to combine the t commandCopy codeThe Code is as follows: t // till, which is one of my favorite commands
Dt "// Delete in the row until", "Do not delete
Ct) // Changes in the row until),) does not delete
......
(8). Learn how to mix x and X to delete forward and backward characters, respectively.
(9).: rd deletes row r.
3. Search
Copy codeThe Code is as follows: # // search for the next result
* // Search for the next result
// After #/*, run the n/N command to repeat them.
// These two commands are useful in programming.
Gd // jump to the position defined by the cursor variable (local variable)
GD // jump to the position defined by the cursor variable (global variable)
4. Revocation and recovery
Copy codeThe Code is as follows: u // undo
Ctrl + r // restore
5. Scroll
Copy codeThe Code is as follows: ctrl + e/y // scroll down/the last row
Ctrl + d/u // scroll down/top half page
Ctrl + f/B // scroll down/Previous Page
6. Visual mode (select block)
The visual mode is actually very useful. Its selection is flexible, and the visual effect is the same as the selection of the mouse, the learning cost is low, but the function is powerful.
(1). v enters the visual mode
(2) shift + v is the upper-case V. You can select the rows in the visual mode and select the whole row for convenient processing of the entire line or large space.
(3). Press ctrl + v to select columns in visual mode, which is more refined than row selection mode. You can select continuous columns to enter the operation.
To select block mode, you must combine it with other commands:
Copy codeThe Code is as follows: V + G // select the current line until the end of the text
V + G + d // Delete the current row until the end of the text
V + G + y // copy the current row until the end of the text
V + gg + d // Delete the current row until the beginning of the text
V + gg + y // copy the current row until the beginning of the text
Ctrl + v + <move> + d // Delete selected columns
Ctrl + v + <move> + y // copy the selected column
(4). gv selects the last selected region
(5) select words, sentences, and paragraphs:Copy codeThe Code is as follows: vaw // select a word)
Vas // select a sentence (sentence)
Vap // select a paragraph (paragraph)
Viw // select a word)
Vis // select a sentence (sentence)
Vip // select a paragraph (paragraph)
(6). Selected parentheses:Copy codeThe Code is as follows: Content in vab // select (), including parentheses
Content in vaB // select {}, including braces
7. execute shell commandsCopy codeThe Code is as follows: // remember these two usage methods.
: Sh // open the shell. Run the shell command and enter exit to exit the shell and return to vim.
:! Cmd // directly execute shell
For example, after I modify the code of the automated packaging script mentioned in the previous article, I need to execute this script to compile it, but I do not want to exit vim completely because I still need to modify it, you can useCopy codeThe Code is as follows: sh // open a terminal
./Build. sh // execute build. sh
Exit // exit the terminal and continue vim editing
// Or
:!. /Build. sh // execute the build. sh script in the vim window, and then press Enter or another command to continue
Is it convenient!
8. Topic 1: Quickly select, delete, copy, modify the content in quotation marks or brackets
Copy codeThe Code is as follows: // note the following operations to greatly improve the efficiency of text editing.
Ci ', ci', ci (, ci [, ci {, ci <// modify the text content in these paired punctuation marks, respectively.
Di ', di ", di (or dib, di [, di {or diB, di </Delete the text content in these paired punctuation marks respectively
Yi', yi ", yi (, yi [, yi {, yi <// copy the text content of these paired punctuation marks, respectively.
Vi ', vi ", vi (, vi [, vi {, vi <// select the text content in these paired punctuation marks respectively
9. Topic 2: Replacement
The search and replace operations are used too frequently. The following describes several frequently used replacement methods from the Internet or books for this topic:Copy codeThe Code is as follows: // syntax
/**
* Description
* Range indicates the number of rows. If it is %, it indicates all rows.
* S indicates substitution, meaning of replacement
* Flag indicates the flag. values include g, I, and c.
* G indicates global, all
* I indicates ignore, case insensitive
* C Indicates confirm, which is replaced by one interactive confirmation.
* Count indicates the number of rows from the current row to the next row, indicating the range
*/
: [Range] s/{pattern}/{string}/[flag] [count]
(1). replace all
Replace all parameters with g; otherwise, replace only one
(1). replace all
Replace all parameters with g; otherwise, replace only one
: % S/{old-pattern}/new-text/g
(2). Intra-row replacement
% Indicates all rows. % indicates the current row. Only the current row is replaced.
: S/{old-pattern}/new-text/g
(3). Replace the specified row range
Replace the results of rows 1 to 10
: 1, 10 s/{old-pattern}/new-text/g
(4). Visual mode replacement
If you think the number of rows is troublesome, you can use the visual mode, first enter the visual mode, and then select the row
Column, input: automatically enter: '<,'>
After s/{old-pattern}/new-text/g is added, only the result of the selected region is replaced.
: '<,'> S/{old-pattern}/new-text/g
(5) Replace the entire word instead of Partially Matching words.
Outsourcing a word matching mode <and>
: S/\ <blog \>/weibo/g // Replace the blog, but the cnblog and blogs won't replace
(6). Replacement of multiple items
Replace multiple matches with a certain string.
: % S/\ (good \ | nice \)/awesome/g // both good and nice will be replaced with awesome
(7). Confirm replacement
Parameter c allows replacement for interaction requests. You need to select y, n, a, l, q.
Copy codeThe Code is as follows :/**
* Selection instructions:
* Replace y and jump to the next matching result
* N is not replaced and jumps to the next matching result
* A replaces all and exits the interaction mode.
* L replace the current mode and exit the Interaction Mode
* Q: exit the interaction mode.
*/
: S/{old-pattern}/new-text/gc
(8) insert row number at the beginning of the line
Replace the first line ^ With the row number. You can customize the specific form.
: % S/^/\ = line (".")/g
10. Topic 3: case-insensitive Conversion
I think this problem is quite interesting.
Copy codeThe Code is as follows :~ // Change the current character and automatically switch to the next character. Replace uppercase letters with lowercase letters and uppercase letters. You can always press
Gum // convert to lowercase, and m indicates the move command, for example:
// A word after the guw cursor is lowercase
// All rows from guG cursor to last row are in lowercase
// Gut = lowercase characters after the cursor till =
// The gu can also be combined with the visual mode to make the selected area lowercase.
GUm // convert to uppercase. The usage is the same as above
11. Topic 4: Recording
This is actually very simple, but it is also a very powerful function. Let's explain it separately.Copy codeThe Code is as follows: // define a macro for an operation or take an alias
// This command is powerful in simplifying repeated complex commands.
Q // press q to start recording
X // x is the recording register. x can be another character.
Dd // Delete the current row. This operation can be used to customize other complex operations.
Q // recording ends. NOW dd is defined in the @ x command.
12. Topic 5: increase or decrease the number
This tips is quite useful. For example, we have defined a series of static final variables, and we have to insert some new variables from time to time. These values are generally incremental, at this time, this technique is more convenient than replacing numbers.
Copy codeThe Code is as follows: // run in Normal Mode
Ctrl + a // auto-increment 1
Ctrl + x // auto-minus 1
13. Summary
Do your best to do your work first.
When you first think about this, there must be many omissions. I will add updates at any time when I come up with things.