[英]How to specify the line under cursor in Vim?
I want to count number of occurrences of the line under the cursor. 我想计算光标下行的出现次数。 I intend to do it with the
我打算用它来做
:%s/pattern/&/gn
command. 命令。 So how do I specify the line under cursor in place of
pattern
? 那么如何指定光标下的线代替
pattern
呢?
You can insert the current line via the expression register . 您可以通过表达式寄存器插入当前行。 For a literal match, switch the regular expression to very nomagic mode (
\\V
), and escape any backslashes and the separator in the line: 对于文字匹配,将正则表达式切换到非常无意义模式(
\\V
),并转义行中的任何反斜杠和分隔符:
:%s/\V<C-r>=escape(getline('.'), '/\')<CR>/&/gn
Depending on what you want to count exactly, you may also need to anchor ( \\^...\\$
in very nomagic mode) the pattern. 根据您想要精确计算的内容,您可能还需要锚定(
\\^...\\$
以非常无条件模式)模式。
Instead of direct insertion via <Cr>
, you can also build the command via :execute
. 您也可以通过
:execute
来构建命令,而不是通过<Cr>
直接插入。 This is more suitable in a function. 这更适合功能。
:execute '%s/\V' . escape(getline('.'), '/\') . '/&/gn'
If you're looking for a canned plugin solution, my SearchPosition plugin can count occurrences. 如果您正在寻找一个预装插件解决方案,我的SearchPosition插件可以计算出现次数。 With it,
V<Am>
will show a summary like this: 有了它,
V<Am>
将显示如下摘要:
On sole match in this line, 8 following, 2 in previous lines; total 10 for /this line\n/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.