***** DONE Skipping Empty Directories In zsh :blog:software:diy:programming: CLOSED: [2017-07-23 Sun 19:00] SCHEDULED: <2017-07-23 Sun> :PROPERTIES: :CREATED: [2017-07-23 Sun 18:52] :ID: 2017-07-23-zsh-skip-empty-dirs :END: :LOGBOOK: - State "DONE" from "NEXT" [2017-07-23 Sun 19:00] :END: On GitHub, I started to love a nice feature I would like to have in my zsh as well. If I change to a directory that contains nothing but a single sub-directory, change to this sub-directory instead (recursive). Therefore, when I am in a directory which contains the following sub-directories, I would like to skip all directories until =there_is_a_file= when I type =cd dir_with_empty_subdirs=: - =dir_with_empty_subdirs= (dir) - =until= (dir) - =once= (dir) - =there_is_a_file= (dir) - =this_is_a_file.txt= (file) You can [[https://github.com/novoid/github-tests/tree/7ecd4b58dbeaf1d4ff6dfc0f338bd03b0d522c25][try out this feature on GitHub in my test repo]]: simply click on the directory =dir_with_empty_subdirs= and you really going to end up in the directory =there_is_a_file=. In case I need to visit any (almost empty) directory within this cascade, I can do so by going back via "cd .." which does not skip empty directories, thus avoiding an endless loop and non-visitable directories. Well, I was not able to come up with a solution on my own. Reddit-user [[https://www.reddit.com/user/seanliao96][seanliao96]] did [[https://www.reddit.com/r/zsh/comments/6omtk9/cd_skipping_directories_that_contain_only_a/][provide a perfectly working solution]]: #+BEGIN_SRC function chpwd() { files=$(ls -A | wc -l) if [[ $files = "1" ]]; then zmodload zsh/parameter if [[ "cd .." != $history[$HISTCMD] ]]; then f=$(ls -A) if [[ -d "$f" ]]; then cd "$f" fi fi fi } #+END_SRC Simply paste this code in your zsh or add it to your =~/.zshrc.local= and enjoy the higher usability in your shell.