Originally posted on my blog - codenio.hashnode.dev
Ever find yourself typing cd ~/workspace/client-project/backend
for the hundredth time? Let's fix that.
Here's a bash function that'll help (or at least save you some keystrokes):
projects(){
# If '-o' or '--open' flag is passed, open selected project in VS Code
if [[ $* == *-o* || $* == *--open* ]]; then
# List projects in ~/workspace/demo/projects and select one with fzf
repo=`command ls ~/workspace/demo/projects | fzf --height=50% --layout=reverse`
echo $repo
# Open the selected project folder in VS Code
code ~/workspace/demo/projects/$repo
# If '-c' or '--cd' flag is passed, change directory to selected project
elif [[ $* == *-c* || $* == *--cd* ]]; then
# Change to projects directory first
cd ~/workspace/demo/projects
# Select a project folder with fzf
repo=`command ls | fzf --height=50% --layout=reverse`
echo $repo
# Change directory into the selected project
cd $repo
# If no flags passed, just change directory to projects folder
else
cd ~/workspace/demo/projects
fi
What's This Magic? 🪄
Drop this in your .bashrc
or .zshrc
, and you get three superpowers:
-
projects
→ Jump to your projects folder -
projects -c
→ Fuzzy-find and cd into any project -
projects -o
→ Open any project in VS Code
Why This Rocks 🎸
Sure, you could:
- Set up 50 different aliases (and update them constantly)
- Use tab completion (and type half the path anyway)
- Install a fancy terminal (and learn a whole new tool)
Or... just use this tiny function that:
- Works with your existing setup
- Leverages fzf's awesome fuzzy search
- Stays out of your way
Pro Tips 💡
- Customize the base path (
~/workspace/demo/projects
) to match your setup - Add more flags for different actions (git operations, running tests, etc.)
- Use
--height=50%
to control how much screen space fzf takes
That's It!
No fancy frameworks. No complex setup. Just a simple hack that makes project navigation easy.
Already using fzf? This'll fit right in. Never tried it? Here's your excuse to start.
Happy hacking! ⚡️