The four modes of VIM technique-common mode

Source: Internet
Author: User
Tags sublime text

Vim skills of the four modes _ ordinary mode to see the ordinary mode of not loving ordinary mode below the tough operation what is the operator what is the action command misoperation How to do that operation more cost-effective ordinary mode of the mysterious big strokes

the four modes of VIM technique-common mode

As we all know, Vim is the most distinctive thing is that it can switch mode, then what is the model. It can be simply understood to have different effects by pressing the same key under different patterns. For example, if Vim presses two times D under Insert mode, it will insert two letters d at the cursor position, and if you press two d at the bottom of normal mode, the current row of the cursor will be deleted.

Since said is the four modes of vim, then vim in the end have that four modes. Normal Mode insert mode visual mode command line mode

Users can switch between modes by pressing <ESC>, we will introduce the common mode in detail, and we will introduce the common pattern in detail in the following articles.

Like emacs,sublime text These mainstream editor in the open can directly into the inside of their desired content, but for the first time using Vim novice, not with their own imagination, the same, and then will start to go crazy: How to enter text in this box ah. Days of chatter, how to exit this black box ... (the blogger started off with a kill-9, crying)

Vim uses normal mode when it is turned on, as shown in the following figure

If you need to enter something in this black box, you can press any one of the following on the screen, VIM will enter the insert mode , and then you can enter the text

Press function
A Inserts after the cursor
A Inserts at the end of the line at the current cursor
I Insert from the cursor
I Inserts at the beginning of the current cursor
O Create a new row below the current line and insert it
O Creates a new row above the current line and inserts it

Then if you exit Insert mode back to the Normal mode between. You can just press <ESC>.

At this point you may ask, is not there an insertion mode on it. Why do we need to add a normal mode? You see the other editors do not have so many modes ... This is the refinement of vim, for other editors, if you want to achieve a more complex operation, basically through the <Ctrl>,<Alt>,<Shilt> combination to complete, but vim is not so, Vim is a complex operation by switching between different modes, and in simple terms, common patterns are used to launch skills. The insert mode is a simple text operation

For example, you need to delete the line where the current cursor is located, and for an editor with no schema to launch this skill

<CTRL-K> (Emacs)
<CTRL-L><Backspace> (Sublime Text)

If it's vim, then go to Normal mode first and then launch the Delete line skills

<ESC> (go to normal mode), DD (delete current line)

Personally or more like vim a little, concise and clear; before also tried Ctrl to the dead Emacs, combination of buttons is really much, and the hands of a small person is simply a torture. a tough operation under normal mode

Normal mode The following action can be interpreted as the following formula:
operator + Action Command = operation operator + Action Command = operation

what are operator characters.

The operator is what you plan to do next. For example, D, mentioned above, is the deletion operator, in addition to the deletion of operators, there are the following operators:

operator Use
C Modify
D Delete
Y Copy
g~ Case inversion
Gu To lowercase
GU to uppercase
> Indent Right
< Chanzo Indent
= Auto Indent
What is an action command.

Operator is used to specify an operation, and this operation can not operate blindly. I want to delete a line of text, the entire contents of the file can not be deleted ... So, simply put, the action command is used to specify the scope of the operator's action.

That's the order of action. In fact, the action command more flexible, here I summed up for two categories: A class is a text object, such as AW, AP category is the position range, such as L, j,k,f{char},$,^,0 ...

action commands for text object types

Text Object Operating Range
Aw Add a space to the word at the cursor
Iw The word where the cursor is
Ap The paragraph where the cursor is located
i< <> the text inside
i{ The text inside the {}
I "" Inside the text

Let's say there's a line of text

I am a Vimer

We need to remove the word vimer.

$ (move to end of line)
Daw (delete the word with the cursor and a space)

Finally get:

I am A

action commands for position range types
What is the position range? That is, from the current cursor, after a move command, the new cursor position between the text, such as 3j,10<space>,3l,f{char} and so on, as long as you can move the cursor command can be

Let's say there's a line of text

I am a Vimer

We're going to capitalize all the text between Vimer, the word subtitle E.

0 (move to the beginning of the line)
Gute (operator (GU) + (new cursor position TE))

Finally get:

I AM A Vimer

One thing to note:
When an operator is called two consecutive times, the corresponding scope of action is the current line, such as DD is to delete the current line, yy is the copy of the current line, Gugu is the current line to the capitalization error operation how to do.

We can switch to normal mode, and then u, you can undo the previous misoperation, such as using DD mistakenly deleted a line of text, we directly enter u can undo the previous delete operation

Here we look at a chestnut:
Enter a line of text

I am a Vimer

found that Vimer mistakenly lost to Vier, so returned to normal mode, click U want to undo this wrong word input, but the surprise found before the discovery of I am a text is also missing, vim undo too much.

So what to do. How to control the granularity of vim undo.

The U key triggers the Undo command, and it undoes the latest changes. One change can be any manipulation of text within a document, including commands that are triggered in normal mode, visual mode, and command-line mode, and one modification includes text that was entered or deleted in one insert mode, that is, i{insert some Stuff}<esc > is a modification

So we can still control the granularity of the U undo, in the insert mode, each time <ESC> even a granular control that operation is more cost-effective.

What is a bargain. That's to do more text with the fewest keystrokes.

Give me a chestnut:
Now you need to delete the last word in the following text

I am a Vimer

Operation One:

$
DB (delete word forward)
X (delete x)

Operation two:

$
B (Last word's first word)
DW (delete word)

Operation three:

$
Daw

The above three operations are all using the same number of keys, this step is a tie, if we still have to delete the remaining two words. The first two actions obviously need to be repeated from the beginning, but the third operation we just need to use. The operation can be repeated to modify the command, so the operation of the three obviously more cost-effective. So a more cost-effective definition is not only the current operation, but also the future operations to be calculated in the ordinary mode of the mysterious big strokes

This mysterious trick is actually <c-a>: add operation to the number <C-X>: reduce the number of operations

The <C-a> command adds 1 to the top or top of the current cursor, and if you need to add more than once, you can [Count]<c-a>, which adds count to the number.

Give me a chestnut:
An existing paragraph of text:

I am years old

Now you need to change 23 to 24, and if you don't know this technique now, it's only

F3 (move to 3)
R4 (change 3 to 4)

It's too complicated.
Now all we need

<Ctrl-a> (the cursor immediately jumps to 23 and adds a value of 1 to 24)

If you need to change the 23 to 1023 now (thousand Old demon)
Just need

1000<ctrl-a>

Equally available <C-x>

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.