fzf – Command-line Fuzzy Finder on macOS X

If you master your Terminal it always looks like you are a superhero. Therefore, I always try to present a lot using terminal during my lecture, just to provide some smoke and mirrors.

However, I am far away from mastering Terminal truly. Therefore, it comes in handy that one of my students pointed me to reverse-i-search which is available on Terminal.

Wich this reverse lookup you can search through previous commands used in Terminal before. While this is an awesome feature you can activate by control-r on macOS, it does not allow you to search for similar commands used before. It always shows just exact matches of what you typed.

reverse-i-search on macOS

To search through similar typed commands, you can use fzf, a command-line fuzzy finder available at GitHub.

It’s an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

Installing on macOS using Homebrew

For your convenience you can install fzf using Homebrew using the command

brew install fzf 

For me, the bash integration was not available after the installation. After some investigation (I might haven’t read the instruction well enough), I realized the installation script needs to be run:

/usr/local/opt/fzf/install

Eventually, the installation routines put [ -f ~/.fzf.bash ] && source ~/.fzf.bash into .bashrc. I run into this issue some time before. Reading through this article on Scripting OS X might give you an idea why it does not run.

When you open a terminal application, it does not ask for login. You will just get a command prompt. In other versions of Unix or Linux, this will not run the .bash_profile but a different file .bashrc. The underlying idea is that the .bash_profile should be run only once when you login, and the .bashrc for every new interactive shell.

However, Terminal.app on macOS, does not follow this convention. When Terminal.app opens a new window, it will run .bash_profile. Not, as users familiar with other Unix systems would expect.bashrc.

Moving the line mentioned above into .bash_profile and deleting .bashrc eventually solved the issue. After restarting Terminal the key bindings CTRL-R and CTRL-T have been available as expected.

fzf command-line fuzzy search

With this little trick the installations of fzf works quite well on macOS X Mojave.

tl;dr

Use reverse-i-search by pressing control-r on macOS to search previous commands typed in Terminal. In addition, you can use fzf, a command-line fuzzy finder to find similar commands and all other kinds of stuff.

To install run

  • brew install fzf
  • /usr/local/opt/fzf/install
  • copy [ -f ~/.fzf.bash ] && source ~/.fzf.bash from .bashrc into ~/.bash_profile (and remove .bashrc if not needed) or source .basrc from .bash_profile

Link: https://github.com/junegunn/fzf

2 Comments

  1. andreas

    Reply

    Also a nice one. I will definitely give it a try. Looking forward to see how well this one works on macOS 🙂

Leave a Reply to andreas Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.