Skip to content
Browse files

Fixing fuzzycd to play nicely with other tools that re-define cd, suc…

…h as RVM.
  • Loading branch information...
1 parent 6f66a94 commit 1f7ae870a31c9deb682505199caa968cfc22e429 @harob harob committed
Showing with 12 additions and 2 deletions.
  1. +12 −2 fuzzycd_bash_wrapper.sh
View
14 fuzzycd_bash_wrapper.sh
@@ -1,5 +1,15 @@
# This displaces the normal bash "cd" builtin command. cd needs to be redefined this way because it's not
# a normal binary, but rather a Bash builtin.
+
+# The "cd" command may have already been redefined by another script (RVM does this, for example):
+if [ x`type -t cd` == "xfunction" ]; then
+ # In this case, we define a new "__cd" function with the same body as the previously defined "cd" function.
+ eval $(type cd | grep -v 'cd is a function' | sed 's/^cd/__cd/' | sed 's/^}/;}/' )
+else
+ # Otherwise, we just define "__cd" to directly call the builtin.
+ eval "__cd() { builtin cd \$*; }"
+fi
+
cd() {
# This assumes that fuzzycd.rb is available somewhere in your PATH.
fuzzycd.rb $*
@@ -9,8 +19,8 @@ cd() {
if [ "$output" = "@nomatches" ]; then
echo "No files match \"$*\""
elif [ "$output" = "@passthrough" ]; then
- builtin cd "$*"
+ __cd "$*"
elif [ "$output" != "@exit" ]; then
- builtin cd "$output"
+ __cd "$output"
fi
}

0 comments on commit 1f7ae87

Please sign in to comment.
Something went wrong with that request. Please try again.