问题描述
在 vim 中,我经常想要搜索需要转义的字符串的字符串。有没有办法,可以减少所有特殊字符的含义,例如 regex-off 模式或 fgrep ?
In vim, I often want to search on a string with finicky characters which need escaping. Is there a way I can turn off the meaning of all special characters, kind of like regex-off mode in less, or fgrep?
我正在处理特别毛茸茸的字符串;这里是一个例子:
I am dealing with particularly hairy strings; here's an example:
((N/N)/(N/N))/N
不必转义任何字符在vim中进行搜索将是一个主要的时间。
Not having to escape any characters to do a search in vim would be a major timesaver.
\V in Vim 有助于某些元字符,但是不能/或\。
\V in Vim helps with some metacharacters, but critically not / or \.
感谢所有!最后,我将其添加到我的.vimrc中:
Thanks all! In the end, I added this to my .vimrc:
command! -nargs=1 S let @/ = escape('<args>', '\')
nmap <Leader>S :execute(":S " . input('Regex-off: /'))<CR>
推荐答案
看看你的具体例子,可能是最简单的方法这样做(因为\V在这里不会帮助)是使用?
而不是 /
:那么你赢了不得不逃避 /
s:
Looking at your specific example, probably the easiest way to do this (since \V won't help here) is to use ?
instead of /
: then you won't have to escape the /
s:
?((N/N)/(N/N))/N
这将向后搜索而不是向前搜索,但是你可以随时做'N'而不是'n'在第一次去之后向前搜索。或者您可以按 /
,然后按向上光标键自动退出正斜杠。
This will search backwards instead of forwards, but you can always do 'N' instead of 'n' to search forwards after the first go. Or you can press /
followed by the up cursor key to automatically escape the forward slashes.
然而,没有什么您可以轻松地执行转义反斜杠。我想你可以做:
However, there's nothing you can easily do about escaping backslashes. I guess you could do:
:let @/ = escape(pattern, '\')
然后使用 n
进行搜索,但可能不会那么容易。我可以想出的最好的是:
and then use n
to search, but it's probably not much easier. The best I can come up with is:
:command! -nargs=1 S let @/ = escape('<args>', '\')
然后做:
:S (N)/(N+1)\(N)
n
这篇关于vim中的精确字符串匹配? (像'regex-off'模式更少)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!