diff --git a/modules/utility/README.md b/modules/utility/README.md index ebcc43c5..120e93b7 100644 --- a/modules/utility/README.md +++ b/modules/utility/README.md @@ -124,6 +124,7 @@ Functions ### Files and Directories - `cdls` changes to a directory and lists its contents. + - `cdx` locates directory of an executable in your path and changes to it. - `dut` displays the grand total disk usage using human readable units. - `find-exec` finds files and executes a command on them. - `mkdcd` makes a directory and changes to it. @@ -153,6 +154,7 @@ Authors - [Robby Russell](https://github.com/robbyrussell) - [Suraj N. Kurapati](https://github.com/sunaku) - [Sorin Ionescu](https://github.com/sorin-ionescu) + - [Wil Moore III](https://github.com/wilmoore) [1]: https://github.com/sorin-ionescu/prezto/issues diff --git a/modules/utility/init.zsh b/modules/utility/init.zsh index 91c78090..caffd2a0 100644 --- a/modules/utility/init.zsh +++ b/modules/utility/init.zsh @@ -185,3 +185,24 @@ function psu { ps -U "${1:-$USER}" -o 'pid,%cpu,%mem,command' "${(@)argv[2,-1]}" } +# locates directory of an executable in your path and changes to it. +function cdx { + local exe="$1" + local fqe="$(command -v $exe 2>/dev/null)" + local dir="$(dirname $fqe 2>/dev/null)" + + if [[ ! -x "$fqe" ]]; then + echo "cdx: '$exe' not found." + return 1 + fi + + if [[ ! -d "$dir" ]]; then + echo "cdx: '$dir' is not a directory" + return 1 + fi + + cd $dir + + return $? +} +