dirhistory.plugin.zsh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ##
  2. # Navigate directory history using ALT-LEFT and ALT-RIGHT. ALT-LEFT moves back to directories
  3. # that the user has changed to in the past, and ALT-RIGHT undoes ALT-LEFT.
  4. #
  5. # Navigate directory hierarchy using ALT-UP and ALT-DOWN.
  6. # ALT-UP moves to higher hierarchy (cd ..)
  7. # ALT-DOWN moves into the first directory found in alphabetical order
  8. #
  9. dirhistory_past=($PWD)
  10. dirhistory_future=()
  11. export dirhistory_past
  12. export dirhistory_future
  13. export DIRHISTORY_SIZE=30
  14. # Pop the last element of dirhistory_past.
  15. # Pass the name of the variable to return the result in.
  16. # Returns the element if the array was not empty,
  17. # otherwise returns empty string.
  18. function pop_past() {
  19. typeset -g $1="${dirhistory_past[$#dirhistory_past]}"
  20. if [[ $#dirhistory_past -gt 0 ]]; then
  21. dirhistory_past[$#dirhistory_past]=()
  22. fi
  23. }
  24. function pop_future() {
  25. typeset -g $1="${dirhistory_future[$#dirhistory_future]}"
  26. if [[ $#dirhistory_future -gt 0 ]]; then
  27. dirhistory_future[$#dirhistory_future]=()
  28. fi
  29. }
  30. # Push a new element onto the end of dirhistory_past. If the size of the array
  31. # is >= DIRHISTORY_SIZE, the array is shifted
  32. function push_past() {
  33. if [[ $#dirhistory_past -ge $DIRHISTORY_SIZE ]]; then
  34. shift dirhistory_past
  35. fi
  36. if [[ $#dirhistory_past -eq 0 || $dirhistory_past[$#dirhistory_past] != "$1" ]]; then
  37. dirhistory_past+=($1)
  38. fi
  39. }
  40. function push_future() {
  41. if [[ $#dirhistory_future -ge $DIRHISTORY_SIZE ]]; then
  42. shift dirhistory_future
  43. fi
  44. if [[ $#dirhistory_future -eq 0 || $dirhistory_futuret[$#dirhistory_future] != "$1" ]]; then
  45. dirhistory_future+=($1)
  46. fi
  47. }
  48. # Called by zsh when directory changes
  49. autoload -U add-zsh-hook
  50. add-zsh-hook chpwd chpwd_dirhistory
  51. function chpwd_dirhistory() {
  52. push_past $PWD
  53. # If DIRHISTORY_CD is not set...
  54. if [[ -z "${DIRHISTORY_CD+x}" ]]; then
  55. # ... clear future.
  56. dirhistory_future=()
  57. fi
  58. }
  59. function dirhistory_cd(){
  60. DIRHISTORY_CD="1"
  61. cd $1
  62. unset DIRHISTORY_CD
  63. }
  64. # Move backward in directory history
  65. function dirhistory_back() {
  66. local cw=""
  67. local d=""
  68. # Last element in dirhistory_past is the cwd.
  69. pop_past cw
  70. if [[ "" == "$cw" ]]; then
  71. # Someone overwrote our variable. Recover it.
  72. dirhistory_past=($PWD)
  73. return
  74. fi
  75. pop_past d
  76. if [[ "" != "$d" ]]; then
  77. dirhistory_cd $d
  78. push_future $cw
  79. else
  80. push_past $cw
  81. fi
  82. }
  83. # Move forward in directory history
  84. function dirhistory_forward() {
  85. local d=""
  86. pop_future d
  87. if [[ "" != "$d" ]]; then
  88. dirhistory_cd $d
  89. push_past $d
  90. fi
  91. }
  92. # Bind keys to history navigation
  93. function dirhistory_zle_dirhistory_back() {
  94. # Erase current line in buffer
  95. zle .kill-buffer
  96. dirhistory_back
  97. zle .accept-line
  98. }
  99. function dirhistory_zle_dirhistory_future() {
  100. # Erase current line in buffer
  101. zle .kill-buffer
  102. dirhistory_forward
  103. zle .accept-line
  104. }
  105. zle -N dirhistory_zle_dirhistory_back
  106. bindkey "\e[3D" dirhistory_zle_dirhistory_back # xterm in normal mode
  107. bindkey "\e[1;3D" dirhistory_zle_dirhistory_back # xterm in normal mode
  108. bindkey "\e\e[D" dirhistory_zle_dirhistory_back # Putty
  109. bindkey "\eO3D" dirhistory_zle_dirhistory_back # GNU screen
  110. case "$TERM_PROGRAM" in
  111. iTerm.app) bindkey "^[^[[D" dirhistory_zle_dirhistory_back ;; # iTerm2
  112. Apple_Terminal) bindkey "^[b" dirhistory_zle_dirhistory_back ;; # Terminal.app
  113. esac
  114. if (( ${+terminfo[kcub1]} )); then
  115. bindkey "^[${terminfo[kcub1]}" dirhistory_zle_dirhistory_back # urxvt
  116. fi
  117. zle -N dirhistory_zle_dirhistory_future
  118. bindkey "\e[3C" dirhistory_zle_dirhistory_future # xterm in normal mode
  119. bindkey "\e[1;3C" dirhistory_zle_dirhistory_future # xterm in normal mode
  120. bindkey "\e\e[C" dirhistory_zle_dirhistory_future # Putty
  121. bindkey "\eO3C" dirhistory_zle_dirhistory_future # GNU screen
  122. case "$TERM_PROGRAM" in
  123. iTerm.app) bindkey "^[^[[C" dirhistory_zle_dirhistory_future ;; # iTerm2
  124. Apple_Terminal) bindkey "^[f" dirhistory_zle_dirhistory_future ;; # Terminal.app
  125. esac
  126. if (( ${+terminfo[kcuf1]} )); then
  127. bindkey "^[${terminfo[kcuf1]}" dirhistory_zle_dirhistory_future # urxvt
  128. fi
  129. #
  130. # HIERARCHY Implemented in this section, in case someone wants to split it to another plugin if it clashes bindings
  131. #
  132. # Move up in hierarchy
  133. function dirhistory_up() {
  134. cd .. || return 1
  135. }
  136. # Move down in hierarchy
  137. function dirhistory_down() {
  138. cd "$(find . -mindepth 1 -maxdepth 1 -type d | sort -n | head -n 1)" || return 1
  139. }
  140. # Bind keys to hierarchy navigation
  141. function dirhistory_zle_dirhistory_up() {
  142. zle .kill-buffer # Erase current line in buffer
  143. dirhistory_up
  144. zle .accept-line
  145. }
  146. function dirhistory_zle_dirhistory_down() {
  147. zle .kill-buffer # Erase current line in buffer
  148. dirhistory_down
  149. zle .accept-line
  150. }
  151. zle -N dirhistory_zle_dirhistory_up
  152. bindkey "\e[3A" dirhistory_zle_dirhistory_up # xterm in normal mode
  153. bindkey "\e[1;3A" dirhistory_zle_dirhistory_up # xterm in normal mode
  154. bindkey "\e\e[A" dirhistory_zle_dirhistory_up # Putty
  155. bindkey "\eO3A" dirhistory_zle_dirhistory_up # GNU screen
  156. case "$TERM_PROGRAM" in
  157. iTerm.app) bindkey "^[^[[A" dirhistory_zle_dirhistory_up ;; # iTerm2
  158. Apple_Terminal) bindkey "^[[A" dirhistory_zle_dirhistory_up ;; # Terminal.app
  159. esac
  160. if (( ${+terminfo[kcuu1]} )); then
  161. bindkey "^[${terminfo[kcuu1]}" dirhistory_zle_dirhistory_up # urxvt
  162. fi
  163. zle -N dirhistory_zle_dirhistory_down
  164. bindkey "\e[3B" dirhistory_zle_dirhistory_down # xterm in normal mode
  165. bindkey "\e[1;3B" dirhistory_zle_dirhistory_down # xterm in normal mode
  166. bindkey "\e\e[B" dirhistory_zle_dirhistory_down # Putty
  167. bindkey "\eO3B" dirhistory_zle_dirhistory_down # GNU screen
  168. case "$TERM_PROGRAM" in
  169. iTerm.app) bindkey "^[^[[B" dirhistory_zle_dirhistory_down ;; # iTerm2
  170. Apple_Terminal) bindkey "^[[B" dirhistory_zle_dirhistory_down ;; # Terminal.app
  171. esac
  172. if (( ${+terminfo[kcud1]} )); then
  173. bindkey "^[${terminfo[kcud1]}" dirhistory_zle_dirhistory_down # urxvt
  174. fi