jump.plugin.zsh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Easily jump around the file system by manually adding marks
  2. # marks are stored as symbolic links in the directory $MARKPATH (default $HOME/.marks)
  3. #
  4. # jump FOO: jump to a mark named FOO
  5. # mark FOO: create a mark named FOO
  6. # unmark FOO: delete a mark
  7. # marks: lists all marks
  8. #
  9. export MARKPATH=$HOME/.marks
  10. jump() {
  11. builtin cd -P "$MARKPATH/$1" 2>/dev/null || {echo "No such mark: $1"; return 1}
  12. }
  13. mark() {
  14. if [[ $# -eq 0 || "$1" = "." ]]; then
  15. MARK=${PWD:t}
  16. else
  17. MARK="$1"
  18. fi
  19. if read -q "?Mark $PWD as ${MARK}? (y/n) "; then
  20. command mkdir -p "$MARKPATH"
  21. command ln -sfn "$PWD" "$MARKPATH/$MARK"
  22. fi
  23. }
  24. unmark() {
  25. LANG= command rm -i "$MARKPATH/$1"
  26. }
  27. marks() {
  28. local link max=0
  29. for link in $MARKPATH/{,.}*(@N); do
  30. if [[ ${#link:t} -gt $max ]]; then
  31. max=${#link:t}
  32. fi
  33. done
  34. local printf_markname_template="$(printf -- "%%%us" "$max")"
  35. for link in $MARKPATH/{,.}*(@N); do
  36. local markname="$fg[cyan]$(printf -- "$printf_markname_template" "${link:t}")$reset_color"
  37. local markpath="$fg[blue]$(readlink $link)$reset_color"
  38. printf -- "%s -> %s\n" "$markname" "$markpath"
  39. done
  40. }
  41. _completemarks() {
  42. reply=("${MARKPATH}"/{,.}*(@N:t))
  43. }
  44. compctl -K _completemarks jump
  45. compctl -K _completemarks unmark
  46. _mark_expansion() {
  47. setopt localoptions extendedglob
  48. autoload -U modify-current-argument
  49. modify-current-argument '$(readlink "$MARKPATH/$ARG" || echo "$ARG")'
  50. }
  51. zle -N _mark_expansion
  52. bindkey "^g" _mark_expansion