junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
65.63k stars 2.41k forks source link

How can I make __fzf_cd__ pipe to ag #986

Closed chiefjester closed 7 years ago

chiefjester commented 7 years ago

Just installed this yesterday, everything is working great. Was able to figure out how to pipe commands in ag so it ignores node_modules and whatever it's in .gitignore.

However, I think the __fzf_cd__ doesn't use fzf default's command FZF_DEFAULT_COMMAND

I have it set export FZF_DEFAULT_COMMAND='ag --hidden --ignore .git -g ""'

junegunn commented 7 years ago

FZF_DEFAULT_COMMAND lists files, the command behind ALT-C should list directories. See https://github.com/junegunn/fzf#key-bindings-for-command-line

chiefjester commented 7 years ago

woohoo!! works! Sorry about not reading fully the README, just so happy fzf is so extensible! @junegunn


export FZF_ALT_C_COMMAND='ag --hidden --ignore .git -g ""'

chiefjester commented 7 years ago

@junegunn any direction on what to add for alt-c command? I'm just trying to ignore node_modules and .git.

If I add ag in the mix, it includes files now instead of just directories?

chiefjester commented 7 years ago

I ended up redefining the function inside my bash_profile

...
[ -f ~/.fzf.bash ] && source ~/.fzf.bash

__fzf_cd__() {
  local cmd dir
  cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' -o -path '*/node_modules*' -o -path '*/.git*' \\) -prune \
    -o -type d -print 2> /dev/null | cut -b3-"}"
  dir=$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m) && printf 'cd %q' "$dir"
}

where I added the node_modules and .git to be pruned.

junegunn commented 7 years ago

You need a tool that lists directories while respecting gitignored entries. I wrote one.

https://github.com/junegunn/blsd

command -v blsd > /dev/null && export FZF_ALT_C_COMMAND='blsd'
command -v tree > /dev/null && export FZF_ALT_C_OPTS="--preview 'tree -C {} | head -200'"
chiefjester commented 7 years ago

I've tried to install blsd, getting

+ cmake -DTHREADSAFE=ON -DBUILD_CLAR=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=../install ..
./script/build-libgit2-static.sh: line 11: cmake: command not found
make[1]: *** [build-libgit2] Error 127
make: *** [git2go] Error 2

I think I'm missing some dependencies to compile blsd?

@junegunn what do you think of the idea, I make a PR to ignore node_modules and .git in __fzf_cd__ function?

Afterall it's unlikely people need to search anything inside node_modules, they can go to the node_modules directly if they are looking for a particular module?

junegunn commented 7 years ago

The message looks pretty clear to me, you're missing cmake.

Regarding the suggestion, no thanks. I don't want to maintain a list of directories that are irrelevant for each and every language platform. Also I don't understand why you're redefining the whole __fzf_cd__ when you can customize the command with FZF_ALT_C_COMMAND.

chiefjester commented 7 years ago

Well, I just wanted to ask :). I hope you didn't get offended; I meant that with utmost respect hence I was asking permission. Also thank you very much for making this wonderful command line tool and pointing me to the right direction on wielding this simple but very powerful tool. 🙇

To answer your question on why I was proposing the solution. It's a solution where you don't need to install other dependencies. Also.git and node_modules are not language specific though.

I was able to install blsd, now getting 0/0 results from command. I've removed the function I edited in bashrc too.

http://take.ms/2ZS4U

@junegunn Should I open an issue in blsd repo?

junegunn commented 7 years ago

No offense taken, and I know you didn't mean to offend me in anyway. But I got the impression that you took little time to investigate the issues by yourself (reading the documentation, trying to understand the error message, googling solutions, etc) before asking me questions. And questions, they demand my time and mental attention, which are both limited.

Also.git and node_modules are not language specific though.

I don't do much Javascript development, and most of the projects I work on do not have node_modules. Instead they have target, pkg, vendor, build, etc. Should we keep updating the find command whenever we run into such directories? I don't think so. They are precisely specified in .gitignore of each project and we should delegate the filtering to a tool that already knows how to process .gitignore. blsd is one such example, and one can easily come up with a script doing that based on ag or rg (e.g. https://github.com/junegunn/agl).

now getting 0/0 results from command

Does it work on your command line?

chiefjester commented 7 years ago

@junegunn in zsh getting this: ❯ blsd zsh: killed blsd

in bash I get this: $ blsd Killed: 9


I absolutely understand your reasoning on not having to maintain a list, perhaps setting an env variable would suffice for folders that should be skipped for the find command? I also do think blsd would be the most optimal solution since it requires almost minor change.

chiefjester commented 7 years ago

So I've tried to dig up on some of PRs in blsd, and tried to reinstalled it via

brew install --HEAD https://raw.githubusercontent.com/junegunn/blsd/master/blsd.rb

That came from this PR. https://github.com/junegunn/blsd/pull/2

Now it seems blsd is working fine. And indeed confirm adding export FZF_ALT_C_COMMAND='blsd' works.

Thank for your direction @junegunn 🙇


Just my 2 cents on having an ability to pass/add/amend ignore files. I very much agree that most of the time adding files in .gitignore solve the 90%~95% of the use cases. But there are cases where you do need to filter out files that were committed in git. I have a build that was passed to me, a very old one. Most of the vendor files were committed in the repo.

Ag keeps on giving me false positive. I'm glad I followed your advice on looking for solutions here in git. Turns out there's a fzf#vim#ag_raw function that I can pass the flags to ag and that I was able to make a custom function to ignore files that are not part of the build.


What I meant about node_modules and .git not being language specific though is you can pretty much use npm for any language you want. Albeit most of them are javascript but, nothing is stopping you from using it on say PHP. For .git files, is language agnostic as well.

Just clarifying things. ✌️