dircycle.plugin.zsh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # enables cycling through the directory stack using
  2. # Ctrl+Shift+Left/Right
  3. #
  4. # left/right direction follows the order in which directories
  5. # were visited, like left/right arrows do in a browser
  6. # NO_PUSHD_MINUS syntax:
  7. # pushd +N: start counting from left of `dirs' output
  8. # pushd -N: start counting from right of `dirs' output
  9. # Either switch to a directory from dirstack, using +N or -N syntax
  10. # or switch to a directory by path, using `switch-to-dir -- <path>`
  11. switch-to-dir () {
  12. # If $1 is --, then treat $2 as a directory path
  13. if [[ $1 == -- ]]; then
  14. # We use `-q` because we don't want chpwd to run, we'll do it manually
  15. [[ -d "$2" ]] && builtin pushd -q "$2" &>/dev/null
  16. return $?
  17. fi
  18. setopt localoptions nopushdminus
  19. [[ ${#dirstack} -eq 0 ]] && return 1
  20. while ! builtin pushd -q $1 &>/dev/null; do
  21. # We found a missing directory: pop it out of the dir stack
  22. builtin popd -q $1
  23. # Stop trying if there are no more directories in the dir stack
  24. [[ ${#dirstack} -eq 0 ]] && return 1
  25. done
  26. }
  27. insert-cycledleft () {
  28. switch-to-dir +1 || return $?
  29. local fn
  30. for fn in chpwd $chpwd_functions precmd $precmd_functions; do
  31. (( $+functions[$fn] )) && $fn
  32. done
  33. zle reset-prompt
  34. }
  35. zle -N insert-cycledleft
  36. insert-cycledright () {
  37. switch-to-dir -0 || return $?
  38. local fn
  39. for fn in chpwd $chpwd_functions precmd $precmd_functions; do
  40. (( $+functions[$fn] )) && $fn
  41. done
  42. zle reset-prompt
  43. }
  44. zle -N insert-cycledright
  45. insert-cycledup () {
  46. switch-to-dir -- .. || return $?
  47. local fn
  48. for fn in chpwd $chpwd_functions precmd $precmd_functions; do
  49. (( $+functions[$fn] )) && $fn
  50. done
  51. zle reset-prompt
  52. }
  53. zle -N insert-cycledup
  54. insert-cycleddown () {
  55. switch-to-dir -- "$(find . -mindepth 1 -maxdepth 1 -type d | sort -n | head -n 1)" || return $?
  56. local fn
  57. for fn in chpwd $chpwd_functions precmd $precmd_functions; do
  58. (( $+functions[$fn] )) && $fn
  59. done
  60. zle reset-prompt
  61. }
  62. zle -N insert-cycleddown
  63. # These sequences work for xterm, Apple Terminal.app, and probably others.
  64. # Not for rxvt-unicode, but it doesn't seem differentiate Ctrl-Shift-Arrow
  65. # from plain Shift-Arrow, at least by default.
  66. #
  67. # iTerm2 does not have these key combinations defined by default; you will need
  68. # to add them under "Keys" in your profile if you want to use this. You can do
  69. # this conveniently by loading the "xterm with Numeric Keypad" preset.
  70. bindkey "\e[1;6D" insert-cycledleft # Ctrl+Shift+Left
  71. bindkey "\e[1;6C" insert-cycledright # Ctrl+Shift+Right
  72. bindkey "\e[1;6A" insert-cycledup # Ctrl+Shift+Up
  73. bindkey "\e[1;6B" insert-cycleddown # Ctrl+Shift+Down