diff --git a/modules/bd/Readme.md b/modules/bd/Readme.md new file mode 100644 index 00000000..a2c39e00 --- /dev/null +++ b/modules/bd/Readme.md @@ -0,0 +1,18 @@ +# bd +Jump back to a specific directory, without doing `cd ../../..` + +This is simply [bd](https://github.com/Tarrasch/zsh-bd) ported to a prezto module. + +For further information, e.g. a usage animation, I refer to the [original repository](https://github.com/Tarrasch/zsh-bd). + +## Usage +```zsh +$ mkdir -p a/b/c/d +$ cd a/b/c/d +$ bd b +$ ls +c +``` + +## Acknowledgements +Thanks to [@Tarrasch](https://github.com/Tarrasch) for the zsh implementation of bd! diff --git a/modules/bd/init.zsh b/modules/bd/init.zsh new file mode 100644 index 00000000..b67a44be --- /dev/null +++ b/modules/bd/init.zsh @@ -0,0 +1,40 @@ +bd () { + (($#<1)) && { + print -- "usage: $0 " + return 1 + } >&2 + # Get parents (in reverse order) + local parents + local num=${#${(ps:/:)${PWD}}} + local i + for i in {$((num+1))..2} + do + parents=($parents "`echo $PWD | cut -d'/' -f$i`") + done + parents=($parents "/") + # Build dest and 'cd' to it + local dest="./" + local parent + foreach parent (${parents}) + do + if [[ $1 == $parent ]] + then + cd $dest + return 0 + fi + dest+="../" + done + print -- "bd: Error: No parent directory named '$1'" + return 1 +} +_bd () { + # Get parents (in reverse order) + local num=${#${(ps:/:)${PWD}}} + local i + for i in {$((num+1))..2} + do + reply=($reply "`echo $PWD | cut -d'/' -f$i`") + done + reply=($reply "/") +} +compctl -V directories -K _bd bd