dirhistory.plugin.zsh 6.5 KB

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