为什么他们使用双星号?
我在 vimgrep
上阅读了 Vim 的帮助,我环顾四周,堆栈溢出和 vimcast,虽然我发现很多人说这是 怎么样你在当前目录递归搜索,我没有找到关于的解释为什么 .
让我们用一个例子来更容易解释。如果我想在当前目录下查找所有出现的 foo,我可以使用
:vim[grep][!] /{pattern}/[g][j] {file}
这样就变成
:vimgrep /foo/ **/*
所以看着
{file}
这个grep的一部分:*
)是通配符。 目录分隔符。
我的具体问题是为什么它以
**/*
并不是
*/*
我已经尝试在几种不同的情况下使用/进行搜索,这似乎是在距我当前目录正好 1 深的任何目录中搜索任何文件,我认为这就是使用双星号的原因。
即为什么它是一个双星号?这是否通过一些我不完全理解的巧妙机制表示“我希望您递归搜索”,或者这只是一个用于表示“递归搜索”的关键字?我是否完全关闭,这不是 vim 的内置部分,而是 shell 的一部分? (这些部分不是我的实际问题,而是有助于理解我的困惑,我的实际问题在上面)。
如果有什么方法可以改进我的问题,请告诉我,这是我第一次提问。
最佳答案
简答:
双星号是vim 内置关键字,它简单地表示“递归搜索”。
之所以使用它,是因为 vim 的创建者选择在这种情况下使用它。
更长的答案:
有两种不同的情况,其中 **
用于:文件搜索和其余部分。
Vim 帮助很好地解释了这一点。
对于“其余”,请参阅 :help wildcard
和 :help starstar-wildcard
:
来自 :help wildcard
:
** matches anything, including nothing, recurses into directories
和
:help starstar-wildcard
规定:Expanding "**" is possible on Unix, Win32, Mac OS/X and a few other systems. This allows searching a directory tree. This goes up to 100 directories deep.
有关文件搜索,请参阅
:help **
或 :help starstar
.引用相关部分(强调我的):
The file searching is currently used for the 'path', 'cdpath' and 'tags' options, for finddir() and findfile(). Other commands use wildcards which is slightly different.
[...]
Downward search uses the wildcards '*', '**' and possibly others
supported by your operating system. '*' and '**' are handled inside Vim,
so they work on all operating systems.
[...]
'**' is more sophisticated:
- It ONLY matches directories.
- It matches up to 30 directories deep by default, so you can use it to search an entire directory tree
- The maximum number of levels matched can be given by appending a number to '**'.
关于regex - 为什么用**/*递归搜索当前目录:vimgrep vim command?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51306648/