Bash Functions + fzf : A Faster Way to Switch Project Directories


A Tiny Hack That Speeds Up Directory/Project Switching
When you're juggling multiple projects, switching between folders can become tedious—especially if you keep typing long paths every time. Whether you want to open a repo in VS Code or quickly check something in the terminal, it interrupts your workflow.
This simple Bash function streamlines that process. Using fzf, it lets you quickly pick a project and either open it in VS Code or jump into the folder—all with a single command.
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
}
That’s it. Add this to your .bashrc
or .zshrc
, reload, and you’re good to go.
How It Works
projects
:→ Navigates to
~/workspace/demo/projects
(your projects directory).projects -c
:→ Shows an interactive list of folders in
projects
→ Then changes the directory to the selected one.
projects -o
:→ Displays an interactive folder list
→ Opens the selected project in VS Code without changing the current working directory.
It’s a wrapper, but it could save a few seconds every time—and those add up.
Why Prefer This Over Everything Else
Trying a few different approaches for quick project switching. Here’s how this stacks up against some of them:
Aliases are great for 1–2 projects, but not scalable when you have dozens. You’re constantly editing your dotfiles.
Fuzzy search with tab (
fzf ~/workspace/**<Tab>
) works, but it’s a bit clunky and requires typing part of the folder name.Modern terminal tools like Warp/Tabby offer smart navigation, but they come with their own learning curve, require installation, and sometimes feel a bit too much for just changing/opening directories.
The function I’m sharing here hits a sweet spot:
- It gives full control, keeps everything local and simple, and lets us keep using fzf —which I already love for file picking and history search.
The best part? - we are not locked into some tool or interface. It’s our terminal, our setup, and we could extend it based on requirements. Tomorrow, we could add support to do git pull
after switching. Or open the project’s docs. Or anything else.
Final Thoughts
This isn’t a game-changing framework or a new CLI tool. It’s just a small quality-of-life hack that I genuinely use every single day.
If you’re a terminal person and already use fzf, this will feel like a natural extension of your workflow. And if you don’t—maybe this is your excuse to try it out.
Let me know if you tweak it or improve on it. I’m always down to make small things better.
Happy coding 👋
Subscribe to my newsletter
Read articles from codenio directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
