kelleyma49 / PSFzf

A PowerShell wrapper around the fuzzy finder fzf
MIT License
787 stars 34 forks source link

How can I ignore directories while searching? #97

Closed Accelx9 closed 2 years ago

Accelx9 commented 2 years ago

fzf version: 0.28.0 PSFzf version: 2.2.9

I would like to know how I can ignore multiple directories in PSFzf. For example: node modules, .git directories, etc.

I have tried creating a global .gitignore file via core.excludesFile but didn't work.

How can I do this?

Accelx9 commented 2 years ago

SOLVED:

For newcomers like me follow the next steps:

  1. Install fd or ripgrep.
  2. Create a global .gitignore file and add the directories you want to exclude there.
  3. Configure the FZF_DEFAULT_COMMAND adding one the following lines in your powershell profile:

For fd: $env:FZF_DEFAULT_COMMAND='fd --ignore-file .gitignore'

For ripgrep $env:FZF_DEFAULT_COMMAND='rg --files --hidden --no-require-git'

I hope this helps someone.

Merry Christmas.

breakersun commented 2 years ago

also can simply list directory names in FZF_DEFAULT_COMMAND like this: $env:FZF_DEFAULT_COMMAND='rg --files --hidden -g !.idea -g !__pycache -g !.git'

the trick here is the "-g !" option. you might want to take a look at the man pages of rg. rg --help ... -g, --glob ... Include or exclude files and directories for searching that match the given glob. This always overrides any other ignore logic. Multiple glob flags may be used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude it. If multiple globs match a file or directory, the glob given later in the command line takes precedence.

        As an extension, globs support specifying alternatives: *-g ab{c,d}* is
        equivalet to *-g abc -g abd*. Empty alternatives like *-g ab{,c}* are not
        currently supported. Note that this syntax extension is also currently enabled
        in gitignore files, even though this syntax isn't supported by git itself.
        ripgrep may disable this syntax extension in gitignore files, but it will
        always remain available via the -g/--glob flag.

        When this flag is set, every file and directory is applied to it to test for
        a match. So for example, if you only want to search in a particular directory
        'foo', then *-g foo* is incorrect because 'foo/bar' does not match the glob
        'foo'. Instead, you should use *-g 'foo/**'*.