jump.plugin.zsh 1.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
# 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
10

11
jump() {
12
	cd -P "$MARKPATH/$1" 2>/dev/null || {echo "No such mark: $1"; return 1}
13
}
14

15
mark() {
16
	if [[ ( $# == 0 ) || ( "$1" == "." ) ]]; then
17
		MARK=$(basename "$PWD")
18
	else
Jeroen Janssens's avatar
Jeroen Janssens committed
19
		MARK="$1"
20
	fi
21
	if read -q \?"Mark $PWD as ${MARK}? (y/n) "; then
22
		mkdir -p "$MARKPATH"; ln -sfn "$PWD" "$MARKPATH/$MARK"
23
	fi
24
}
25

26
unmark() {
27
	rm -i "$MARKPATH/$1"
28
}
29

30
marks() {
31
32
33
34
35
36
37
	local max=0
	for link in $MARKPATH/*(@); do
		if [[ ${#link:t} -gt $max ]]; then
			max=${#link:t}
		fi
	done
	local printf_markname_template="$(printf -- "%%%us " "$max")"
38
39
40
	for link in $MARKPATH/*(@); do
		local markname="$fg[cyan]${link:t}$reset_color"
		local markpath="$fg[blue]$(readlink $link)$reset_color"
41
42
		printf -- "$printf_markname_template" "$markname"
		printf -- "-> %s\n" "$markpath"
43
	done
44
}
45

46
_completemarks() {
47
	if [[ $(ls "${MARKPATH}" | wc -l) -gt 1 ]]; then
48
		reply=($(ls $MARKPATH/**/*(-) | grep : | sed -E 's/(.*)\/([_a-zA-Z0-9\.\-]*):$/\2/g'))
49
50
51
52
53
	else
		if readlink -e "${MARKPATH}"/* &>/dev/null; then
			reply=($(ls "${MARKPATH}"))
		fi
	fi
54
55
56
}
compctl -K _completemarks jump
compctl -K _completemarks unmark
57
58
59
60
61
62
63
64

_mark_expansion() {
	setopt extendedglob
	autoload -U modify-current-argument
	modify-current-argument '$(readlink "$MARKPATH/$ARG")'
}
zle -N _mark_expansion
bindkey "^g" _mark_expansion