dirhistory.plugin.zsh 6.9 KB

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