標籤:global vim
Meet The Global Command
The :global
command allows us to run an Ex command on each line that matches a particular pattern. Let’s start by studying its syntax. The :global command takes the following form (see :h :g ):
:global命令可以讓我們在每一行匹配特定pattern的文本上執行Ex命令。我們首先學習它的文法。:global命令形式如下
:[range] global[!] /{pattern}/ [cmd]
The default range for the :global command is the entire file (%). That sets it apart from most other Ex commands, including :delete, :substitute, and :normal, whose range is the current line (.) by default.
:global命令的預設range為(%) 也就是全文範圍,和其他的Ex命令如:delete,:substitude,:normal的預設range不一樣,這些命令的預設range為(.)當前行。
The {pattern} field integrates with search history. That means we can leave it blank and Vim will automatically use the current search pattern.
{pattern}可以和搜尋曆史整合。意味著我們可以把這一項留空,然後Vim會自動用當前的搜尋pattern.
The [cmd] could be any Ex command except for another :global command
[cmd]可以是除了:global命令的任何的Ex命令。
We can invert the behavior of the :global command either by running :global! or :vglobal (mnemonic: invert). Each of these tells Vim to execute [cmd] on each line that doesn’t match the specified pattern.
我們可以反轉:global的效果通過使用:global!
或:vglobal
.這些符號告訴Vim在每一條不匹配pattern的行上執行命令[cmd]
The :global command works by making two passes through the lines specified by [range]. In the first pass, Vim marks each line that matches the specified {pattern}. Then on the second pass, the [cmd] is executed for each marked line. The [cmd] can accept a range of its own, which allows us to operate on multiline regions.
:global命令執行時,由[range]指定的行將會被傳遞兩次。在第一次傳遞時,Vim標註那些匹配{pattern}的行,然後在第二次傳遞時,這些被標記的行執行[cmd]命令。[cmd]命令可以接受range本身,允許我們在多個行上執行命令。
[Practical.Vim(2012.9)].Drew.Neil.Tip97 學習摘要