Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is **/* used to search recursively through current directory in :vimgrep vim command?

Why do they use the double asterisk?

I've read Vim's help on vimgrep, I've looked around on stack overflow and vimcasts and whilst I have found lots of people saying that this is how you search recursively in current directory, I haven't found an explanation as to why.

Lets use an example to make it easier to explain. If I wanted to find all occurrences of foo in my current directory and down, I could use

:vim[grep][!] /{pattern}/[g][j] {file}

so that becomes

:vimgrep /foo/ **/*

So looking at the {file} part of this grep:

  • I understand that it is a file path.
  • I understand that an asterisk (*) is a wildcard.
  • I understand that the forward-slash acts as a directory separator.

My specific question is why it comes in the format of

**/*

and not

*/*

I've tried searching using / in a few different cases and that seems to search any file in any directory exactly 1 deep from my current directory, which I assume is why double asterisk is used.

ie Why is it a double asterisk? Does this indicate "I want you to search recursively" through some clever mechanisms I don't fully understand or is this just simply a keyword that serves to say 'search recursively'? Am I completely off and this is not an inbuilt part of vim but a part of the shell? (these parts are not my actual question but rather serve to give understanding as to what I am confused about, my actual question is above).

If there are any ways I can improve my question, please let me know, it's my first time asking.

like image 978
Stun Brick Avatar asked Nov 08 '22 04:11

Stun Brick


1 Answers

Short answer:

The double asterisk is a vim build-in keyword, which simply says 'search recursively'. It is used, because the creator of vim choosed to use it for this case.

Longer answer:

There are two different cases, where ** is used: file searching and the rest.

Vim help explains this very well.

For the 'rest' see :help wildcard and :help starstar-wildcard:

From :help wildcard:

** matches anything, including nothing, recurses into directories

and :help starstar-wildcard specifies:

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.


For file searching see :help ** or :help starstar.

To quote the relevant parts (emphasis mine):

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 '**'.

like image 189
rkta Avatar answered Nov 12 '22 19:11

rkta