[英]What's wrong with this Vim mapping?
Inside a function I use to initialise some TeX related settings I have the following mapping defined: 在用于初始化某些与TeX相关设置的函数中,我定义了以下映射:
vmap <buffer> ucm :s/^\% //<CR>:nohlsearch<CR>
I expected it to allow me to easily uncomment visually selected lines. 我希望它可以使我轻松取消视觉选择的行的注释。 The analogous:
类似的:
vmap <buffer> cm :s/^/\% /<CR>:nohlsearch<CR>
does a pretty nice job in commenting. 在评论方面做得很好。 Also analogous mappings for other languages, which use a #, and not a % work just fine.
同样,使用#而不是%的其他语言的类似映射也可以正常工作。 Those last ones look like this:
最后的那些看起来像这样:
vmap <buffer> cm :s/^/# /<CR>:nohlsearch<CR>
vmap <buffer> ucm :s/^# //<CR>:nohlsearch<CR>
A sequence of V 10 j cm V 10 k ucm is supposed to leave the code intact. V 10 j cm V 10 k ucm的序列应该使代码完整无缺。
So now: What am I doing wrong? 所以现在:我在做什么错?
You are adding unecessary stuff. 您正在添加不必要的内容。
:s/^/% <CR>
and 和
:s/^% /<CR>
should work for commenting and uncommenting respectively. 应该分别用于评论和取消评论。
The third /
is used to add options such as /c
for "confirm" or /g
for "global". 第三个
/
用于添加选项,例如/c
表示“确认”, /g
表示“全局”。 If you don't use those options you don't need this /
at all. 如果您不使用这些选项,则根本不需要此
/
。
In your "uncomment" substitution you are escaping %
but %
in itself has no special meaning for Vim's regex flavour. 在“ uncomment”替换中,您转义了
%
但%
本身对于Vim的正则表达式味道没有特殊意义。 Not only Vim is certainly not going to match it if it's escaped but \\%<something>
is used for a bunch of atoms like \\%d
. 如果Vim逃逸了,不仅Vim肯定不会与之匹配,而且
\\%<something>
用于一堆原子,例如\\%d
。 So your pattern fails because Vim stumbles upon your \\%
expecting the rest of the atom and getting "nothing". 因此您的模式失败了,因为Vim偶然发现您的
\\%
期望原子的其余部分并“一无所获”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.