Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 133 lines (132 sloc) 3.728 kB
052bfd5 @rupa oops how'd that shift get there
authored
1 # maintains a jump-list of the directories you actually use
8d2f4d1 @rupa dunno why it thinks this is different
authored
2 # old directories eventually fall off the list
e597cd3 @rupa rename some variables for clarity, and credit autojump's author by name
authored
3 # inspired by Joel Schaerer's http://wiki.github.com/joelthelion/autojump
8d2f4d1 @rupa dunno why it thinks this is different
authored
4 # and something similar i had - but i could never get the dir list right.
5 #
6 # INSTALL:
7 # source into .bashrc under your '[-z "$PS1" ] || return' line
8 # cd around for a while
9 #
10 # USE:
b57503a @rupa major refactoring. add --s - shortest match
authored
11 # j [--h[elp]] [--l] [--r] [--s] [regex1 ... regexn]
07f18e6 @rupa use local variables
authored
12 # with no args, returns full list (same as j --l)
052bfd5 @rupa oops how'd that shift get there
authored
13 # regex1 ... regexn jump to the the weighted directory matching all masks
07f18e6 @rupa use local variables
authored
14 # --l show the list instead of jumping
b57503a @rupa major refactoring. add --s - shortest match
authored
15 # --r order by recently used instead of most used.
16 # --s shortest matching path
1921938 @rupa update documentation for internal reasons
authored
17 #
116a4c3 @rupa allow / in search terms, and documentation
authored
18 # TIPS:
19 # Some handy aliases:
20 # alias jl='j --l'
21 # alias jr='j --r'
b57503a @rupa major refactoring. add --s - shortest match
authored
22 # alias js='j --s'
116a4c3 @rupa allow / in search terms, and documentation
authored
23 #
1921938 @rupa update documentation for internal reasons
authored
24 # CREDITS:
25 # Joel Schaerer aka joelthelion for autojump
26 # Daniel Drucker aka dmd for finding bugs and making me late for lunch
8d2f4d1 @rupa dunno why it thinks this is different
authored
27 j() {
28 # change jfile if you already have a .j file for something else
07f18e6 @rupa use local variables
authored
29 local jfile=$HOME/.j
b57503a @rupa major refactoring. add --s - shortest match
authored
30 [ "$1" = "--add" ] && {
052bfd5 @rupa oops how'd that shift get there
authored
31 shift
8d2f4d1 @rupa dunno why it thinks this is different
authored
32 # we're in $HOME all the time, let something else get all the good letters
33 [ "$*" = "$HOME" ] && return
1e25349 @rupa support for sorting by most recently accessed with --r and --lr options
authored
34 awk -v q="$*" -v t="$(date +%s)" -F"|" '
8d2f4d1 @rupa dunno why it thinks this is different
authored
35 $2 >= 1 {
1e25349 @rupa support for sorting by most recently accessed with --r and --lr options
authored
36 if( $1 == q ) {
37 l[$1] = $2 + 1
38 d[$1] = t
39 found = 1
40 } else {
41 l[$1] = $2
42 d[$1] = $3
43 count += $2
44 }
8d2f4d1 @rupa dunno why it thinks this is different
authored
45 }
46 END {
1e25349 @rupa support for sorting by most recently accessed with --r and --lr options
authored
47 if( !found ) l[q] = 1 && d[q] = t
e597cd3 @rupa rename some variables for clarity, and credit autojump's author by name
authored
48 if( count > 1000 ) {
1e25349 @rupa support for sorting by most recently accessed with --r and --lr options
authored
49 for( i in l ) print i "|" 0.9*l[i] "|" d[i] # aging
50 } else for( i in l ) print i "|" l[i] "|" d[i]
8d2f4d1 @rupa dunno why it thinks this is different
authored
51 }
4c79e1f @rupa put back error suppression i removed for debugging
authored
52 ' $jfile 2>/dev/null > $jfile.tmp
afe869a Fixed 'mv' asking to overwrite ~/.j
Brett Anderson authored
53 mv -f $jfile.tmp $jfile
b57503a @rupa major refactoring. add --s - shortest match
authored
54 return
55 }
56 # tab completion
57 [ "$1" = "--complete" ] && {
8530507 @rupa fix bash completion to account for all arguments. wow, that was a pain
authored
58 awk -v q="$2" -F"|" '
b57503a @rupa major refactoring. add --s - shortest match
authored
59 BEGIN { split(substr(q,3),a," ") }
ffa5795 @rupa hide directories that no longer exist, remove a debug
authored
60 {
61 if( system("test -d \"" $1 "\"") ) next
62 for( i in a ) $1 !~ a[i] && $1 = ""; if( $1 ) print $1
63 }
8d2f4d1 @rupa dunno why it thinks this is different
authored
64 ' $jfile 2>/dev/null
b57503a @rupa major refactoring. add --s - shortest match
authored
65 return
66 }
90a5035 @rupa fix regression where --l didnt work
authored
67 if [ $1 ]; then
68 local x; local out
69 for x do case $x in
70 --h*) echo "j [--h[elp]] [--r] [--l] [regex1 ... regexn]"; return;;
71 --l)local list=1;;
72 --r)local recent=r;;
73 --s)local short=1;;
74 *)local out="$out $x";;
75 esac; shift; done
76 set -- $out
77 else
78 local list=1
79 fi
80 if [ $list ]; then
b57503a @rupa major refactoring. add --s - shortest match
authored
81 [ "$short" ] && return
82 awk -v q="$*" -v t="$(date +%s)" -v r="$recent" -F"|" '
83 BEGIN { f = 2; split(q,a," "); if( r ) f = 3 }
84 {
85 if( system("test -d \"" $1 "\"") ) next
86 for( i in a ) $1 !~ a[i] && $1 = ""
87 if( $1 ) if( f == 3 ) { print t - $f "\t" $1 } else print $f "\t" $1
1e25349 @rupa support for sorting by most recently accessed with --r and --lr options
authored
88 }
b57503a @rupa major refactoring. add --s - shortest match
authored
89 ' $jfile 2>/dev/null | sort -n$recent
90 # if we hit enter on a completion just go there
91 elif [ -d "/${out#*/}" ]; then
92 cd "/${out#*/}"
93 # prefer case sensitive
94 else
95 out=$(awk -v q="$*" -v s="$short" -v r="$recent" -F"|" '
96 BEGIN { split(q,a," "); if( r ) { f = 3 } else f = 2 }
ffa5795 @rupa hide directories that no longer exist, remove a debug
authored
97 {
98 if( system("test -d \"" $1 "\"") ) next
116a4c3 @rupa allow / in search terms, and documentation
authored
99 for( i in a ) $1 !~ a[i] && $1 = ""
b57503a @rupa major refactoring. add --s - shortest match
authored
100 if( $1 ) {
101 if( s ) {
102 if( length($1) <= length(x) ) {
103 x = $1
104 } else if( ! x ) x = $1
105 } else if( $f >= dx ) { x = $1; dx = $f }
106 }
ffa5795 @rupa hide directories that no longer exist, remove a debug
authored
107 }
8d2f4d1 @rupa dunno why it thinks this is different
authored
108 END {
b57503a @rupa major refactoring. add --s - shortest match
authored
109 if( ! x ) {
110 close(FILENAME)
111 while( getline < FILENAME ) {
112 if( system("test -d \"" $1 "\"") ) continue
113 for( i in a ) tolower($1) !~ tolower(a[i]) && $1 = ""
114 if( $1 ) {
115 if( s ) {
116 if( length($1) <= length(x) ) {
117 x = $1
118 } else if( ! x ) x = $1
119 } else if( $f >= dx ) { x = $1; dx = $f }
120 }
121 }
8d2f4d1 @rupa dunno why it thinks this is different
authored
122 }
b57503a @rupa major refactoring. add --s - shortest match
authored
123 if( x ) print x
8d2f4d1 @rupa dunno why it thinks this is different
authored
124 }
b57503a @rupa major refactoring. add --s - shortest match
authored
125 ' $jfile)
126 [ "$out" ] && cd "$out"
8d2f4d1 @rupa dunno why it thinks this is different
authored
127 fi
128 }
116a4c3 @rupa allow / in search terms, and documentation
authored
129 # tab completion for j
1ad373a @rupa ugly hack to deal with completions after several arguments. Basically…
authored
130 complete -C 'j --complete "$COMP_LINE"' j
116a4c3 @rupa allow / in search terms, and documentation
authored
131 # populate directory list. avoid clobbering other PROMPT_COMMANDs.
8d2f4d1 @rupa dunno why it thinks this is different
authored
132 PROMPT_COMMAND='j --add "$(pwd -P)";'"$PROMPT_COMMAND"
Something went wrong with that request. Please try again.