cd 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env bash
  2. # Source a .rvmrc file in a directory after changing to it, if it exists. To
  3. # disable this feature, set rvm_project_rvmrc=0 in /etc/rvmrc or $HOME/.rvmrc
  4. case "${rvm_project_rvmrc:-1}" in
  5. 1|cd)
  6. # cloned from git@github.com:mpapis/bash_zsh_support.git
  7. source "$rvm_scripts_path/extras/bash_zsh_support/chpwd/function.sh"
  8. # not using default loading to support older Zsh
  9. [[ -n "${ZSH_VERSION:-}" ]] &&
  10. __rvm_version_compare "$ZSH_VERSION" -gt 4.3.4 ||
  11. {
  12. function cd() { __zsh_like_cd cd "$@" ; }
  13. function popd() { __zsh_like_cd popd "$@" ; }
  14. function pushd() { __zsh_like_cd pushd "$@" ; }
  15. }
  16. __rvm_after_cd()
  17. {
  18. \typeset rvm_hook
  19. rvm_hook="after_cd"
  20. if [[ -n "${rvm_scripts_path:-}" || -n "${rvm_path:-}" ]]
  21. then source "${rvm_scripts_path:-$rvm_path/scripts}/hook"
  22. fi
  23. }
  24. __rvm_cd_functions_set()
  25. {
  26. __rvm_do_with_env_before
  27. if [[ -n "${rvm_current_rvmrc:-""}" && "$OLDPWD" == "$PWD" ]]
  28. then rvm_current_rvmrc=""
  29. fi
  30. __rvm_project_rvmrc >&2 || true
  31. __rvm_after_cd || true
  32. __rvm_do_with_env_after
  33. return 0
  34. }
  35. [[ " ${chpwd_functions[*]} " == *" __rvm_cd_functions_set "* ]] ||
  36. chpwd_functions=( "${chpwd_functions[@]}" __rvm_cd_functions_set )
  37. # This functionality is opt-in by setting rvm_cd_complete_flag=1 in ~/.rvmrc
  38. # Generic bash cd completion seems to work great for most, so this is only
  39. # for those that have some issues with that.
  40. if (( ${rvm_cd_complete_flag:-0} == 1 ))
  41. then
  42. # If $CDPATH is set, bash should tab-complete based on directories in those paths,
  43. # but with the cd function above, the built-in tab-complete ignores $CDPATH. This
  44. # function returns that functionality.
  45. _rvm_cd_complete ()
  46. {
  47. \typeset directory current matches item index sep
  48. sep="${IFS}"
  49. export IFS
  50. IFS=$'\n'
  51. COMPREPLY=()
  52. current="${COMP_WORDS[COMP_CWORD]}"
  53. if [[ -n "$CDPATH" && ${current:0:1} != "/" ]] ; then
  54. index=0
  55. # The change to IFS above means that the \command \tr below should replace ':'
  56. # with a newline rather than a space. A space would be ignored, breaking
  57. # TAB completion based on CDPATH again
  58. for directory in $(printf "%b" "$CDPATH" | \command \tr -s ':' '\n') ; do
  59. for item in $( compgen -d "$directory/$current" ) ; do
  60. COMPREPLY[index++]=${item#$directory/}
  61. done
  62. done
  63. else
  64. COMPREPLY=( $(compgen -d ${current}) )
  65. fi
  66. IFS="${sep}";
  67. }
  68. complete -o bashdefault -o default -o filenames -o dirnames -o nospace -F _rvm_cd_complete cd
  69. fi
  70. ;;
  71. 2|prompt)
  72. if
  73. [[ -n "${ZSH_VERSION:-}" ]]
  74. then
  75. precmd_functions+=(__rvm_do_with_env_before __rvm_project_rvmrc __rvm_do_with_env_after)
  76. else
  77. PROMPT_COMMAND="${PROMPT_COMMAND%% }"
  78. PROMPT_COMMAND="${PROMPT_COMMAND%%;}"
  79. PROMPT_COMMAND="${PROMPT_COMMAND:-}${PROMPT_COMMAND:+; }__rvm_do_with_env_before; __rvm_project_rvmrc; __rvm_do_with_env_after"
  80. fi
  81. ;;
  82. esac