jump.plugin.zsh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. local markpath="$(readlink $MARKPATH/$1)" || {echo "No such mark: $1"; return 1}
  12. builtin cd "$markpath" 2>/dev/null || {echo "Destination does not exist for mark [$1]: $markpath"; return 2}
  13. }
  14. mark() {
  15. if [[ $# -eq 0 || "$1" = "." ]]; then
  16. MARK=${PWD:t}
  17. else
  18. MARK="$1"
  19. fi
  20. if read -q "?Mark $PWD as ${MARK}? (y/n) "; then
  21. command mkdir -p "$MARKPATH"
  22. command ln -sfn "$PWD" "$MARKPATH/$MARK"
  23. fi
  24. }
  25. unmark() {
  26. LANG= command rm -i "$MARKPATH/$1"
  27. }
  28. marks() {
  29. local link max=0
  30. for link in $MARKPATH/{,.}*(@N); do
  31. if [[ ${#link:t} -gt $max ]]; then
  32. max=${#link:t}
  33. fi
  34. done
  35. local printf_markname_template="$(printf -- "%%%us" "$max")"
  36. for link in $MARKPATH/{,.}*(@N); do
  37. local markname="$fg[cyan]$(printf -- "$printf_markname_template" "${link:t}")$reset_color"
  38. local markpath="$fg[blue]$(readlink $link)$reset_color"
  39. printf -- "%s -> %s\n" "$markname" "$markpath"
  40. done
  41. }
  42. _completemarks() {
  43. reply=("${MARKPATH}"/{,.}*(@N:t))
  44. }
  45. compctl -K _completemarks jump
  46. compctl -K _completemarks unmark
  47. _mark_expansion() {
  48. setopt localoptions extendedglob
  49. autoload -U modify-current-argument
  50. modify-current-argument '$(readlink "$MARKPATH/$ARG" || echo "$ARG")'
  51. }
  52. zle -N _mark_expansion
  53. bindkey "^g" _mark_expansion