dirhistory.plugin.zsh 6.9 KB

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