diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2015-03-10 15:36:42 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2015-03-10 15:36:42 +1300 |
commit | abe696b817b37ee54a2bca454a9e1553a4eff465 (patch) | |
tree | b96b3b48730e19d6dd108367390544c42450b107 /bash/bashrc.d/pd.bash | |
parent | Add configuration for Vimperator (diff) | |
download | dotfiles-abe696b817b37ee54a2bca454a9e1553a4eff465.tar.gz dotfiles-abe696b817b37ee54a2bca454a9e1553a4eff465.zip |
Define pd function (parent directory)
Diffstat (limited to 'bash/bashrc.d/pd.bash')
-rw-r--r-- | bash/bashrc.d/pd.bash | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/bash/bashrc.d/pd.bash b/bash/bashrc.d/pd.bash new file mode 100644 index 00000000..f9fb2ab7 --- /dev/null +++ b/bash/bashrc.d/pd.bash @@ -0,0 +1,46 @@ +# Attempt to change into the argument's parent directory; preserve any options +# and pass them to cd. This is intended for use when you've got a file path in +# a variable, or in history, or in Alt+., and want to quickly move to its +# containing directory. In the absence of an argument, this just shifts up a +# directory, i.e. `cd ..` +pd() { + local arg + local -a opts + for arg in "$@" ; do + case $arg in + --) + shift + break + ;; + -*) + shift + opts=("${opts[@]}" "$arg") + ;; + *) + break + ;; + esac + done + if (($#)) ; then + case $# in + 1) + target=$1 + target=${target%/} + target=${target%/*} + ;; + 2) + printf 'bash: pd: too many arguments\n' >&2 + return 1 + ;; + esac + else + target=.. + fi + if [[ $target ]] ; then + builtin cd "${opts[@]}" -- "$target" + else + printf 'bash: pd: error calculating parent directory\n' >&2 + return 1 + fi +} + |