You’ve probably heard me mention fzf before. It’s an amazing command line tool created by Junegunn Choi. It takes a list of data and turns it into a command line menu with fuzzy searching, multi-select, and can even preview each item in whatever way is appropriate. I’ve been using it in all kinds of scripts — where I used to have rudimentary numbered menus, I now have much friendlier and more flexible terminal navigation.
fzf is available via Homebrew, just run brew install fzf. See man fzf for very good documentation.
One great example of how fzf can change your command line life is the set of keybindings for git shared by the author. With these set up you can, for example, start typing a git command that requires a commit hash, hit Ctrl-G Ctrl-H, and get a searchable menu of all your commits. Select the one you’re looking for and the menu closes and the commit’s hash is inserted in your command. It’s crazy handy.
If you’re running Bash or Zsh, Junegunn already has you taken care of. Just add the code in this gist to the appropriate startup files and you’ll be flying through git commands in no time. You can stop reading here, really. The rest of this is for the Fish weirdos.
I run Fish, and the existing solutions needed a little hacking to do what I wanted. Alexandru Rosianu created a gist that works pretty well. I made some modifications for my own needs and it’s working great.
If you want to try it out, you can save the code below to a file and source it from ~/.config/fish/config.fish, then call the git_fzf_key_bindings function to bind the keys. Something like:
if status is-interactive && test -f ~/.config/fish/custom/git_fzf.fish
source ~/.config/fish/custom/git_fzf.fish
git_fzf_key_bindings
end
Once it’s installed, open up a new session to load the init files, cd to a git repository, and type Ctrl-G Ctrl-H. You’ll see a list of your commits with previews on the right. Use the arrows or type to search the text and select the commit you want, and when you hit return its hash will be inserted at the prompt.
Here’s the code:
Here are the bindings:
Ctrl-G Ctrl-F
Shows modified and unstaged files, with their diff in the preview, selection inserts the filename
Ctrl-G Ctrl-B
Shows a menu of local and remote branches, selection inserts branch name
Ctrl-G Ctrl-T
Shows all tags, preview shows tag commit message, selection inserts tag name
Ctrl-G Ctrl-H
Shows all commits on current branch, preview shows commit’s message and diff, selection inserts hash
Ctrl-G Ctrl-R
Shows remotes and their url, selection inserts remote name
Hit Ctrl-P in any menu to turn off the preview. Have fun!