conf2/zsh/.oh-my-zsh/plugins/jump/jump.plugin.zsh

60 lines
1.4 KiB
Bash
Raw Normal View History

2019-01-12 15:47:23 +00: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
jump() {
2020-06-11 14:41:32 +00:00
builtin cd -P "$MARKPATH/$1" 2>/dev/null || {echo "No such mark: $1"; return 1}
2019-01-12 15:47:23 +00:00
}
mark() {
2020-06-11 14:41:32 +00:00
if [[ $# -eq 0 || "$1" = "." ]]; then
MARK=${PWD:t}
2019-01-12 15:47:23 +00:00
else
MARK="$1"
fi
2020-06-11 14:41:32 +00:00
if read -q "?Mark $PWD as ${MARK}? (y/n) "; then
command mkdir -p "$MARKPATH"
command ln -sfn "$PWD" "$MARKPATH/$MARK"
2019-01-12 15:47:23 +00:00
fi
}
unmark() {
2020-06-11 14:41:32 +00:00
LANG= command rm -i "$MARKPATH/$1"
2019-01-12 15:47:23 +00:00
}
marks() {
2020-06-11 14:41:32 +00:00
local link max=0
for link in $MARKPATH/{,.}*(@N); do
2019-01-12 15:47:23 +00:00
if [[ ${#link:t} -gt $max ]]; then
max=${#link:t}
fi
done
local printf_markname_template="$(printf -- "%%%us " "$max")"
2020-06-11 14:41:32 +00:00
for link in $MARKPATH/{,.}*(@N); do
2019-01-12 15:47:23 +00:00
local markname="$fg[cyan]${link:t}$reset_color"
local markpath="$fg[blue]$(readlink $link)$reset_color"
printf -- "$printf_markname_template" "$markname"
printf -- "-> %s\n" "$markpath"
done
}
_completemarks() {
2020-06-11 14:41:32 +00:00
reply=("${MARKPATH}"/{,.}*(@N:t))
2019-01-12 15:47:23 +00:00
}
compctl -K _completemarks jump
compctl -K _completemarks unmark
_mark_expansion() {
2020-06-11 14:41:32 +00:00
setopt localoptions extendedglob
2019-01-12 15:47:23 +00:00
autoload -U modify-current-argument
2020-06-11 14:41:32 +00:00
modify-current-argument '$(readlink "$MARKPATH/$ARG" || echo "$ARG")'
2019-01-12 15:47:23 +00:00
}
zle -N _mark_expansion
bindkey "^g" _mark_expansion