Terminal Tricks: A Fuzzy cd Command
Mac OS X
— 14 Aug 2008 07:19 — 1274 days ago
Here’s another trick to speed up file system navigation in the shell. A previous tip showed how to use bookmarks to reach deeply nested directories effectively.
I use that a lot, but I frequently use it to enter directories with a large number of subdirectories whose names are long and share a prefix:
... Arteria-Media-ImageEditor Arteria-Media-ImageEditor-Base Arteria-Media-ImageRep-Magick Arteria-Media-JSONClient Arteria-Media-JSONSegmenter Arteria-Media-Webapp-MediaServer ...
I wanted a way to enter one of these directories by typing only a minimal, unique substring of its name (like typing *substring* but only considering directories and ignoring case), so I wrote this shell function:
function c {
shopt -q nocasematch || resetcase=1
shopt -s nocasematch
for i in *; do [ -d "$i" ] && [[ "$i" == *"$1"* ]] && cd "$i" && break; done
[ $resetcase ] && shopt -u nocasematch
}
Now I can type
c ncl
and, in the example directory above, it takes me directly into the Arteria-Media-JSONClient subdirectory.
|


