An Idiosyncratic Blog

馃攷 Quick search git projects from the command line

Published on
1 minutes read

I have a lot of git projects on my machine. I have a folder called ~/Development where I keep all my projects. The projects are sorted by categories like Work, Personal, Open Source, etc.

Sometimes I want to quickly search for a project and open it in VS Code. I have an extension called Project Manager that allows me to quickly open a project in VS Code. But I wanted to do it from the command line.

I created a simple alias that allows me to quickly search for a project and open it in VS Code.

alias pm='function _pm() { local depth="${1:-3}"; local selected_folder; selected_folder=$(find . -maxdepth "$depth" -type d -name ".git" -prune -exec dirname {} \; | fzf --height=50%); [ -n "$selected_folder" ] && code "$selected_folder"; }; _pm'

This alias defines a shell function _pm that:

  • Searches for folders with Git repositories using find.
  • Pipes the result to fzf for interactive selection.
  • Stores the selected folder in the variable selected_folder.
  • Checks if a folder was selected ([ -n "$selected_folder" ]).
  • If a folder is selected, opens it in Visual Studio Code using the code command.

To use this alias, add the line to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc, etc.) and restart your terminal or run source ~/.bashrc(or the equivalent command for your shell).

After adding the alias, you can use pm in your terminal. It will display a list of folders with Git repositories, and you can interactively select one using fzf. If you press Enter after selecting a folder, it will open in Visual Studio Code.