2013-08-15 22:32:01 +08:00
|
|
|
# Easily jump around the file system by manually adding marks
|
|
|
|
# marks are stored as symbolic links in the directory $MARKPATH (default $HOME/.marks)
|
|
|
|
#
|
|
|
|
# jump FOO: jump to a mark named FOO
|
|
|
|
# mark FOO: create a mark named FOO
|
|
|
|
# unmark FOO: delete a mark
|
|
|
|
# marks: lists all marks
|
|
|
|
#
|
|
|
|
export MARKPATH=$HOME/.marks
|
2013-09-06 21:34:27 +08:00
|
|
|
|
2013-09-06 21:40:44 +08:00
|
|
|
jump() {
|
2020-02-12 02:35:17 +08:00
|
|
|
builtin cd -P "$MARKPATH/$1" 2>/dev/null || {echo "No such mark: $1"; return 1}
|
2013-08-15 22:32:01 +08:00
|
|
|
}
|
2013-09-06 21:34:27 +08:00
|
|
|
|
2013-09-06 21:40:44 +08:00
|
|
|
mark() {
|
2020-02-12 02:35:17 +08:00
|
|
|
if [[ $# -eq 0 || "$1" = "." ]]; then
|
|
|
|
MARK=${PWD:t}
|
2013-09-06 21:55:43 +08:00
|
|
|
else
|
2013-09-07 03:29:14 +08:00
|
|
|
MARK="$1"
|
2013-09-06 21:55:43 +08:00
|
|
|
fi
|
2020-02-12 02:35:17 +08:00
|
|
|
if read -q "?Mark $PWD as ${MARK}? (y/n) "; then
|
|
|
|
command mkdir -p "$MARKPATH"
|
|
|
|
command ln -sfn "$PWD" "$MARKPATH/$MARK"
|
2013-09-06 21:55:43 +08:00
|
|
|
fi
|
2013-08-15 22:32:01 +08:00
|
|
|
}
|
2013-09-06 21:34:27 +08:00
|
|
|
|
2013-09-06 21:40:44 +08:00
|
|
|
unmark() {
|
2020-02-12 02:35:17 +08:00
|
|
|
LANG= command rm -i "$MARKPATH/$1"
|
2013-08-15 22:32:01 +08:00
|
|
|
}
|
2013-09-06 21:34:27 +08:00
|
|
|
|
2013-09-06 21:40:44 +08:00
|
|
|
marks() {
|
2015-11-27 19:29:15 +08:00
|
|
|
local link max=0
|
2020-02-12 03:12:01 +08:00
|
|
|
for link in $MARKPATH/{,.}*(@N); do
|
2019-01-01 03:15:14 +08:00
|
|
|
if [[ ${#link:t} -gt $max ]]; then
|
|
|
|
max=${#link:t}
|
|
|
|
fi
|
|
|
|
done
|
2023-04-07 19:35:12 +08:00
|
|
|
local printf_markname_template="$(printf -- "%%%us" "$max")"
|
2020-02-12 03:12:01 +08:00
|
|
|
for link in $MARKPATH/{,.}*(@N); do
|
2023-04-07 19:35:12 +08:00
|
|
|
local markname="$fg[cyan]$(printf -- "$printf_markname_template" "${link:t}")$reset_color"
|
2013-09-06 21:40:44 +08:00
|
|
|
local markpath="$fg[blue]$(readlink $link)$reset_color"
|
2023-04-07 19:35:12 +08:00
|
|
|
printf -- "%s -> %s\n" "$markname" "$markpath"
|
2013-09-06 21:40:44 +08:00
|
|
|
done
|
2013-08-15 22:32:01 +08:00
|
|
|
}
|
2013-08-19 02:16:26 +08:00
|
|
|
|
2013-09-06 21:40:44 +08:00
|
|
|
_completemarks() {
|
2015-11-27 19:29:15 +08:00
|
|
|
reply=("${MARKPATH}"/{,.}*(@N:t))
|
2013-08-19 02:16:26 +08:00
|
|
|
}
|
|
|
|
compctl -K _completemarks jump
|
|
|
|
compctl -K _completemarks unmark
|
2013-09-06 21:34:27 +08:00
|
|
|
|
|
|
|
_mark_expansion() {
|
2015-11-27 19:29:15 +08:00
|
|
|
setopt localoptions extendedglob
|
2013-09-06 21:34:27 +08:00
|
|
|
autoload -U modify-current-argument
|
2020-02-12 03:12:01 +08:00
|
|
|
modify-current-argument '$(readlink "$MARKPATH/$ARG" || echo "$ARG")'
|
2013-09-06 21:34:27 +08:00
|
|
|
}
|
|
|
|
zle -N _mark_expansion
|
|
|
|
bindkey "^g" _mark_expansion
|