The Cmd-line mode also has 3 types: the EX command (ex commands), the lookup mode (search patterns), and the filter command (filter commands). The main focus of this paper is the ex command and search patterns. The Filter command temporarily uses less of the scene if there is a more appropriate scenario to add.
Objective
Mastering the syntax and concepts of common operations in Cmd-line mode
Introduction to the Ex command
At first, there is ed,ed as the father of the ex, the father of the VI, and VI as the father of Vim. The importance of the ex command is visible.
The ex command has a wide range of effects and is far away. Compared to the text operation in normal mode, it is appropriate to operate locally (or, once, by shortcut keys).
The general operation is range ({range}) + action.
VIM provides the corresponding ex command for almost all functions. Below is a brief list of several
Command |
Use |
: [Range]delete [x] |
Delete rows in the specified range [into register x] |
: [Range]yank [x] |
Copy the specified range of rows [into register x] |
: [Line]put [x] |
Pastes the contents of the register x after the specified line |
: [Range]copy {address} |
Copies the rows in the specified range below the line specified by {address} |
: [Range]move {address} |
Moves the row within the specified range below the line specified by {address} |
: [Range]join |
Connecting rows within a specified range |
: [Range]normal {commands} |
Executes the normal mode command {commands} for each row in the specified range |
: [Range]global/{pattern}/[cmd] |
Executes the ex command {cmd} on all rows within the specified range that match {pattern} |
: [Range]substitute/{pattern}/{string}/[flags] |
Replace {string} where {pattern} appears in the specified range |
Concept:
- [Range] refers to the range. Below will involve
- [x] Indicates the name of the register device. i.e. [a-za-z]
- {address} represents the addresses.
- {pattern} corresponds to the following pattern
- [CMD] represents an action
- {string} string
- [Flags] Tag
- {Commands} represents a series of actions in normal mode
To learn more, see: H ex-cmd-index.
Move cursor
How to move the cursor more quickly in Cmd-line mode. This is typically done to correct an input error.
The syntax is as follows:
Command |
Use |
Ctrl+b |
Move to the end of the command |
Ctrl+e |
Move to the beginning of a command |
Ctrl+<left> |
Move one word to the left |
Ctrl+<right> |
Move one word to the right |
<Left> |
Move one character to the left |
<Right> |
Move one character to the right |
Delete
When moving, the ctrl+b,ctrl+e is generally used only. If this is the wrong time, before the insert mode also said, the best way is to delete the re-output. So here's an introduction to the shortcut keys for deletion
Command |
Use |
Ctrl+u |
Delete to beginning of line |
Ctrl+w |
Delete a word |
Shortcut key Mapping
If you are still more accustomed to moving the cursor, you can map the keys of the non-primary key area to the combination of the primary key area
This setting, you can according to their own habits to set. I generally only set the word movement, and then if the error is lost directly delete the re-output.
Historical records
View the history of the command.
The syntax for viewing history is as follows
Command |
Use |
Ctrl+p |
Previous command |
CTRL + N |
Next command |
Command Window
The command-line window is like a regular Vim buffer, except that each line of its contents corresponds to an entry in the command history. We can use the K and J keys to move forward or backward in history (like a separate action window, you can use all the normal mode commands), or you can use Vim's find function to find a row. When the <CR> key is pressed, the contents of the current line will be executed as ex-life
Open Command Window syntax
Command |
Use |
q/ |
Open command-line window to find command history in normal mode |
Q: |
Open the command-line window of the Ex command history in normal mode |
<Ctrl-f> |
Switch from Cmd-line mode to command-line window |
For example
# Let's say we're writing a simple ruby script, and then we find that each time we make a change, we execute the # two commands: #: Write#:!ruby%# After several successive executions of these two commands, we realized that we could simplify the work process and put the two command # together. This allows you to select the complete command from history and execute it again: #: Write |!ruby%# These commands are already in history, so we don't have to enter the entire command from scratch. But how do you combine the two records in history # into one? # Follow the steps below to Q:kj (shift+j) <CR>
Auto-complete
Grammar of the complement
Command |
Use |
tab |
As in the shell, you can also use the <Tab> key to automatically complete commands on the command line. |
Shift+tab |
To traverse the complement list backwards |
Ctrl+d |
The command will let vim display the list of available complements. This is very useful. |
CTRL + A |
Complete all matching lists |
Ctrl+l |
There is only one matching time. I don't think it's very useful |
<C-r><C-w> |
Inserts the current word into the command line |
<c-r>{register} |
The contents of the register can be inserted into the command line |
For example
Repeated execution
Repeating the previous Ex command in normal mode is very simple, just press @:.
Precautions
Range
Many Ex commands can use [range] to specify the range to be manipulated. We can specify the starting and ending positions of the range using line numbers, position markers, or find patterns.
When you define a [range], it always represents a series of contiguous rows, but the global command can also execute an ex command on a series of noncontiguous rows
Specify a range with an address
Refers to the above {address}.
Address types are as follows
symbols |
Address |
0 |
The virtual row, located above the first line of the file. |
N |
The nth line of the file |
$ |
The last line of the file |
. |
The line where the cursor is located |
' m |
Row containing the position mark M |
% |
Entire file (: abbreviated form of 1,$) |
Precautions:
Line No. 0 does not exist in the file, but it is useful as an address in some specific scenarios. In particular, when copying or moving rows within a specified range to the beginning of a file, you can use it to do: copy{address} and: The last parameter of the move {address} command.
- In many cases not writing [range] is the default when moving forward
- : {Start},{end} represents a broader scope. where {start} and {end} both represent {address}. For example:., $ represents the current line to the end of the file.
Special scenes:
- If you enter an ex command that contains only numbers, Vim parses the number into an address and moves the cursor to the line specified by the number. For example: 3 jumps to line 3rd.
Specify a range with a highlighted selection
When the highlight is selected, the selected range is triggered when the: is pressed. At this point the ex command will show ": ' <, ' >".
' <, which indicates the beginning of the highlight selection, ' > Indicates the end of the highlighted selection.
Specifying a range with a pattern
Vim also accepts the address of the mode as an Ex command. :/{pattern1}/,/{pattern2}/.
For example:
This range looks more complex, but it actually conforms to the general form of the scope: {start},{end}. In this example, the {start} address is a pattern/
In this case, the address: 2,5 can also get the same result, and the presentation is more concise, but it's also more unreliable. When you specify a range with a pattern, our command always operates on the entire
Correcting the range of addresses with offsets
The above example assumes that we want to run an Ex command for each row that is located between to include the lines of
The general form of the offset is this: ": {addr}+n".
{addr} can be either an {address} or a lookup mode. Can be +n, or minus-1. "+" indicates a downward offset, "-" indicates an upward offset
Action Copy
Syntax:: [range]t{address} t== (T|copy|co)
For example:
Precautions:
In the table above, you can also change the YYP to copy the rows we want, but in any case, this requires some extra movement. We have to jump to the line that you want to copy (6G), copy the line (yy), quickly jump back to the original position (<C-o>), and then use the Paste command (p) to create a copy. This shows that the T command is often more efficient when replicating distances that are far away.
Move
Syntax:: [Range]m[ove]{address}
The method of use is very similar to reproduction, and it is not only explained.
Show
You can use this action when you want to show the match to something.
Syntax:: [Range]p[rint]
Connection
You can use this action when you connect text to a line.
Syntax:: [Range]j[oin]
Delete
Syntax:: [range]d[elete][x]
You can put the deleted content in the Register X.
Read
Read text to current cache
Grammar 1:[range]r[ead]! {cmd} here {cmd} refers to an external command
Syntax 2:[range]r[ead] {filename} here {filename} refers to the file name
Precautions:
- The [range] here will only take the last position if it is a range.
Write
Write the current cache somewhere else
here {filename} refers to the file name
The syntax is as follows
Command |
Use |
: [Range]w[rite] {filename} |
Writes the text of the range to the specified file. If the file exists, it cannot be overwritten directly |
: [range]w[rite]! {filename} |
Writes the text of the range to the specified file. If the file exists, overwrite it directly |
: [Range]w[rite] >> |
Appends the text of the range to the end of the current file. However, the window does not flush out. |
: [Range]w[rite] >> {filename} |
Appends the text of the range to the specified end of the file. |
: [Range]w[rite]! {cmd} |
Call command with range text as standard input text |
Precautions:
- If there is no [range] and {filename}, it is equivalent to overwriting the current file
Normal
Executes a normal mode command on a series of contiguous rows. This command is in the with. When used in combination with commands or macros, we can do a lot of repetitive tasks with minimal effort.
Syntax:: [range]normal {commands}
Precautions
- {commands} is a command in normal mode
For example
#想给所有行添加注释 # using normal:% normal i# #使用替换:%s/^/#/g
Global
Combines the ex command with the vim pattern to match both of these capabilities. With this command, we can run the ex command on all matching rows in a specified pattern. The global command is one of the most powerful vim tools in addition to point paradigms and macros, in terms of efficiency in dealing with repetitive tasks.
Syntax:: [Range] global[!]/{pattern}/[cmd]
Precautions
By default, the global command is scoped to the entire file (%), which differs from most other ex commands (including:d elete,: Substitute, and: normal), which have a default range of only the current line (.).
- Inverse use: Vglobal or shorthand: v. You can use this command when you want to operate on something that does not match. For example, do not match all delete:: v/{pattern}/d
- :p rint: The default [cmd] of the global command
Find
Syntax: (/|?) {Pattern}
Direction
- Start with/to find backwards
- To start with, look forward.
Repeat Lookup
- n continue to look for the same direction
- N take the opposite direction to find
Shortcut keys for turning off highlighting
: Noh <CR> Although we can disable the Find highlighting feature, we also took a lot of effort on the keyboard. By creating a mapping entry, you can let us speed things up.
For example:
Nnoremap <silent> <C-l>:<c-u>nohlsearch<cr><c-l>
<C-l> is typically used to clear and redraw the display (parameter: H ctrl-l). The new mapping term adds the ability to temporarily turn off lookup highlighting on top of the original base.
This one is very sensitive to use.
Preview the first match before performing a find
Navigate to the first matching position each time you find it. Press ESC to return to the original position.
Open syntax:: Incsearch
Can be used to check if a match exists.
For example
What if we just want to confirm that the word "carrot" appears in the current document but doesn't want to move the cursor? When the ' incsearch ' option is enabled, we simply bring up the Find prompt and enter as many characters as possible to make up the word "carrot" until the word is first reflected in our eyes. Once the word is found, we simply press <Esc> to end the search and return to the original position, thus avoiding interrupting our thinking.
Auto-complete lookup fields based on preview results
This method automatically complements the lookup field with the matching results of the current preview. The word is very useful when it is long!! Recommend
Grammar:<c-r><c-w>
For example
#文本里有一个单词like.
#查找步骤/li<C-r><C-W>#此时会显示/like
Offsets the cursor to the end of a find match
The cursor is positioned at the end of the search for matching characters. If not added, it will be placed in the starting position.
Syntax:/{pattern}/e
Precautions
- Here the "/" can be replaced with "?", but two "/" must be changed. Just like this:? {Pattern}?e
Find the currently selected text
Can be implemented via registers.
The steps are as follows:
- Select the text you want to find in visual mode
- Then use Y to copy to the register "".
- Last input/<c-r> "<CR>
Replace
Syntax:: [Range]s[ubstitute]/{pattern}/{string}/[flags]
Reusing the last lookup mode
The Execute substitute command usually consists of two steps: one is to compose the lookup pattern, and the other is to design the appropriate replacement string. As a result, the two-way technology allows us to eliminate the coupling of both tasks, which is the key.
Leaving the lookup domain of the substitute command blank means that VIM will reuse the last lookup mode. We can use this feature to streamline our work processes.
For example
# Look at this huge substitute command:%s/\v'(([^']|'\w) +)'/“\1”/g# It is equivalent to the following two separate commands:/\v'(([^']|'\w) +)':%s//"\1"/g# in the process of writing complex regular expressions, it is often necessary to try multiple times to achieve the correct matching effect. # If you're going to validate the pattern by executing the substitute command, it's too much trouble to do it every time the command changes the contents of the document. # in stark contrast, the document is not modified when the Find command is executed. So it doesn't matter if we make more mistakes # Please note that this is generally used for complex matching. If it's a simple match, don't do it. This simplicity is due to a person's proficiency in the regular. # for example,5, 10 lines plus comments, this simple sentence can be solved. :5, 10s/^/#/g
Replace with the contents of the Register
Input into <c-r>{register}, we can insert the contents of the register into the command line
Mark
{flags}. Different tags affect the behavior of the substitution.
Specific functions are as follows
character |
Behavior |
& |
Keep the previous replacement tag |
C |
Need to be confirmed |
E |
If there is an error, the error message is not prompted. I haven't found a better scene yet. |
G |
Action on all matching content |
I |
Ignoring case-sensitive letters |
I |
Uppercase and lowercase letters sensitive |
N |
Statistics count, no replacement content is performed. |
P |
Prints out the last matching content. If there is a setting to display the line number, then the line number is displayed without setting |
# |
Prints the last match and displays the line number. |
L |
Prints the last match, but prints the same content as: list. As I understand it, it's more than print out escape characters. |
Skills:
Count the number of matches in the current pattern
/{pattern} #这个就是我们要统计的模式:%s///gn #抑止执行
Mode
That is {pattern}.
Let's focus on the core that drives their operation, the VIM search engine. Have you ever wondered how Vim's regular expressions work? Or how to turn them off?
Vim's regular expression engine may be different from the other engines you are accustomed to. We will see that the most confusing differences can be easily resolved by very magic mode switches.
Case sensitive
The general default is case-sensitive. If you need to force it, add a flag after the string.
- \c (lowercase c), which indicates case insensitivity
- \c (uppercase C), indicating case sensitivity
For example
#查找单词"word", case insensitive/word\c# find word "word", Case Sensitive /word\c# replace wordword with "me", case insensitive: %s/word\c/me/g# replace word Word with "me", Case sensitive :%s/ Word\c/me/g
Vim's regular
The grammatical style of the Vim regular expression is closer to POSIX than Perl.
However, using the very magic mode switch allows Vim to use the regular expression syntax that we are more familiar with.
The default is to use the Magic mode. The very magic mode can be used with \v (lowercase v). The nomagic mode can be very by \v (capital V).
For example
#假设我们要构造一个正则表达式 to match each set of color codes in the following CSS fragment: body {color: #3c3c3c;} A {color: #0000EE;} Strong {color: #;} Find the hexadecimal color code for the #用magic search mode /#\ ([0-9a-fa-f]\{6}\|[ 0-9a-fa-f]\{3}\) #用very Magic Search mode find hex color code /\v# ([0-9a-fa-f]{6}|[ 0-9a-fa-f]{3}) #用十六进制字符类进一步优化模式/\v# (\x{6}|\x{3})
Precautions
- In this example, we use the 3 class brackets in the Magic mode. The square brackets default has a special meaning, so you don't have to escape. The parentheses match the literal (and) characters, so they need to be escaped so that they have a special meaning. Curly braces also need to be escaped, but we only have to escape the opening brackets, and the corresponding closing brackets don't, because vim guesses our intentions. The parentheses are different, regardless of the opening and closing brackets that must be escaped.
Magic Mode
The pattern automatically assigns special meanings to some additional symbols, such as:. , *, and square brackets. The Magic mode is designed to make it easier to construct simple regular expressions, but it does not work for things like +,? , parentheses, and curly braces are given special meanings, and the symbols must also be escaped to have special meanings.
Nomagic mode
Not very clear for the time being???
Very magic Mode
The switch makes up for this, except for _, numbers, and letters, which give special meaning to all symbols. In this way, it is well-remembered and coincides with the rules of the Perl regular expression.
Very nomagic mode
The special characters used in regular expressions are handy when searching by pattern, but they become a hindrance if we want to find text by literal means. Using the very nomagic switch, you can eliminate the attached in., * and? And so on most characters of special meaning.
For example:
#文本内容The N Key searches backward......the \v pattern switch (a.k.a. Very magic Search) ... #现在假设我们想通过查找 "a.k.a." (this abbreviation represents also known as) moves the cursor to that place. In this case, the first response is to perform the following lookup command:/a.k.a. #实质上我们需要这么才能得到效果/a\.k\.a\.<cr>#或者, we can use the \v switch to activate very Nomagic Search mode:/\VA.K.A.
Precautions
- As a general rule, if you want to find the regular expression, you use the mode switch \v, and if you want to find the text according to the original meaning, use the original switch \v. Pay special attention to this.
Special characters
Special characters are more easily confused when writing in several modes.
A list of special characters for several modes is as follows:
Magic |
Nomagic |
Very Magic |
very nomagic |
Match Content |
$ |
$ |
$ |
$ |
End of Line |
. |
. |
\. |
\. |
Any character |
* |
* |
\* |
\* |
Any number |
() |
\(\) |
\(\) |
\(\) |
Group |
| |
\| |
\| |
\| |
Separator |
\a |
\a |
\a |
\a |
Character of the alphabet |
\\ |
\\ |
\\ |
\\ |
Back slash |
\. |
\. |
. |
. |
Character points |
\{ |
{ |
{ |
{ |
Characters |
A |
A |
A |
A |
Character A |
Match and highlight
When we talk about a pattern, we refer to a regular expression (or text that matches verbatim) that is entered in the lookup field, whereas a match is the text highlighted in the document (assuming that the ' hlsearch ' option is enabled). Matching and highlighting are two things.
There is such a thing, but temporarily did not feel any use, tried for a while did not realize the use of the scene.
Grammar:
- /zs Highlight Start
- /ze highlighting results
Run the shell command
We can easily invoke external programs without having to leave Vim. Even better, we can send the contents of the buffer as standard input to an external command, or import the standard output of the external command into the buffer.
When operating in Vim, it is easy to invoke the shell command. The following table selects some of the most useful ways to invoke external commands
Command |
Use |
: Shell |
Start a shell (enter exit to return to VIM) |
:! {cmd} |
Execute {cmd} in Shell |
: Read! {cmd} |
Execute {cmd} in the shell and insert its standard output below the cursor |
: [Range]write! {cmd} |
Execute {cmd} in the shell, with [range] as its standard input. This is not a trial yet, okay? |
: [range]! {Filter} |
Use external program {filter} to filter the specified [range] |
There are several types of running shell commands:
Disposable external
Applies to executing a one-time command.
Grammar::! {cmd}
Interaction
Applies to several commands that you want to execute in the shell.
Syntax:: Shell
Precautions
- To exit, enter exit to return to VIM
Filtering buffer content with external commands
Syntax:: [range]! {Filter}
For example
#把当前文本排序一下:%! Sort
REDIRECT the standard output of a command to a buffer
Syntax:: [Range]read! {cmd}
For example
! Date
Precautions
- If there is no [range], it is inserted at the current cursor position
- If there is a [range], the range following the
Rookie Vimer Growth record--chapter 2.4, Cmd-line model