Objective
Master the syntax and concepts of common operations in normal mode, the application scenarios for these operations, and practical tips.
Master the common text operations of Cmd-line and visual with normal mode extrapolate.
Brief introduction
The ideal state for a text operation is an operator + an action command.
Normal, visual, and Cmd-line all have basic functions for manipulating text.
- Normal operator + action command.
- Visual action Command + operator.
- Cmd-line Match range + operator.
These three functions are the same when the underlying operation of a text is implemented. Mainly around the normal, the other extrapolate can be.
Learning new action commands and operators is like learning the words of vim. If you have mastered this simple language rule , by combining it, you can express more ideas when your vocabulary grows. Therefore, it is necessary to define the concept of the true correspondence of each operation, as these concepts are common throughout vim.
Uniform Rules
- Commands are usually paired, so you can keep them in mind, so that the memory can be half as much. For example:
- Word movement, from left to right and from right to left, has corresponding action commands. From left to right: w| w,e| E. Right-to-left: B|b,ge|ge .....
- The screen scrolls forward and backward. Ctrl+f| Ctrl+b, ctrl+u| Ctrl+d
- Uppercase and lowercase. Gu |gu
- The order is pictographic. Understanding this pictographic is useful for remembering. For example:
- The symbol itself represents the concept of forward or backward. ),},],>
- lowercase represents word and uppercase for word. Word movement: w (word), W (word)
- For example, by adding G to the direction, the physical line and the screen line are indicated. K (move to next physical line), GK (move to next screen line)
- Uppercase is represented to the end of the line. C is equivalent to c$,d equivalent to d$.
- The casing indicates the direction. F{char} looks from left to right, F{char} from right to left.
- When an operator command is called consecutively two times, it acts on the current line. For example: DD: Delete the current line, yy: Copy the current line, >>: Indent the current line ...
Basic concept Words
A coherent collection of non-whitespace characters separated by whitespace characters. The character does not include the keyword (iskeyword).
Details can be viewed: H Word,: H iskeyword.
Big words
A coherent collection of non-whitespace characters separated by whitespace characters. Characters include keywords (iskeyword).
For example:
This is mine/yours
Where "Mine" is word, and "mine/yours" is a word, here "mine", "/", "yours" is three word. The "/" Here is Iskeyword.
Details can be viewed: H word,:h iskeyword.
Sentence
A sentence to ".", "!", "?" End. It is important to note that it is a symbol in the English state, which is invalid in the Chinese state, such as ". "。
Details can be viewed: h sentence.
Yes
The difference between the actual row and the screen line.
Unlike many text editors, Vim distinguishes between actual rows and screen lines. When the ' wrap ' setting is enabled (enabled by default), each line of text that exceeds the width of the window will be wrapped around to ensure that no text is displayed. As a result, a row in the file may be displayed as several lines on the screen. The simplest way to know the difference between the actual row and the screen line is to enable the ' number ' setting.
It is important to understand the difference between the actual line and the screen, because Vim provides different action commands to manipulate both. The J and K commands move down and up according to the actual rows, while GJ and GK Move down and up by the screen line. That is, as long as the addition of G is the screen line, without the G is the actual line represents.
Paragraph
Separate paragraphs with a blank line area.
Details can be viewed: H paragraph.
Method
A method similar to the Java programming language.
Details can be viewed: H various-motions in the]m.
Structure
There are two types of situations:
1. Start with a symbol and end with this symbol. For example (), [],{},<>, ' ", '".
2, start with the label, and end with this label. such as <aaa> </aaa>
This is mostly text selection when used more. A and I (inner) are generally distinguished. A represents the entire structure, and I represents the text inside the structure.
Details can be viewed: H object-select,: H tag-blocks.
operator
Convention: If the following needs to be represented with the action command, {motion}.
Details can be viewed: h operator,: H XX (operator).
Modify
Definition: Deletes the specified text and enters insert mode.
Grammar:
operator |
Implementation features |
C{motion} |
Delete the specified action command select the represented text and enter insert mode |
Cc |
Delete the current row and enter insert mode |
C |
Delete the text to the end of the line and enter insert mode. Equivalent to C $ |
S |
Deletes the character under the cursor and enters insert mode. Equivalent to CL |
S |
Deletes the current row. Equivalent to CC. I don't want to use shift without pressing the SHIFT key. So generally directly with CC |
Delete
Definition: Deletes the specified text.
Grammar:
operator |
Implementation features |
X |
Delete the character under the cursor |
X |
Delete a character before the cursor |
D{motion} |
Deletes the text represented by the specified action command |
Dd |
Delete When moving forward |
D |
Deletes the cursor to the end of the current line line. Equivalent to d$ |
Case switching
Definition: Specifies the text case toggle.
Grammar:
operator |
Implementation features |
~ |
Toggle character capitalization under cursor |
~~ |
Toggles the case of the current line |
G~{motion} |
Toggle the case of the text represented by the action command |
Gu{motion} |
Change the text represented by the action command to lowercase |
Gugu |
The current line becomes lowercase |
Gu{motiong} |
To capitalize the text represented by an action command |
Gugu |
The current line becomes uppercase |
Copy, paste
Definition: Copy text to register.
Grammar:
operator |
Implementation features |
Y{motion} |
Copy the action command to represent the text to the Register |
Yy |
Copy Current line to register |
Y |
Copies the current line to the register, which is equivalent to YY. I don't want to use shift without pressing the SHIFT key. So generally directly with YY |
Definition: Paste the copied text out.
Grammar:
operator |
Implementation features |
P |
Pastes the copied text after the cursor. If the row is copied, it is placed below the current line, and the cursor rests on the first line of the copied text |
P (SHIFT+P) |
Pastes the copied text before the cursor. If the row is copied, it is placed above the current line, and the cursor rests on the next line of the copied text |
Gp |
Pastes the copied text after the cursor. If the row is copied, it is placed below the current line, and the cursor rests on the first line of the copied text |
GP (SHIFT+P) |
Pastes the copied text before the cursor. If the row is copied, it is placed above the current line, and the cursor rests on the next line of the copied text |
Precautions:
- There is an operator with G, which should be noted if the row is copied. Plus g The last cursor stays on the next line of the copied text, without g The last cursor stays in the first line of the copied text.
Indent in
Definition: Indentation for text. Indentation is for rows, so it is the indentation of the corresponding line for the text action selection.
Grammar:
operator |
Implementation features |
>{motion} |
Indents the action command text to the right represents the line |
>> |
Indent left as you go forward |
<{motion} |
Indents the action command text to the left represents the line |
<< |
Indent left as you go forward |
Folded
Fold up the text. At this point, the operation collapses to one row.
This is not currently involved, the feeling of temporary application will not be too much, if useful to follow-up will be added.
Action Commands
The range that is used to select the text to manipulate.
All basic action commands can be preceded by a quantifier {count} indicating repetition, not a description.
Details can be viewed: H motion,:h usr_03.txt,:h xx (Action command).
From the current cursor to the next cursor up or down
Grammar:
Action Commands |
Action Description |
H |
Move one character to the left |
L |
Move one character to the right |
J |
Move down one actual row |
Gj |
Move down one screen line |
K |
Move an actual row up |
Gk |
Move up one screen line |
Precautions:
- G for distinguishing between actual lines and screen lines
Row positioning
Grammar:
Action Commands |
Action Description |
0 |
Move to the beginning of the actual line |
G0 |
Move to the beginning of the screen line |
^ |
Move to the first non-whitespace character of the actual row |
g^ |
Move to the first non-whitespace character of a line on the screen |
$ |
Move end of line to actual line |
g$ |
Move to the end of the line on the screen |
gm |
Move to the middle of the screen line |
+ |
Move to the first non-whitespace character at the beginning of the next line |
- |
Move to the beginning of the previous line a non-whitespace character |
Precautions:
- The difference between 0 and ^ is non-whitespace characters.
- G for distinguishing between actual lines and screen lines
Row Lookup
Grammar:
Action Commands |
Action Description |
F{char} |
Find characters from right to left. The cursor rests above the found character. |
F{char} |
Finds characters from left to right. The cursor rests above the found character. |
T{char} |
Finds characters from left to right. The cursor rests on a character after the found character. |
T{char} |
Finds characters from left to right. The cursor rests on a character before the found character. |
; |
Repeat the last row lookup (f,f,t,t) |
, |
Go back to the location of the last line lookup |
Precautions:
- Capital letters are right-to-left, lowercase letters are right-to-right
- The F system is the cursor resting on the found character above, and the T series is the cursor resting on the found character near a character.
Line spanning
Grammar:
Action Commands |
Action Description |
G |
The last line of the file |
NG |
Jumps to the nth row of the file. |
Gg |
The first line of the file, equivalent to 1G |
n% |
Jumps to the nth line of the file. |
Word movement
Grammar:
Action Commands |
Action Description |
W |
Move from left to right to the beginning of the next word |
W |
From left to right, move to the beginning of the next big word. |
B |
Right-to-left if the cursor is not at the beginning of the word. Then move to the beginning of the word. If you move to the beginning of the next word in the beginning of the word. |
B |
Right-to-left if the cursor is not at the beginning of the word. Then move to the beginning of the word. If you move to the beginning of the next big word in the first word. |
E |
From left to right, if the cursor is not in the ending, move to the ending of this word. If you move to the next word ending in the ending |
E |
From left to right, if the cursor is not in the ending, move to the ending of this word. If you move to the next big word ending in the ending |
Ge |
From right to left, move to the next word ending |
GE |
From right to left, move to the next big word ending |
Precautions
- Uppercase means word, lowercase for word
Text movement
Grammar:
Action Commands |
Action Description |
) |
Move the next sentence to the beginning. |
( |
Move the top of a sentence. |
} |
Move the blank line in front of the next paragraph segment |
{ |
Move a blank line before the first paragraph segment |
]] |
Move to the beginning of the next line with "{" as the first character. If you combine the operator, the previous line of "{" is left. |
[[ |
Moves to the previous beginning with "{" as the first character. If you combine the operator, the line that stays on the previous "{" |
][ |
Move to the next lines with "}" as the first word. If you combine operators, leave a row on "}" |
[] |
Move to the previous lines with "}" as the first word. If you combine operators, stay on the previous "}" line |
Mark
Tags have two types of symbols ' (single quotes), ' (reverse-skimming keys). ', which means to navigate to the beginning of the line. ', which indicates the first non-blank place to navigate to the beginning of a line. There are two types of commands followed, so do not say "only" to do the syntax instructions.
Grammar:
Action Commands |
Action Description |
M{A-ZA-Z} |
Set tag |
‘[ |
Jumps to the first line position of the last modification or copy of the text |
‘] |
Jumps to the last line where the text was recently modified or copied |
' > |
Jumps to the last line position of the most recent visual selection |
' < |
Jumps to the first line position of the last visual selection |
"(two single quotes) |
Jump to the last top position |
‘" |
Jumps to the last position of the file once opened |
' ^ |
Jump to the point where the cursor exits the insertion mode |
‘. |
Skip to the last file modification place |
]‘ |
Jumps to the previous lowercase letter mark (by: Marks view) place |
[‘ |
Jumps to the next lowercase letter Mark Place |
Precautions:
- There are some meaningless, not listed. '), '}, ' (, ' {= (,), {,} ').
- It should be noted that: with the cmd-line: Marks. View all tags.
Details can be viewed: H marks.
Skip
A skip, specifying is to use the following command: "'", "'", "G", "/", "?", "N", "n", "%", "(", ")", "[[", "]]", "{", "}", ": S", ": Tag", "L", "M", "H" and other commands
Grammar:
Action Commands |
Action Description |
Ctrl+o |
Jumps to the previous cursor position |
Ctrl+i |
Jumps to the next cursor position |
Ng |
Skip to the previous N-time modifications |
Ng |
Change the span to n times after |
Precautions:
- With Cmd-line under: Jumps, see all skip.
- With Cmd-line: changes, see all modified skip.
Details can be viewed: H jumps,: H changes.
Variable
Grammar:
Action Commands |
Action Description |
% |
Find a match ([{}]) |
[( |
Find a mismatch ( |
[{ |
Find the previous mismatch { |
]m |
Skip to Next method |
] Symbol (#,*,/) |
Jumps to the next symbol position |
Select range text text from current cursor selection
Once the action command is executed, it is equivalent to the area where the selection is executed.
The difference between a and I. I: Inner refers to the text within the region; A: Represents a, which refers to a complete range of text content.
The following is only the name of the area, not all written out, are in pairs appear.
Grammar:
Action Commands |
the text area represented by |
S |
Sentence |
P |
Paragraph |
Or |
A "]" block |
Or |
A "(" Block |
Or |
A "{" Block |
B |
Equivalent to a "(" Block |
B |
Equivalent to a "{" Block |
> or < |
A "<" block |
T |
A tag blocks, equivalent to <aa></aa> |
" |
A "" "Block |
‘ |
A "'" Block |
Details can be viewed: H object-select.
Roll screen
Displays the current window scrolling text content.
Z{COUNT}CR (Enter). Sets the height of the window.
Details can be viewed: H scroll.text,: H xx.
Up and down scrolling screen
Grammar:
Action Commands |
Action Description |
Ctrl+e |
Line down. Keep your cursor up until you reach the top of the screen, scrolling through the contents |
Ctrl+y |
Line up. The cursor goes down until the last line of the screen is scrolled, the content |
Ctrl+d |
Half-screen down. The cursor is unchanged in the relative position of the screen, scrolling is the content |
Ctrl+u |
Half-screen upward. The cursor is unchanged in the relative position of the screen, scrolling is the content |
Ctrl+f |
Down one screen. The cursor is always on the first line, and the content is scrolled. |
Ctrl+b |
Up one screen. The cursor is always on the last line and scrolls through the contents. |
The screen does not move the cursor movement
Description: The contents of the screen do not scroll, the cursor moves.
Grammar:
Action Commands |
Action Description |
H |
Cursor is positioned to the top of the current screen |
M |
Cursor navigates to the middle row of the current screen |
L |
Cursor is positioned to the top of the current screen |
Scroll one screen based on the cursor
Description: The cursor in the original physical line position does not move, the contents of the screen scrolling maximum value of a screen.
Grammar:
Action Commands |
Action Description |
Zt |
The cursor is placed on the first line of the screen, and the cursor column is the same as before moving. |
z+ |
The cursor is placed on the first line of the screen. Equivalent to Z<CR (enter), the cursor is at the beginning of the line |
Zz |
The cursor is placed on the screen in Chinese, the cursor column or the same as before moving. |
Z. |
The cursor is placed on the screen Chinese, the cursor is beginning |
Zb |
The cursor is placed at the bottom of the screen, and the cursor column is the same as before moving. |
Z- |
The cursor is positioned at the beginning of the screen |
Precautions:
- Z, if followed by a letter, stay in the original position, if the character stays at the beginning of the line
Details can be viewed,: H Z
Arithmetic operations
{count}ctrl+a addition
{Count}ctrl+x Subtraction
Useful when doing some digital processing and correction. For example
{ background-position:}//At this time, if the normal operation, f0s180<esc>//faster operation is to use arithmetic operations, directly 180ctrl+a
Macro
Grammar:
Define Macros:
1, Q{name}
2. Recorded commands
3, Q
Using Macros:
${count}@{name}
// to generate a 100-row serial number // Enter text first 1 , // Start Recording // Qayyp (Ctrl + a) Q // Execute Macro // [email protected]
Little Tricks
These tips are in the "Vim practical skills", if you are interested in further reading study.
Construct repeatable modifications
Suppose the cursor is on the character "H" at the end of the line, and we want to delete the word "nigh": the End Is nigh
We tried 3 different ways to delete a word: dbx, BDW, and Daw. Their number of golf (key operation number) is 3, which is the most repetitive? We use the. Command for all three cases.
DBX consists of two steps: The DB command is deleted to the beginning of the word, and the x command deletes a character. If we follow it one time. command, it will repeatedly delete a character (. = = x). I don't think it's worth anything.
The BDW contains two steps. This time, B is just a normal move, and the DW completes the modification. Used at this time. The command repeats the DW, removing the contents from the cursor position to the beginning of the next word. But since we're just at the end of the line, there's no "next word", so in this scenario. The command is of no use. However, at least it represents a longer operation (. = = DW)
Daw only calls one operation: Daw. This operation not only removes the word, it also deletes a space, so the cursor will eventually stop at the last character of the word "is". If we use this at this time. command, which repeats the last command to delete a word. This time. The command will do something really useful (. = = Daw).
Conclusion: Daw can be played. The maximum power of the command, so I declare it to be the winner of this round.
If you find yourself making the same small changes in several places, you can try to construct your modifications so that they can be. command to execute repeatedly. It takes some practice to identify such opportunities, but once you develop a habit of making changes repeatable, you get "rewards" from Vim.
Can repeat, do not use the number of times
When dealing with certain tasks, the number of keystrokes can be minimized, but we do not have to. We need to think carefully about the number of times and the pros and cons of repeating each.
More than one word we want to change this text to "delete word", that is to say, delete two words as described in this paragraph.
Our 3 choice of d2w, 2DW or DW. It's a 3-time button, but which is the best one?
According to our discussion, d2w and 2DW are the same, after executing either of the two, we can press the U key to undo, so that two deleted words will return. Or, instead of undoing, we use the. command to repeat it, which removes the next two words.
For DW. , press U or. The result will be slightly different. The change here is DW, which deletes a word. Therefore, if you want to recover these two deleted words, you must undo two times, press UU (or, if you prefer, you can press 2u). Press. Then delete only one word later, instead of two.
Now suppose we wanted to delete 3 words instead of 2. We performed d2w instead of d3w because we judged something wrong, so what do we do next? We can't use it. command, because that will delete 4 words altogether. Therefore, we either undo the number of corrections (ud3w) or continue to delete the next word (DW).
Now consider another scenario, if we are using DW in the first place. command, then we just need to repeat it one more time. The command is on the line. Because our initial modification is simply the DW, so the U command and. Commands have finer granularity, which only works on one word at a time.
Now suppose we want to delete 7 words, we can run d7w, or DW ... (That is, the DW is followed 6 times. command). It is very obvious which command wins when calculating the number of keystrokes. But are you really sure you've counted the right number? The number of calculations is annoying, so I'd rather press 6 times. command, nor do you want to reduce the number of keystrokes,
and waste the same time to count. If I press it more than once. What about the order? It doesn't matter, just press the U key once to return back.
Use as many times as necessary
Suppose we want to change the words "I have a couple of questions" to "I have some more questions"
In this scenario, use the. The meaning of the command is not big, we can delete a word and then use it. command to delete the other, but then we have to switch to insert mode (for example, using I or CW). It is not easy for me to do so, but I prefer to use the number of times.
Another benefit of using the number of times is that it retains a clean, consistent undo history. After this modification, we can undo the entire change by pressing the U key.
Delete perimeter, modify internal
Let's say we want to delete the word "excellent" in the next sentence, and then use the DAW command:
This command deletes the word, plus a space, so the result will be clean. If we use DIW, then there will be two of them after the deletion, which may not be what we want.
Now suppose we want to change this word to another word, this time with the CIW command:
The CIW command only deletes the word, not the white space character before and after it, and then it goes into insert mode, which is exactly what we want. If CAW is used, then the last two words will be joined together and become "mostadjectives". It's easy to fix, but wouldn't it be better if you could have avoided the problem from the start?
In general, the d{motion} command works well with the AW, as, and AP, while the c{motion} commands work better with IW and similar text objects.
Rookie Vimer Growth Record--chapter 2.1, Normal mode