jump.plugin.zsh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 -sfn "$PWD" "$MARKPATH/$MARK"
  21. fi
  22. }
  23. unmark() {
  24. rm -i "$MARKPATH/$1"
  25. }
  26. marks() {
  27. local max=0
  28. for link in $MARKPATH/*(@); do
  29. if [[ ${#link:t} -gt $max ]]; then
  30. max=${#link:t}
  31. fi
  32. done
  33. local printf_markname_template="$(printf -- "%%%us " "$max")"
  34. for link in $MARKPATH/*(@); do
  35. local markname="$fg[cyan]${link:t}$reset_color"
  36. local markpath="$fg[blue]$(readlink $link)$reset_color"
  37. printf -- "$printf_markname_template" "$markname"
  38. printf -- "-> %s\n" "$markpath"
  39. done
  40. }
  41. _completemarks() {
  42. if [[ $(ls "${MARKPATH}" | wc -l) -gt 1 ]]; then
  43. reply=($(ls $MARKPATH/**/*(-) | grep : | sed -E 's/(.*)\/([_a-zA-Z0-9\.\-]*):$/\2/g'))
  44. else
  45. if readlink -e "${MARKPATH}"/* &>/dev/null; then
  46. reply=($(ls "${MARKPATH}"))
  47. fi
  48. fi
  49. }
  50. compctl -K _completemarks jump
  51. compctl -K _completemarks unmark
  52. _mark_expansion() {
  53. setopt extendedglob
  54. autoload -U modify-current-argument
  55. modify-current-argument '$(readlink "$MARKPATH/$ARG")'
  56. }
  57. zle -N _mark_expansion
  58. bindkey "^g" _mark_expansion