dircycle.plugin.zsh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. switch-to-dir () {
  10. setopt localoptions nopushdminus
  11. [[ ${#dirstack} -eq 0 ]] && return 1
  12. while ! builtin pushd -q $1 &>/dev/null; do
  13. # We found a missing directory: pop it out of the dir stack
  14. builtin popd -q $1
  15. # Stop trying if there are no more directories in the dir stack
  16. [[ ${#dirstack} -eq 0 ]] && return 1
  17. done
  18. }
  19. insert-cycledleft () {
  20. switch-to-dir +1 || return
  21. local fn
  22. for fn (chpwd $chpwd_functions precmd $precmd_functions); do
  23. (( $+functions[$fn] )) && $fn
  24. done
  25. zle reset-prompt
  26. }
  27. zle -N insert-cycledleft
  28. insert-cycledright () {
  29. switch-to-dir -0 || return
  30. local fn
  31. for fn (chpwd $chpwd_functions precmd $precmd_functions); do
  32. (( $+functions[$fn] )) && $fn
  33. done
  34. zle reset-prompt
  35. }
  36. zle -N insert-cycledright
  37. # These sequences work for xterm, Apple Terminal.app, and probably others.
  38. # Not for rxvt-unicode, but it doesn't seem differentiate Ctrl-Shift-Arrow
  39. # from plain Shift-Arrow, at least by default.
  40. # iTerm2 does not have these key combinations defined by default; you will need
  41. # to add them under "Keys" in your profile if you want to use this. You can do
  42. # this conveniently by loading the "xterm with Numeric Keypad" preset.
  43. bindkey "\e[1;6D" insert-cycledleft
  44. bindkey "\e[1;6C" insert-cycledright