├── bd.plugin.zsh ├── animation.gif ├── tests ├── .zshenv └── bd.t ├── Makefile ├── .travis.yml ├── README.md └── bd.zsh /bd.plugin.zsh: -------------------------------------------------------------------------------- 1 | bd.zsh -------------------------------------------------------------------------------- /animation.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crash/zsh_zsh-bd/master/animation.gif -------------------------------------------------------------------------------- /tests/.zshenv: -------------------------------------------------------------------------------- 1 | test -f "$TESTDIR/.zcompdump" && rm "$TESTDIR/.zcompdump" 2 | 3 | source "$TESTDIR/../bd.zsh" 4 | export ENV_AUTHORIZATION_FILE="$PWD/.env_auth" 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: itests tests 2 | 3 | itests: 4 | ZDOTDIR="${PWD}/tests" cram -i --shell=zsh tests 5 | 6 | tests: 7 | ZDOTDIR="${PWD}/tests" cram --shell=zsh tests 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.3" 4 | before_script: 5 | - "sudo apt-get install zsh" 6 | install: "sudo pip install cram" 7 | script: "make tests" 8 | -------------------------------------------------------------------------------- /tests/bd.t: -------------------------------------------------------------------------------- 1 | Move into some folders 2 | 3 | $ mkdir -p a/b/c/d 4 | $ cd a/b/c/d 5 | 6 | Move down to b 7 | 8 | $ bd b 9 | 10 | Check that it went OK 11 | 12 | $ ls 13 | c 14 | 15 | Now lets try to jump to folder with spaces in it 16 | 17 | $ mkdir -p 'a/space dir/f/g/h' 18 | $ cd 'a/space dir/f/g/h' 19 | $ bd 'space dir' 20 | $ ls 21 | f 22 | 23 | Issue [#6] in github, `Fix error "not an identifier"`. I think the actual error 24 | was due to OS X vs gnuutils differences. But it wont hurt to have another test 25 | case. 26 | 27 | $ cd 'f/g/h' 28 | $ bd f 29 | $ ls 30 | g 31 | 32 | Lets also make sure we can jump beyond a 'space dir' 33 | 34 | $ cd g/h 35 | $ bd a 36 | $ ls 37 | space dir 38 | 39 | If the provided argument is an integer, go up as many times as required. 40 | 41 | $ cd 'space dir/f/g/h' 42 | $ bd 1 43 | $ ls 44 | h 45 | $ bd 2 46 | $ ls 47 | f 48 | 49 | Make sure folders can be only numbers, and they are understood 50 | 51 | $ mkdir -p ncpc/2015/whistles 52 | $ cd ncpc/2015/whistles 53 | $ bd 2015 54 | $ ls 55 | whistles 56 | 57 | Also document (with this test) the ambigious case 58 | 59 | $ mkdir -p 1/2/3 60 | $ cd 1/2/3 61 | $ bd 1 62 | $ ls 63 | 2 64 | 65 | Exclude current working directory from parent list 66 | 67 | $ mkdir -p aaa/bbb/aaa/ccc 68 | $ cd aaa/bbb/aaa 69 | $ bd aaa 70 | $ ls 71 | bbb 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/Tarrasch/zsh-bd.png)](https://travis-ci.org/Tarrasch/zsh-bd) 2 | 3 | # zsh-bd 4 | 5 | Quickly go back to a specific parent directory instead of typing `cd ../../..` redundantly. 6 | 7 | Update August 2017: While there have not been commits lately, it's still fully functional. 8 | 9 | --- 10 | 11 | This is a reimplementation of 12 | [vigneshwaranr/bd](https://github.com/vigneshwaranr/bd) that utilizes the power 13 | of the zsh shell. 14 | 15 | ## Install 16 | 17 | All you need to do is to source `bd.zsh`. Here's the manual installation 18 | 19 | mkdir -p $HOME/.zsh/plugins/bd 20 | curl https://raw.githubusercontent.com/Tarrasch/zsh-bd/master/bd.zsh > $HOME/.zsh/plugins/bd/bd.zsh 21 | print -- "\n# zsh-bd\n. \$HOME/.zsh/plugins/bd/bd.zsh" >> $HOME/.zshrc 22 | 23 | Restart your shell (or run `zsh`) 24 | 25 | ### [Antigen](https://github.com/zsh-users/antigen) 26 | 27 | If you prefer antigen over manual installation 28 | 29 | antigen-bundle Tarrasch/zsh-bd 30 | 31 | ## Usage 32 | 33 | $ mkdir -p a/b/c/d 34 | $ cd a/b/c/d 35 | $ bd b 36 | $ ls 37 | c 38 | $ cd c/d 39 | $ bd 2 40 | $ ls 41 | c 42 | 43 | Here's an animation also showing the completion functionality 44 | 45 | ![Animated gif](animation.gif "Animation that shows auto completion") 46 | 47 | ## Thanks 48 | 49 | Thanks to 50 | [@vigneshwaranr](https://github.com/vigneshwaranr) 51 | for inspiring me to do a zsh version. 52 | 53 | Thanks to [@voria](https://github.com/voria) for many useful 54 | patches! (see commit log) 55 | -------------------------------------------------------------------------------- /bd.zsh: -------------------------------------------------------------------------------- 1 | bd () { 2 | (($#<1)) && { 3 | print -- "usage: $0 " 4 | print -- " $0 " 5 | return 1 6 | } >&2 7 | # example: 8 | # $PWD == /home/arash/abc ==> $num_folders_we_are_in == 3 9 | local num_folders_we_are_in=${#${(ps:/:)${PWD}}} 10 | local dest="./" 11 | 12 | # First try to find a folder with matching name (could potentially be a number) 13 | # Get parents (in reverse order) 14 | local parents 15 | local i 16 | for i in {$num_folders_we_are_in..2} 17 | do 18 | parents=($parents "$(echo $PWD | cut -d'/' -f$i)") 19 | done 20 | parents=($parents "/") 21 | # Build dest and 'cd' to it 22 | local parent 23 | foreach parent (${parents}) 24 | do 25 | dest+="../" 26 | if [[ $1 == $parent ]] 27 | then 28 | cd $dest 29 | return 0 30 | fi 31 | done 32 | 33 | # If the user provided an integer, go up as many times as asked 34 | dest="./" 35 | if [[ "$1" = <-> ]] 36 | then 37 | if [[ $1 -gt $num_folders_we_are_in ]] 38 | then 39 | print -- "bd: Error: Can not go up $1 times (not enough parent directories)" 40 | return 1 41 | fi 42 | for i in {1..$1} 43 | do 44 | dest+="../" 45 | done 46 | cd $dest 47 | return 0 48 | fi 49 | 50 | # If the above methods fail 51 | print -- "bd: Error: No parent directory named '$1'" 52 | return 1 53 | } 54 | _bd () { 55 | # Get parents (in reverse order) 56 | local num_folders_we_are_in=${#${(ps:/:)${PWD}}} 57 | local i 58 | for i in {$num_folders_we_are_in..2} 59 | do 60 | reply=($reply "`echo $PWD | cut -d'/' -f$i`") 61 | done 62 | reply=($reply "/") 63 | } 64 | compctl -V directories -K _bd bd 65 | --------------------------------------------------------------------------------