niklaas
niklaas

Reputation: 93

How to search hidden directories/files with path=.,** and :find?

I have been using junegunn/fzf.vim to quickly find (hidden and non-hidden) files in vim. I just found out that it's also possible to set

path=.,**

and use :find some-file to achieve pretty much the same except fuzzy-searching. Since I rarely use fuzzy-searching and prefer to use as little plugins as possible (it's still a ton though), I am thinking about abonding fzf.vim and completely switching to the path/:find-approach.

That said, there is one important feature missing for me here. As far as I can tell, path doesn't include hidden directories. For example, given the following directory structure

~/testdir
❯ tree -a
.
|-- .hidden-folder
|   |-- some-hidden-file
|   `-- some1-hidden
|       `-- some2-hidden
|           `-- deeply-hidden-file
|-- some-file
`-- some-folder
    `-- some1
        `-- some2
            `-- deep-file

6 directories, 4 files

and opening vim some-file, :find deeply-hidden does not find the file. I assume this is because it is inside a .hidden-folder since :find deep-file is found.

Is there some way I can set ** to include hidden folders as well? Maybe it's also possible to tell vim to use a specific command for searching, so I can configure it to include hidden files/folders.

Upvotes: 6

Views: 3438

Answers (2)

P Varga
P Varga

Reputation: 20269

First of all, you probably want path=.,,** not path=.,**. . is the directory of the edited file, the empty string (between ,,) is the current folder - where the editor is running.

As for your question, like @romainl says in the other answer, unfortunately there isn't an option you can set that would cause ** to recurse into hidden directories as well.

But, you can sort of hack your way around this by setting path+=**/.*/**,**/.*/**/.*/**. This should help with :finding deeply-hidden-file, in your case. It's a "hack" because it finds a/b/.c/d/.e/f/g but won't find a/.b/.c/d/.e/f for example, because it's under 3 levels of hidden directories. So to get that to work you'd need to add path+=**/.*/**/.*/**/.*/**... as you will see that slows the search down a bit.


NB Only tried this in NeoVim, although it should work in Vim, too.

Upvotes: 0

romainl
romainl

Reputation: 196789

The built-in routine used for searching files uses a "depth-first search" algorithm which makes it quite inefficient in many scenarios. set path=.,** may thus be a bit excessive as it will force Vim to look into every subdirectory before switching to the next directory. It may work in some cases (as emphasized in my answer you linked to) but you should know that 'path' is intended as a list of specific directories and having ** in there kind of defeats its point. The "right" way to make :find go through hidden directories is to add them to 'path':

set path+=.some_dir
set path+=.some_other_dir

While it would certainly be an improvement over the current situation, replacing the current algorithm with a "better" one (say iterative deepening depth-first search) can only be done in Vim's C source as Vim doesn't expose a 'filesearchprg' option or similar, unfortunately.

Upvotes: 3

Related Questions