aboutsummaryrefslogtreecommitdiff
path: root/bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2014-10-04 13:26:56 +1300
committerTom Ryder <tom@sanctum.geek.nz>2014-10-04 13:26:56 +1300
commit47cc0769cd5a379d39f7d91c322d284e3a1bba9f (patch)
treec3aad7344d995d49150be3fbd6a376330de1040b /bash
parentDon't mkmv verbosely (diff)
downloaddotfiles-47cc0769cd5a379d39f7d91c322d284e3a1bba9f.tar.gz
dotfiles-47cc0769cd5a379d39f7d91c322d284e3a1bba9f.zip
Command to move up directory tree to matching name
Diffstat (limited to 'bash')
-rw-r--r--bash/bashrc.d/bd.bash36
1 files changed, 36 insertions, 0 deletions
diff --git a/bash/bashrc.d/bd.bash b/bash/bashrc.d/bd.bash
new file mode 100644
index 00000000..2c955453
--- /dev/null
+++ b/bash/bashrc.d/bd.bash
@@ -0,0 +1,36 @@
+# Move back up the directory tree to the first directory matching the name
+bd() {
+
+ # If there are no arguments, we just move up one directory (cd ..)
+ if [[ $1 ]] ; then
+ dir="${PWD%/${1:?}*}"/"$1"
+ else
+ dir=..
+ fi
+
+ # Check the directory exists and try to cd into it if possible
+ if [[ -d $dir ]] ; then
+ builtin cd -- "$dir"
+ else
+ printf 'bash: No dir found in PWD named %s' >&2
+ return 1
+ fi
+}
+
+# Completion setup for bd
+_bd() {
+ local word=${COMP_WORDS[COMP_CWORD]}
+
+ # Build a list of dirs in $PWD
+ local -a dirs
+ while read -d / -r dir ; do
+ if [[ $dir ]] ; then
+ dirs=("${dirs[@]}" "$dir")
+ fi
+ done < <(printf %s "$PWD")
+
+ # Complete with matching dirs
+ COMPREPLY=( $(compgen -W "${dirs[*]}" -- "$word") )
+}
+complete -F _bd bd
+