jhawthorn / fzy

:mag: A simple, fast fuzzy finder for the terminal
MIT License
2.98k stars 126 forks source link

how to exclude certain files/files a-la .gitignore? #113

Closed dmitry-saritasa closed 4 years ago

dmitry-saritasa commented 5 years ago
➜ find . -type f | fzy
> 
./.gitignore
./.gitmessage
./.style.yapf
./docker-compose.yml
./docs/async_processing/index.rst
./docs/jupyter/business/.ipynb_checkpoints/Time Periods-checkpoint.ipynb
./docs/jupyter/business/metrics/.ipynb_checkpoints/flow-checkpoint.ipynb
./docs/jupyter/business/metrics/.ipynb_checkpoints/worktypes_weekly_reports-checkpoint.ipynb
./docs/jupyter/business/metrics/.ipynb_checkpoints/Metrics-checkpoint.ipynb
./docs/jupyter/business/metrics/.ipynb_checkpoints/metrics-checkpoint.ipynb

so how do I exclude folders and files by extensions? For example, exclude all folders named pycache or files with .ipynb extensions?

Dmitry

iamleot commented 5 years ago

Hello Dmitry,

Dmitry Semenov writes:

➜ find . -type f | fzy
> 
./.gitignore
./.gitmessage
./.style.yapf
./docker-compose.yml
./docs/async_processing/index.rst
./docs/jupyter/business/.ipynb_checkpoints/Time Periods-checkpoint.ipynb
./docs/jupyter/business/metrics/.ipynb_checkpoints/flow-checkpoint.ipynb
./docs/jupyter/business/metrics/.ipynb_checkpoints/worktypes_weekly_reports-checkpoint.ipynb
./docs/jupyter/business/metrics/.ipynb_checkpoints/Metrics-checkpoint.ipynb
./docs/jupyter/business/metrics/.ipynb_checkpoints/metrics-checkpoint.ipynb

so how do I exclude folders and files by extensions? For example, exclude all folders named pycache or files with .ipynb extensions?

You can pipe the find output, e.g.: find . -type f | fgrep -v __pycache__ | fzy' or find . -type f | grep -v '.ipynb$' | fzy'.

Alternatively you can also use `ag -l' instead of find(1) that will honor possible .*ignore files.

neuschaefer commented 5 years ago

git ls-files --cached --others --exclude-standard --recurse-submodules is also an option, but I'm not sure about the relative performance versus ag -l. (If you use git ls-files in your vim macro or similar, you should however consider adding a fallback for such cases when you aren't working in a git repo.)

casr commented 5 years ago

I use this script that I made if it's helpful for others:

#!/bin/sh

# NAME
#      pfind – dumb file finder for projects
#
# SYNOPSIS
#      pfind [-n]
#
# DESCRIPTION
#      It just finds files that are checked into a project.
#
#      It does not give you any fancy options with which to filter.

usage() {
    cat <<EOF >&2
usage: $0 [-n]
EOF
}

args=$(getopt n $*)
if [ $? -ne 0 ]; then
    usage
    exit 1
fi

use_vcs=1

set -- $args
while [ $# -ne 0 ]; do
    case "$1"
    in
        -n)
            use_vcs=0; shift;;
        --)
            shift; break;;
    esac
done

if [ ${use_vcs} -eq 1 ] && git rev-parse --is-inside-work-tree >/dev/null; then
    exec git ls-files . --cached --others --exclude-standard 2>/dev/null
else
    # fallback on find but limit depth in case we are in ${HOME}, etc.
    # exec find . -maxdepth 4 -type f -print 2>/dev/null
    exec ag -l --depth 3 2>/dev/null
fi

If you do pfind -n it forces the non-git search path. I usually have this as find so that it grabs all files that aren't excluded from Git.

casr commented 5 years ago

@neuschaefer Does --recurse-submodules work for you as you had it below?

git ls-files --cached --others --exclude-standard --recurse-submodules

I chucked it in my script thinking it was a nice to have that I was missing (previously I just used --cached --others --exclude-standard) but I've noticed it failing with:

fatal: ls-files --recurse-submodules unsupported mode

and checking the man page gives:

       --recurse-submodules
           Recursively calls ls-files on each submodule in the repository.
           Currently there is only support for the --cached mode.

I've updated my script above to just use --cached --others --exclude-standard for now.

neuschaefer commented 5 years ago

@casr: Yes, AFAICT it works.

casr commented 5 years ago

Lucky! (Thanks for checking)