Skip to content

Instantly share code, notes, and snippets.

@oshliaer
Forked from NileDaley/repos.md
Last active December 9, 2022 13:57
Show Gist options
  • Save oshliaer/af7bf73df217bfa41a43a14977f75c49 to your computer and use it in GitHub Desktop.
Save oshliaer/af7bf73df217bfa41a43a14977f75c49 to your computer and use it in GitHub Desktop.
Change `cd` directory to the git repository using fzf (fuzzy search)

A git repositories finder on fzf

I use this little function (repo) 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

or check another options fzf#installation.

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=""
    root_dir=$USER_ROOT

    if [ -n "$1" ]; then
        filter_params="-q $1"
    fi

    find $root_dir -maxdepth 5 -name .git -type d -prune | sed 's/\/.git$//' | sort | fzf $filter_params --select-1 | read  repo_path
    
    if [ -n "$repo_path" ]; then
        echo $repo_path
        cd $repo_path
    fi
}
  • You will need to replace the $USER_ROOT bit on the $repo_path line with the directory where you keep all of your code. As for me it's /drive/userfiles
  • You can open the dir via your faforite IDE. Change cd $repo_path to vim $repo_path or code $repo_path
  • To undo the action of the selected path press [ESC] or [CTRL]+[D] or type :q

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

Credits

It is forked from https://gist.github.com/NileDaley/303090d4e8ee625807ee9f0ee1b8ef53

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