jump.plugin.zsh 1.3 KB

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