Skip to content

Instantly share code, notes, and snippets.

@NileDaley
Last active August 27, 2021 05:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NileDaley/303090d4e8ee625807ee9f0ee1b8ef53 to your computer and use it in GitHub Desktop.
Save NileDaley/303090d4e8ee625807ee9f0ee1b8ef53 to your computer and use it in GitHub Desktop.
CD to repo using fzf (fuzzy search)

I use this little function on the command line to allow me to switch between different repositories easily using fzf

Prerequisits

  • fzf
  • zsh or bash

Installation

FZF

If you don't already have fzf installed you can install it via Homebrew:

brew install fzf

Add function to zsh or bash config

Depending on if you're using bash or zsh as your shell, you can add the following code to ~/.bashrc or ~/.zshrc

function repo {
    filter_params=""
    if [ -n "$1" ]; then
        filter_params="-q $1"
    fi
    repo_path=$(find ~/code -name .git -type d -prune -maxdepth 5 | sed 's/\/.git$//' | sort | fzf $filter_params --select-1)
    cd $repo_path
}

You will need to replace the ~/code bit on the $repo_path line with the directory where you keep all of your code.

Save the file, then reload your shell using either bash or zsh.

Usage

Command Example Description
repo Display all git repositories
repo {query} repo kiosk Display all git repositories matching query. If only 1 result matches query, it'll select that result automatically

Demo

asciicast

@oshliaer
Copy link

Thanks.

How about escape or unselect for exit?

@oshliaer
Copy link

How about escape or unselect for exit?

The next line fixes that

find $root_dir -maxdepth 5 -name .git -type d -prune | sed 's/\/.git$//' | sort | fzf $filter_params --select-1 | read  repo_path

https://gist.github.com/contributorpw/af7bf73df217bfa41a43a14977f75c49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment