The four modes of VIM skills _ Visual Mode

Source: Internet
Author: User
Tags repetition





    • The four modes of VIM skills _ Visual Mode
    • What is Visual mode
    • How to select an area
      • How to quickly select a target area
      • How to select the target area repeatedly
      • How to toggle selection mode in the check box
    • How to work with a selected area
      • Repetition of VISUAL commands
      • It is best to use the operator command instead of the visual command




The four modes of VIM skills _ Visual Mode

In the previous two articles, "four modes of vim skill insertion mode" and "four modes of vim skill ordinary mode", we have introduced the skills in insertion mode and ordinary mode. Next, we will continue to introduce the visual mode of one of the four modes
What is visual mode?
Visual mode is simply to select an editing area, and then perform some operations on it, such as deleting, replacing, changing case, etc.
How to select an area?
In VIM, there are three ways to activate the visual mode and select an area:
Mode type activation mode selection effect
Character text V (lowercase) select text character by character
Line text V (uppercase) select text line by line
Block text < C-V > select text as a block
What do you mean by character text, line text and block text? In fact, it is a scheme to control the granularity of selection. The granularity of character text selection is a character, and the granularity of line text selection is a line of text. If you want to select only a few consecutive characters in a line, it is not suitable to use line text, because the granularity of line text selection is the smallest unit of behavior, and at least one line of content should be selected at a time. If you want to select In case of multiple lines of text, it is obviously inappropriate to use character text because it is too inefficient. Block text is the most powerful selection mode. It can completely ignore the existence of lines, and can choose freely in the text according to the required area.
For example, chestnut: notice the prompt of different selection modes in the lower left corner
Character text selection, move to h, Click v to start selection, we select the Hello world characters
To select the first two lines of text, Click v to start
To select block text, click < C-V > to start, and we will select a block of text
How to quickly select the target area
We have introduced many commands to move the cursor quickly in the normal mode blog, such as h, J, K, L. you can move the cursor in four directions, or you can use f {char} to jump to a character, and then use them again;, To repeat the jump, or use the find command / target to move the cursor quickly, and then use n / N to repeat. There are many other commands to move the cursor. Fortunately, in the visual mode, we can use these commands to move the cursor quickly
Here's a chestnut:
For example, in the chestnuts below, we should start with the 25 line character c and select it until the end of the line
First, Click v to switch to character mode, type FC, and then you can press $. At this time, the cursor will jump to the end of the line, and the content between character c and the end of the line will be selected
What if we choose the Hello World string?
We can directly FH, and then VI "can select the contents of double quotation marks"
There are many other uses like this, which readers can understand by themselves
Did you find that when you use the visual mode, once you switch to the visual mode, the selected area is defined by two endpoints (one is in the upper left corner and the other is in the lower right corner). By default, we can only control the endpoint in the lower right corner, but sometimes we need to adjust the endpoint in the upper left corner. At this time, we can use the o button to be in the upper left corner Switch between the corner and the lower right corner.
Here's a chestnut:
If we have selected the following area under block selection mode
Now we need to expand the selection area to the left a little bit. You can click the o key, and then move it with the command of moving the cursor
How to select target area repeatedly
We can use the GV command to re select the target area selected in the previous visual mode, but if the previously selected area text is deleted, the GV command will behave a little nervous
Here's a chestnut:
If we select the string "Hello world" and then delete "Hello world", if we call GV command again, the selected area is no longer the previous "Hello world", but the content with the same character width
Delete "Hello world", GV selected area
Not only that, even if the number of columns in this line of text is offset, for example, if the text is indented a few more lines, GV will find it wrong again
Therefore, the realization of the bottom layer of vim may be to remember the starting row and the number of rows and the starting column and the number of columns in the target area, type GV, and then restore according to the previously saved row and column information
So the blogger did the experiment again, changed the upper and lower lines, GV again
The findings were as expected.
How to switch selection mode in selection
One of the advantages of visual mode is that we can switch between three modes at will. For example, we want to make more fine-grained choices under line text mode. We can switch to character mode directly by pressing v. similarly, we can get the method of switching to any other two modes in a certain selection mode. Blogger will not repeat here
How to operate the selected area?
We select an area, the most direct purpose is to operate on this area, such as delete, change case, change text, etc. How to operate the selected area? In the previous section of the article, we mentioned that you can use the jump command of normal mode in visual mode. Yes, you can also use the modify command of normal mode in visual mode.
Command effect
D. delete selected text
C. modify the selected text
R replace selected text
I insert before selected text
A insert after selected text
Gu change the selected area to lowercase
Gu change the selected area to uppercase
G ~ case intermodulation
>Indent one unit to the right
< indent one unit left
The common ones are the above ones.
Here are some chestnuts:
Chestnut I:
Suppose we have the following C + + file now
The first three lines of the main function forget to add the; sign. Now we need to use the visual mode to add the semicolon to these three lines of code
First < C-V > turn on block selection mode, and then click JJ $to select to the end of the line. The selected area is as follows
Use a to add text after the selected area; although only one line of text appears to have semicolons during the editing process, such as
However, just press < ESC > and the rest of the selected lines will be automatically added with semicolons. This is slightly different from other editors. Novices should note that
Chestnut two:
Here is a chestnut that opens four file identifiers
If we have moved four files file1, File2, file3 and file4 to the new directory / TMP / filedir, then we need to change the testdir directory to filedir directory
< C-V > enter the block selection mode and select the testdir area, such as
Click C to replace the selected area and enter filedir
Just like the previous chestnut, only one line of text changes. Click < ESC > to make the change affect all selected areas
Repetition of visual commands
When it comes to repetition, the first thing I think about is the command. But this repeated command is not common. Let's first look at some chestnuts:
Character text mode Chestnut:
First, select the open word on line 27 in character text mode, and use the G ~ command to capitalize it
Note that here we select four characters, and then we move the cursor several places to the right, and move it to the beginning of the word filedir. We click. Repeat the previous visual command
The following results are obtained:
We were surprised to find that open did not change to lowercase as expected, but the first four characters of filedir changed to uppercase
Line text mode Chestnut:
Select 27 lines with V and use G ~ for case conversion
Now go to line 31 and click. Repeat the command
All 31 lines of text were found to be case reversed
Block text mode Chestnut:
Click < C-V > to enter the block text selection mode, and select any area, such as
G ~ swap case
Now let's move the cursor down one line and move it to the position shown in the figure
At this time, click... To find the following effect
We find that a rectangle range in the lower right corner starting from the cursor has been swapped case, and the size of the rectangle range is the same as the size of the area selected in the last visual mode
So the blogger concluded that different selection modes have different repetition effects
Select mode repeat effect
Character text selection mode (visual) starts with the current cursor and covers the number of characters in the last operation
Line text selection mode (visual line) starts from the current line, and its scope is the number of lines in the last operation
Block text selection mode (visual block) starts from the current cursor, and its scope is the block scope of the last operation with the current cursor as the upper left endpoint
It is better to use operator commands instead of visual commands
Why do you say that? For example, in the character text selection mode, if we want to repeat, the range of repetition is determined by the number of characters in the last operation. Among them, the same number of characters as the last operation is a big restriction. Only when the number of characters in this operation is the same as that in the last operation can we safely use the repeat operation.
Here's a chestnut:
We're going to capitalize all the printf of 27-29
On line 27, we use VI "to check what's in quotes, and u to capitalize what's selected
If we move down one line, repeat the last operation with. To get
It is found that only the first two characters are used, and the following LLO is lower case
Can't repeat the last operation, do you need to do it again manually? It's not necessary. We just use the operator command under normal mode
The chestnuts above can be better implemented with operator commands
Use GUI to convert the contents of "to uppercase, and then J. can repeat the operation of the previous line
Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger.
Four modes of vim skill

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.