dirhistory.plugin.zsh 5.1 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. eval "$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. eval "$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. # xterm in normal mode
  107. bindkey "\e[3D" dirhistory_zle_dirhistory_back
  108. bindkey "\e[1;3D" dirhistory_zle_dirhistory_back
  109. # Terminal.app
  110. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
  111. bindkey "^[b" dirhistory_zle_dirhistory_back
  112. fi
  113. # iTerm2
  114. if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  115. bindkey "^[^[[D" dirhistory_zle_dirhistory_back
  116. fi
  117. # Putty:
  118. bindkey "\e\e[D" dirhistory_zle_dirhistory_back
  119. # GNU screen:
  120. bindkey "\eO3D" dirhistory_zle_dirhistory_back
  121. zle -N dirhistory_zle_dirhistory_future
  122. bindkey "\e[3C" dirhistory_zle_dirhistory_future
  123. bindkey "\e[1;3C" dirhistory_zle_dirhistory_future
  124. # Terminal.app
  125. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
  126. bindkey "^[f" dirhistory_zle_dirhistory_future
  127. fi
  128. # iTerm2
  129. if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  130. bindkey "^[^[[C" dirhistory_zle_dirhistory_future
  131. fi
  132. bindkey "\e\e[C" dirhistory_zle_dirhistory_future
  133. bindkey "\eO3C" dirhistory_zle_dirhistory_future
  134. #
  135. # HIERARCHY Implemented in this section, in case someone wants to split it to another plugin if it clashes bindings
  136. #
  137. # Move up in hierarchy
  138. function dirhistory_up() {
  139. cd .. || return 1
  140. }
  141. # Move down in hierarchy
  142. function dirhistory_down() {
  143. cd "$(find . -mindepth 1 -maxdepth 1 -type d | sort -n | head -n 1)" || return 1
  144. }
  145. # Bind keys to hierarchy navigation
  146. function dirhistory_zle_dirhistory_up() {
  147. zle .kill-buffer # Erase current line in buffer
  148. dirhistory_up
  149. zle .accept-line
  150. }
  151. function dirhistory_zle_dirhistory_down() {
  152. zle .kill-buffer # Erase current line in buffer
  153. dirhistory_down
  154. zle .accept-line
  155. }
  156. zle -N dirhistory_zle_dirhistory_up
  157. # xterm in normal mode
  158. bindkey "\e[3A" dirhistory_zle_dirhistory_up
  159. bindkey "\e[1;3A" dirhistory_zle_dirhistory_up
  160. if [[ "$TERM_PROGRAM" == "Apple_Terminal" || "$TERM_PROGRAM" == "iTerm.app" ]]; then
  161. bindkey "^[[A" dirhistory_zle_dirhistory_up
  162. fi
  163. # Putty:
  164. bindkey "\e\e[A" dirhistory_zle_dirhistory_up
  165. # GNU screen:
  166. bindkey "\eO3A" dirhistory_zle_dirhistory_up
  167. zle -N dirhistory_zle_dirhistory_down
  168. bindkey "\e[3B" dirhistory_zle_dirhistory_down
  169. bindkey "\e[1;3B" dirhistory_zle_dirhistory_down
  170. if [[ "$TERM_PROGRAM" == "Apple_Terminal" || "$TERM_PROGRAM" == "iTerm.app" ]]; then
  171. bindkey "^[[B" dirhistory_zle_dirhistory_down
  172. fi
  173. bindkey "\e\e[B" dirhistory_zle_dirhistory_down
  174. bindkey "\eO3B" dirhistory_zle_dirhistory_down