how to use fzf on Mac Terminal with bash

8

2

I found a utility called fzf https://github.com/junegunn/fzf which is a fuzzy-finder for your shell.

Two questions:

1) After I run fzf and run a file, I press Enter and then the file I found is output in Terminal. Instead of Enter, what key should I press to copy that file path to my pastebin?

2) Ideally, I'd like to be able to do something like this: cat <pattern><hotkey> where I type part of the name of a file and then press a hotkey to start an fzf search for that pattern. Is there a way to do this?

mark

Posted 2014-09-26T21:09:58.813

Reputation: 539

you write "to my pastebin?" You mean to your clipboard – barlop – 2019-06-06T18:16:04.520

I meant to say "pasteboard" as described in man pbpaste – mark – 2019-06-07T02:06:27.113

Answers

7

I suggest that you read through the README page. You need a basic level of understanding of shell scripting in order to fully utilize fzf.

fzf is a Unix filter just like grep or sed, and all it does is to print the selected items. What to do with the output is completely up to you.


1) After I run fzf and run a file, I press Enter and then the file I found is output in Terminal. Instead of Enter, what key should I press to copy that file path to my pastebin?

You can use pbcopy command to store the result in clipboard like so:

fzf | pbcopy

More conventional way to use fzf is to use it with command substitution:

cat $(fzf)

Or to use the CTRL-T key binding that pastes the names of the selected files onto the command line:

cat <CTRL-T>

2) Ideally, I'd like to be able to do something like this: cat where I type part of the name of a file and then press a hotkey to start an fzf search for that pattern. Is there a way to do this?

fzf is shipped with fuzzy-completion for bash. See here for more details. To invoke auto-completion, append two asterisks to the pattern and hit the tab key as follows:

cat pat**<Tab>

If you don't use bash, the closest you can do is to use CTRL-T keybinding mentioned above.

See: https://github.com/junegunn/fzf#key-bindings-for-command-line

Junegunn Choi

Posted 2014-09-26T21:09:58.813

Reputation: 406

1Ah, I found out my problem. I installed fzf via homebrew (brew install fzf), and the CTRL-T and pat**<Tab> shortcuts didn't work. I now installed fzf directly from the GitHub repo, and everything works now. – mark – 2014-09-27T20:52:17.373

7brew install fzf will only install fzf executable. In order to install extra features, you have to run the attached install script after the installation. See brew info fzf for more info. – Junegunn Choi – 2014-09-28T01:36:28.063

@JunegunnChoi yeah that's mentioned https://www.aheil.de/2019/04/07/fzf-command-line-fuzzy-finder-on-macos-x/

– barlop – 2019-06-06T18:17:03.907

1

I tried installing fzf via homebrew and my CTRL-T and pat** shortcuts also did not work. I found that fzf added some commands to my .bashrc, but my .bashrc doesn't get called.

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

Putting that same source statement from .bashrc into .bash_aliases did the trick for me.

rado

Posted 2014-09-26T21:09:58.813

Reputation: 141

.bash_profile for me.. 'cos apparently osx doesn't use .bashrc it uses .bash_profile – barlop – 2019-06-06T18:17:53.310