dirhistory.plugin.zsh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. chpwd_functions+=(chpwd_dirhistory)
  50. function chpwd_dirhistory() {
  51. push_past $PWD
  52. # If DIRHISTORY_CD is not set...
  53. if [[ -z "${DIRHISTORY_CD+x}" ]]; then
  54. # ... clear future.
  55. dirhistory_future=()
  56. fi
  57. }
  58. function dirhistory_cd(){
  59. DIRHISTORY_CD="1"
  60. cd $1
  61. unset DIRHISTORY_CD
  62. }
  63. # Move backward in directory history
  64. function dirhistory_back() {
  65. local cw=""
  66. local d=""
  67. # Last element in dirhistory_past is the cwd.
  68. pop_past cw
  69. if [[ "" == "$cw" ]]; then
  70. # Someone overwrote our variable. Recover it.
  71. dirhistory_past=($PWD)
  72. return
  73. fi
  74. pop_past d
  75. if [[ "" != "$d" ]]; then
  76. dirhistory_cd $d
  77. push_future $cw
  78. else
  79. push_past $cw
  80. fi
  81. }
  82. # Move forward in directory history
  83. function dirhistory_forward() {
  84. local d=""
  85. pop_future d
  86. if [[ "" != "$d" ]]; then
  87. dirhistory_cd $d
  88. push_past $d
  89. fi
  90. }
  91. # Bind keys to history navigation
  92. function dirhistory_zle_dirhistory_back() {
  93. # Erase current line in buffer
  94. zle kill-buffer
  95. dirhistory_back
  96. zle accept-line
  97. }
  98. function dirhistory_zle_dirhistory_future() {
  99. # Erase current line in buffer
  100. zle kill-buffer
  101. dirhistory_forward
  102. zle accept-line
  103. }
  104. zle -N dirhistory_zle_dirhistory_back
  105. # xterm in normal mode
  106. bindkey "\e[3D" dirhistory_zle_dirhistory_back
  107. bindkey "\e[1;3D" dirhistory_zle_dirhistory_back
  108. # Mac teminal (alt+left/right)
  109. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
  110. bindkey "^[b" dirhistory_zle_dirhistory_back
  111. fi
  112. # Putty:
  113. bindkey "\e\e[D" dirhistory_zle_dirhistory_back
  114. # GNU screen:
  115. bindkey "\eO3D" dirhistory_zle_dirhistory_back
  116. zle -N dirhistory_zle_dirhistory_future
  117. bindkey "\e[3C" dirhistory_zle_dirhistory_future
  118. bindkey "\e[1;3C" dirhistory_zle_dirhistory_future
  119. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
  120. bindkey "^[f" dirhistory_zle_dirhistory_future
  121. fi
  122. bindkey "\e\e[C" dirhistory_zle_dirhistory_future
  123. bindkey "\eO3C" dirhistory_zle_dirhistory_future
  124. #
  125. # HIERARCHY Implemented in this section, in case someone wants to split it to another plugin if it clashes bindings
  126. #
  127. # Move up in hierarchy
  128. function dirhistory_up() {
  129. cd .. || return 1
  130. }
  131. # Move down in hierarchy
  132. function dirhistory_down() {
  133. cd "$(find . -mindepth 1 -maxdepth 1 -type d | sort -n | head -n 1)" || return 1
  134. }
  135. # Bind keys to hierarchy navigation
  136. function dirhistory_zle_dirhistory_up() {
  137. zle kill-buffer # Erase current line in buffer
  138. dirhistory_up
  139. zle accept-line
  140. }
  141. function dirhistory_zle_dirhistory_down() {
  142. zle kill-buffer # Erase current line in buffer
  143. dirhistory_down
  144. zle accept-line
  145. }
  146. zle -N dirhistory_zle_dirhistory_up
  147. # xterm in normal mode
  148. bindkey "\e[3A" dirhistory_zle_dirhistory_up
  149. bindkey "\e[1;3A" dirhistory_zle_dirhistory_up
  150. # Mac teminal (alt+up)
  151. #bindkey "^[?" dirhistory_zle_dirhistory_up #dont know it
  152. # Putty:
  153. bindkey "\e\e[A" dirhistory_zle_dirhistory_up
  154. # GNU screen:
  155. bindkey "\eO3A" dirhistory_zle_dirhistory_up
  156. zle -N dirhistory_zle_dirhistory_down
  157. bindkey "\e[3B" dirhistory_zle_dirhistory_down
  158. bindkey "\e[1;3B" dirhistory_zle_dirhistory_down
  159. # Mac teminal (alt+down)
  160. #bindkey "^[?" dirhistory_zle_dirhistory_down #dont know it
  161. bindkey "\e\e[B" dirhistory_zle_dirhistory_down
  162. bindkey "\eO3B" dirhistory_zle_dirhistory_down