Android extended series (7)-advanced use of the vim Editor

Source: Internet
Author: User


In December last September, the android extended series (4)-basic use of the vim editor recorded the simplest Vim operations. But the charm of VIM comes from the magical skill operations.
these skills can be used to skillfully combine Vim features, experience summaries from specific occasions, or special small functions.
frozen for three feet, not a day cold!
these skills need to be practiced or used continuously in practice until you feel addicted.
after learning these skills, you will not only be able to help, but also be helpful; and it is definitely a symbol of your identity as a Program member. It represents a professional, high-end!
editor god!
however, Do not be scared. Stick to it and you will see the light after a steep learning curve begins!
I will select some commands based on my own experience (to be concise and clear, try to remove the commands that are particularly simple or rarely used ).



1. move command
(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, previous word (first word)
W, word, next word (first word)
E, end, next word (last word)
note that these lower-case commands are divided by letters, dots, numbers, and underscores. The commands for these three uppercase letters, B, W, E, are similar, but it is moved by spaces. In short, the upper-case mobile range is large.
(3 ). gg's first line
G's tail line. These two commands use more
(4 ). line n % to N %
(5 ). ^/0 to the beginning of the line. Sometimes I especially like 0, which is very clear and concise.
$ to the end of the line
(6 ). -Move the beginning of a non-blank row up
+ move the beginning of a non-blank row down
(7 ). moving rows

nj // Move down n lines
nk // Move up n lines
nh // Move n columns to the left
nl // Move n columns
n_ // Move down n-1 lines and move the cursor to the beginning of the non-blank line
n- // Move up n lines and move the cursor to the beginning of the non-blank line
(8). Move by finding characters

fx / Fx // Find the character x backward / forward, in-line jump is very useful
tx / Tx // Find character x backward / forward
// The difference between fx / Fx and tx / Tx is to use the f cursor to stay on x, and the t cursor to stay on the previous one of x

; // Execute the last fFtT
, // Execute fFtT in the opposite direction, these two are interesting
2. Insert, replace, delete
    (1). Learn to mix a and i, don't just use i to enter insert mode, sometimes a is more convenient after entering the insert mode, such as inserting the letter symbol on the right side of the keyboard, if a is used, the left and right hands cooperate more efficiently.
    (2). Learn I and A, quickly enter the beginning and end of line insertion mode, it is super cool to use at a specific moment!
This is easy to understand, the lowercase i is in front of the cursor, the size I is in front of the entire line, o / O is similar, and the picture is very clear.
    (3). Learn to mix o and O, don't just use o to enter the downstream editing:
           (3.1). Enter the upper edit, the capital O is faster, not ko, both hands are faster than one hand!
           (3.2). When you want to insert a line before the first line, O is more direct and faster than any other operation (do not go back to the beginning <+ insert mode> + Enter)
    (4). R Quickly replace characters. The biggest advantage is that you do n’t have to switch modes, saving a lot of time. Very useful! (R, I do n’t use this command very much, sometimes it ’s very confusing, so I wo n’t say it)
           s Delete the character after the cursor and enter the insert mode. This command provides a fast implementation method of r command + entering insert mode.
    (5). C This command is very powerful.

cm // m refers to the previous move command, similar to dm, ym, g? m, gum, gUm
                       // Change the content (m defines the change range) and enter the insert mode, the change here is to delete, for example:
cw // Delete the word after the cursor and enter insert mode
cl // Delete the letter after the cursor and enter insert mode
    (6). Cc and S modify the current line, which means to delete all the characters in the line, but keep the leading space
           D and C delete all contents after the line cursor, regardless of characters or spaces, I use this command a lot: when the code review formatting code, delete the blank space and end space.
    (7). Learn to combine this command

t // till means, this is one of my favorite commands
dt "// Delete in line until", "do not delete
ct) // Change in line until),) do not delete
...
    (8). Learn to mix x and X, delete characters forward and backward respectively
    (9).: Rd delete line r

3. Find

# // Next search result
* // Next search result
// Press n / N after # / * to repeat / reverse their commands

// The following two commands are very useful when programming
gd // Jump to the defined position of the cursor variable (local variable)
gD // Jump to the defined position of the cursor variable (global variable)
4. Undo and restore

u // withdraw
ctrl + r // Restore
5. Scroll

ctrl + e / y // scroll down / up line
ctrl + d / u // Scroll down / up and down half a page
ctrl + f / b // scroll down / previous page
6. Visual mode (select block)
    The visual mode is actually very useful, its selection is very flexible, and the visual effect looks the same as the mouse selection, the learning cost is low, but the function is powerful.
    (1). V Enter visual mode
    (2). Shift + v, which is capital V, line selection of visual mode, selection of entire line and entire line, it is more convenient to process the entire line or large content.
    (3). Ctrl + v Column selection in the visual mode is more detailed than the row selection mode, you can select continuous columns to enter the operation
           Select block mode needs to be combined with other commands:

V + G // Select the current line until the end of the text
V + G + d // Delete the current line until the end of the text
V + G + y // Copy the current line until the end of the text
V + gg + d // Delete the current line until the beginning of the text
V + gg + y // Copy the current line to the beginning of the text
ctrl + v + <move> + d // Delete the selected column
ctrl + v + <move> + y // copy the selected column
    (4). Gv select the last selected area
    (5). Select words, sentences, paragraphs:

vaw // select a word (word)
vas // select a sentence (sentence)
vap // Select a paragraph (paragraph)
viw // Select a word (word)
vis // select a sentence (sentence)
vip // Select a paragraph (paragraph)
    (6). Select the brackets:

vab // select the contents of (), including the parentheses
vaB // select the content in {}, including braces
7. Execute shell commands

// Remember these two usages, you taste their sweetness
: sh // Open the shell, execute shell commands, enter exit to exit the shell and return to vim
:! cmd // Execute shell directly
For example, the automated packaging script mentioned in the previous article, after I modified the code, I need to execute this script to compile it, but I still do n’t want to quit vim completely, because I still need to continue to modify, I can use it

: 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 directly in the vim window, then press Enter or other commands to continue
    Is it convenient!

8. Topic 1: Quickly select, delete, copy, modify the content in quotation marks or brackets

// The following operations must be memorized, which can greatly improve the efficiency of your text editing
ci ’, ci”, ci (, ci [, ci {, ci <// change the text content of these paired punctuation marks respectively
di ’, di”, di (or dib, di [, di {or diB, di <// delete the text content of 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 of these paired punctuation marks respectively
9. Topic 2: Replacement
The search and replace operation is used too frequently. Here are some commonly used powerful usages of replacement from the Internet or books for this topic:

//grammar
/ **
 * Description
 * range indicates the number of lines, if it is%, it means all lines
 * s means substitution, meaning of substitution
 * flag indicates the flag, the value g, i, c, etc.
 * g means global, all
 * i means ignore, ignore case
 * c means confirm, one by one to confirm the replacement
 * count means from the current line to the next few lines, indicating the range
 * /
: [range] s / {pattern} / {string} / [flag] [count]
(1). Replace all
The parameter g realizes all replacements, otherwise only one is replaced

:% s / {old-pattern} / new-text / g
(2). Inline replacement
% Represents all lines, remove% to indicate the current line, only replace the current line

: s / {old-pattern} / new-text / g
(3). Specified line range replacement
Replace the results of rows 1 to 10

: 1,10s / {old-pattern} / new-text / g
(4). Visual mode replacement
If you find it difficult to count the number of lines, you can use the visual mode, first enter the visual mode, and then select the line
Column, the last input: automatic entry: '<,'>
After adding s / {old-pattern} / new-text / g, only replace the result of the selected area

: '<,'> s / {old-pattern} / new-text / g
(5). Whole word replacement, not partial matching words
Outsourcing a <and> to the word matching pattern

: s / \ <blog \> / weibo / g // Replace blog, but cnblog, blogs will not replace
(6). Multiple replacements
Replace multiple matches with a certain string at the same time

:% s / \ (good \ | nice \) / awesome / g // good and nice will be replaced by awesome
(7). Confirmation replacement
The parameter c allows substitution to make interactive requests, requiring you to choose y, n, a, l, q

/ **
 * Selection instructions:
 * y replaces and jumps to the next matching result
 * n does not replace and jump to the next matching result
 * a replace all and exit interactive mode
 * l Replace current and exit interactive mode
 * q Exit interactive mode
 * /
: s / {old-pattern} / new-text / gc
(8). Insert line number at the beginning of the line
Replace the first line ^ with the line number, you can customize the specific form

:% s / ^ / \ = line (".") / g
10. Topic 3: Case Conversion
    Speaking of this, I think this question is quite interesting.

~ // Change the current character and automatically switch to the next character, uppercase to lowercase, lowercase to uppercase, you can keep pressing
gum // Convert to lower case, m refers to mobile commands, such as:
                      // guw lowercase a word after the cursor
                      // guG The line from the cursor to the last line is all lowercase
                      // gut = Lowercase characters after the cursor until =
                      // Long word, gu can also be combined with the visual mode to lowercase the selected area
gUm // Convert to uppercase, usage is the same as above
11. Topic 4: Recording
    This is actually very simple, but it is also a very powerful function.

// Define macro or alias for operation
// The power of this command is to simplify the repetition of complex instructions
q // Press q to start recording
x // x is the recording register, x can be other characters
dd // Delete the current line, this operation can customize other complex operations
q // End recording, now dd is defined in @x command
12. Topic 5: Digital increase and decrease
    This little trick is quite useful, for example, defines a series of static final variables, and from time to time you have to insert some new variables in between, these values are generally increasing, this time this technique is definitely more convenient than the number replacement.

// Execute in Normal mode
Ctrl + a // Increment by 1
Ctrl + x // Decrease by 1
13. Summary
     Work well and do your best first.
     Thinking of these first, there must be a lot of omissions, and what comes to mind later, I will add updates at any time.

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.