A Bash/Zsh shell function that fuzzy-searches for a folder with fzf and jumps straight into it. Ships as a single main.sh file you can source by hand or load automatically from ~/.bashrc / ~/.zshrc — no install step, no dependencies beyond fzf.
- Fuzzy folder jump: Type a few letters, press Enter, and your shell is already inside the folder (
fcd). - File mode: Search for a file instead and
cdinto the folder that contains it (fcd -f). - Recent folders: Every jump is saved, so
fcd -rbrings back places you visited before, newest first. - Search controls: Hidden folders (
-a), depth limit (-d N), pre-filled query (-q), extra excludes (-x), auto-jump on a single match (-1). - Quick starts: Search from home (
-H) or from/(-R), or pass any start directory. - Print mode:
-pprints the chosen path instead of changing directory, for use in pipes and$(...). - Live preview: Folder contents via
ls, or file contents viabat/headin file mode. - Built-in help:
fcd -horfcd --helpshows full usage and examples. - Smart backends: Uses
fd/fdfindwhen installed, falls back tofindotherwise. - Safe input handling: Unknown options, missing arguments, bad depth values and missing
fzfgive clear errors. - Shortcuts:
Ctrl+Fkeybinding plus thefcdh,fcdr,fcdfaliases.
- Language: Bash (also loads in Zsh)
- Picker: fzf
- Optional: fd for faster searching, bat for file previews
- Frontend: none, pure shell
- fzf installed:
sudo apt install fzf # Debian / Ubuntu brew install fzf # macOS
- Optional, for speed:
sudo apt install fd-find(installs asfdfind) orbrew install fd.
-
Clone the repository:
git clone https://github.com/1Xer0/fuzzy-cd.git cd fuzzy-cd -
Pick how you want to use
main.sh:main.shmust be sourced, not executed. A script running in a child process cannot change the directory of your current shell.Option A: try it in the current terminal only
source ./main.sh fcdOption B: integrate with Bash (
~/.bashrc)cp main.sh ~/main.sh echo 'source ~/main.sh' >> ~/.bashrc source ~/.bashrc
Option C: integrate with Zsh (
~/.zshrc)cp main.sh ~/main.sh echo 'source ~/main.sh' >> ~/.zshrc source ~/.zshrc
-
Run it:
fcd # fuzzy-search folders below the current directory fcd -h # show the help message
-
Example session:
$ pwd /home/ali $ fcd -d 3 -q disc fcd> disc projects/school/discrete-structures projects/school/discrete-structures/notes $ pwd /home/ali/projects/school/discrete-structures
fuzzy-cd/
├── README.md
└── main.sh
| Flag | Long form | Description |
|---|---|---|
-h |
--help |
Show the help message and exit |
-a |
--hidden |
Include hidden folders |
-d N |
--depth N |
Limit search depth to N levels |
-f |
--files |
Search files, jump to the chosen file's folder |
-q TEXT |
--query TEXT |
Start fzf with TEXT already typed |
-x NAME |
--exclude NAME |
Exclude folders named NAME (repeatable) |
-r |
--recent |
Pick from previously visited folders |
-p |
--print |
Print the path instead of changing directory |
-n |
--no-preview |
Disable the preview pane |
-1 |
--select-1 |
Jump immediately if there is only one match |
-H |
--home |
Search from ~ |
-R |
--root |
Search from / (can be slow) |
Short flags cannot be combined: use -a -d 2, not -ad 2.
- Loop over
$@with acasestatement, one branch per flag. - Flags with values (
-d,-q,-x) check that an argument follows, thenshiftpast it. - Unknown options print an error and point to
fcd --help. - Validation checks that
fzfexists, the start path is a directory, and depth is a number.
fd/fdfindis used when available, with--type d(orf),--max-depth, and one--excludeper excluded name.findfallback builds an equivalent expression: excluded names are pruned, hidden entries are pruned unless-ais set, then matching types are printed.- Recent mode skips searching and reads
~/.fcd_historyinstead, reversed withtac(ortail -ron macOS) and de-duplicated withawk. - Default excludes:
.git,node_modules,.cache,__pycache__,.venv.
fzfgets a reversed 40% height layout, thefcd>prompt, and a preview command chosen by mode.- Selection is turned into a directory: in file mode,
dirnameof the pick. - Jump:
cd --into the result, then append$PWDto the history file. - Print mode resolves the absolute path with
cd && pwdand prints it without moving.
- Source guard: executing
main.shdirectly prints the exactsourceline to use instead. - Keybinding:
Ctrl+Frunsfcd. Bash usesbind -x; Zsh uses a smallzlewidget withbindkey. Only set in interactive shells. - Aliases:
fcdh(fcd -H),fcdr(fcd -r),fcdf(fcd -f).
| Variable | Default | Purpose |
|---|---|---|
FCD_HISTORY |
~/.fcd_history |
Where visited folders are stored |
To change it, export the variable before sourcing main.sh:
export FCD_HISTORY="$HOME/.config/fcd_history"
source ~/main.sh- Source
main.sh(or add it to your rc file once). - Run
fcd, type part of a folder name, press Enter. Esc cancels without moving. - Narrow or widen the search with flags, for example:
fcd -H -d 3 # search home, max 3 levels deep fcd -a -q config # include hidden folders, start with "config" fcd -f # find a file, jump to its folder fcd -r # jump back to a recent folder fcd -x build -x dist # also skip build and dist folders ls "$(fcd -p)" # use the pick in another command
- Press Ctrl+F anywhere on the command line as a shortcut for
fcd.
fcd: command not found:main.shis not sourced in this shell. Runsource ~/main.shor check your rc file.main.sh must be sourced, not executed: usesource main.sh, not./main.sh.fcd: fzf is not installed: installfzf(see Before Getting Started).- Nothing found: add
-afor hidden folders, or check that the folder is not in the exclude list. - Slow on big trees: install
fd, limit depth with-d, or avoid-R.