dirhistory.plugin.zsh 6.3 KB

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