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