dirhistory.plugin.zsh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. (mac keybindings not yet implemented)
  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. # Mac teminal (alt+left/right)
  110. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
  111. bindkey "^[b" dirhistory_zle_dirhistory_back
  112. fi
  113. # Putty:
  114. bindkey "\e\e[D" dirhistory_zle_dirhistory_back
  115. # GNU screen:
  116. bindkey "\eO3D" dirhistory_zle_dirhistory_back
  117. zle -N dirhistory_zle_dirhistory_future
  118. bindkey "\e[3C" dirhistory_zle_dirhistory_future
  119. bindkey "\e[1;3C" dirhistory_zle_dirhistory_future
  120. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
  121. bindkey "^[f" dirhistory_zle_dirhistory_future
  122. fi
  123. bindkey "\e\e[C" dirhistory_zle_dirhistory_future
  124. bindkey "\eO3C" dirhistory_zle_dirhistory_future
  125. #
  126. # HIERARCHY Implemented in this section, in case someone wants to split it to another plugin if it clashes bindings
  127. #
  128. # Move up in hierarchy
  129. function dirhistory_up() {
  130. cd .. || return 1
  131. }
  132. # Move down in hierarchy
  133. function dirhistory_down() {
  134. cd "$(find . -mindepth 1 -maxdepth 1 -type d | sort -n | head -n 1)" || return 1
  135. }
  136. # Bind keys to hierarchy navigation
  137. function dirhistory_zle_dirhistory_up() {
  138. zle kill-buffer # Erase current line in buffer
  139. dirhistory_up
  140. zle accept-line
  141. }
  142. function dirhistory_zle_dirhistory_down() {
  143. zle kill-buffer # Erase current line in buffer
  144. dirhistory_down
  145. zle accept-line
  146. }
  147. zle -N dirhistory_zle_dirhistory_up
  148. # xterm in normal mode
  149. bindkey "\e[3A" dirhistory_zle_dirhistory_up
  150. bindkey "\e[1;3A" dirhistory_zle_dirhistory_up
  151. # Mac teminal (alt+up)
  152. #bindkey "^[?" dirhistory_zle_dirhistory_up #dont know it
  153. # Putty:
  154. bindkey "\e\e[A" dirhistory_zle_dirhistory_up
  155. # GNU screen:
  156. bindkey "\eO3A" dirhistory_zle_dirhistory_up
  157. zle -N dirhistory_zle_dirhistory_down
  158. bindkey "\e[3B" dirhistory_zle_dirhistory_down
  159. bindkey "\e[1;3B" dirhistory_zle_dirhistory_down
  160. # Mac teminal (alt+down)
  161. #bindkey "^[?" dirhistory_zle_dirhistory_down #dont know it
  162. bindkey "\e\e[B" dirhistory_zle_dirhistory_down
  163. bindkey "\eO3B" dirhistory_zle_dirhistory_down