jump.plugin.zsh 1.41 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
	builtin cd -P "$MARKPATH/$1" 2>/dev/null || {echo "No such mark: $1"; return 1}
13
}
14

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

27
unmark() {
28
	LANG= command rm -i "$MARKPATH/$1"
29
}
30

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

47
_completemarks() {
48
	reply=("${MARKPATH}"/{,.}*(@N:t))
49
50
51
}
compctl -K _completemarks jump
compctl -K _completemarks unmark
52
53

_mark_expansion() {
54
	setopt localoptions extendedglob
55
	autoload -U modify-current-argument
56
	modify-current-argument '$(readlink "$MARKPATH/$ARG" || echo "$ARG")'
57
58
59
}
zle -N _mark_expansion
bindkey "^g" _mark_expansion