Skip to content
This repository
Newer
Older
100644 117 lines (116 sloc) 3.584 kb
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
1 # maintains a jump-list of directories you actually use
2 # old directories eventually fall off the list
e597cd36 »
2009-02-18 rename some variables for clarity, and credit autojump's author by name
3 # inspired by Joel Schaerer's http://wiki.github.com/joelthelion/autojump
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
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:
07f18e6e »
2009-02-23 use local variables
11 # j [--h[elp]] [--r] [--l] [regex1 ... regexn]
12 # with no args, returns full list (same as j --l)
13 # regex1 ... regexn jump to the most used directory matching all masks
14 # --l show the list instead of jumping
116a4c36 »
2009-03-05 allow / in search terms, and documentation
15 # --r order by recently used instead of most used. (If this
07f18e6e »
2009-02-23 use local variables
16 # option is used, it must be the first option.)
19219380 »
2009-02-20 update documentation for internal reasons
17 #
116a4c36 »
2009-03-05 allow / in search terms, and documentation
18 # TIPS:
19 # Some handy aliases:
20 # alias jl='j --l'
21 # alias jr='j --r'
22 # alias jrl='j --r --l'
23 #
19219380 »
2009-02-20 update documentation for internal reasons
24 # CREDITS:
25 # Joel Schaerer aka joelthelion for autojump
26 # Daniel Drucker aka dmd for finding bugs and making me late for lunch
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
27 j() {
28 # change jfile if you already have a .j file for something else
07f18e6e »
2009-02-23 use local variables
29 local jfile=$HOME/.j
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
30 if [ "$1" = "--add" ]; then
31 shift
32 # we're in $HOME all the time, let something else get all the good letters
33 [ "$*" = "$HOME" ] && return
1e253496 »
2009-02-20 support for sorting by most recently accessed with --r and --lr options
34 awk -v q="$*" -v t="$(date +%s)" -F"|" '
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
35 $2 >= 1 {
1e253496 »
2009-02-20 support for sorting by most recently accessed with --r and --lr options
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 }
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
45 }
46 END {
1e253496 »
2009-02-20 support for sorting by most recently accessed with --r and --lr options
47 if( !found ) l[q] = 1 && d[q] = t
e597cd36 »
2009-02-18 rename some variables for clarity, and credit autojump's author by name
48 if( count > 1000 ) {
1e253496 »
2009-02-20 support for sorting by most recently accessed with --r and --lr options
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]
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
51 }
4c79e1f7 »
2009-02-20 put back error suppression i removed for debugging
52 ' $jfile 2>/dev/null > $jfile.tmp
afe869ab »
2009-02-17 Fixed 'mv' asking to overwrite ~/.j
53 mv -f $jfile.tmp $jfile
64452588 »
2009-02-20 change --rl to --l --r to aid in aliasing
54 elif [ "$1" = "--h" -o "$1" = "--help" ]; then
f9836a4f »
2009-02-20 had ordering wrong for aliasing.
55 echo "j [--h[elp]] [--r] [--l] [regex1 ... regexn]"
56 elif [ "$1" = "" -o "$1$2" = "--r" -o "$1" = "--l" -o "$1$2" = "--r--l" ];then
57 [ "$1" = "--r" ] && local r=r && shift
58 [ "$1" = "--l" ] && shift
59 awk -v q="$*" -v t="$(date +%s)" -v r="$r" -F"|" '
60 BEGIN { f = 2; split(q,a," "); if( r ) f = 3 }
64452588 »
2009-02-20 change --rl to --l --r to aid in aliasing
61 {
ffa57958 »
2009-03-05 hide directories that no longer exist, remove a debug
62 if( system("test -d \"" $1 "\"") ) next
64452588 »
2009-02-20 change --rl to --l --r to aid in aliasing
63 for( i in a ) $1 !~ a[i] && $1 = ""
64 if( $1 ) if( f == 3 ) { print t - $f "\t" $1 } else print $f "\t" $1
65 }
66 ' $jfile 2>/dev/null | sort -n$r
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
67 # for completion
1e253496 »
2009-02-20 support for sorting by most recently accessed with --r and --lr options
68 elif [ "$1" = "--complete" -o "$2" = "--complete" ];then
8530507c »
2009-02-18 fix bash completion to account for all arguments. wow, that was a pain
69 awk -v q="$2" -F"|" '
1e253496 »
2009-02-20 support for sorting by most recently accessed with --r and --lr options
70 BEGIN {
71 if( substr(q,1,5) == "j --r" ) {
72 split(substr(q,7),a," ")
73 } else split(substr(q,3),a," ")
74 }
ffa57958 »
2009-03-05 hide directories that no longer exist, remove a debug
75 {
76 if( system("test -d \"" $1 "\"") ) next
77 for( i in a ) $1 !~ a[i] && $1 = ""; if( $1 ) print $1
78 }
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
79 ' $jfile 2>/dev/null
80 else
116a4c36 »
2009-03-05 allow / in search terms, and documentation
81 # if we hit enter on a completion just go there (ugh, this is ugly)
82 local x=$*; x=/${x#*/}
83 [ -d "$x" ] && cd "$x" && return
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
84 # prefer case sensitive
07f18e6e »
2009-02-23 use local variables
85 local cd=$(awk -v q="$*" -F"|" '
1e253496 »
2009-02-20 support for sorting by most recently accessed with --r and --lr options
86 BEGIN {
87 if( substr(q,1,3) == "--r" ) {
88 split(substr(q,5),a," ")
89 f = 3
90 } else {
91 split(q,a," ")
92 f = 2
93 }
94 }
ffa57958 »
2009-03-05 hide directories that no longer exist, remove a debug
95 {
96 if( system("test -d \"" $1 "\"") ) next
116a4c36 »
2009-03-05 allow / in search terms, and documentation
97 for( i in a ) $1 !~ a[i] && $1 = ""
98 if( $1 ) { print $f "\t" $1; x = 1 }
ffa57958 »
2009-03-05 hide directories that no longer exist, remove a debug
99 }
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
100 END {
101 if( x ) exit
102 close(FILENAME)
103 while( getline < FILENAME ) {
ffa57958 »
2009-03-05 hide directories that no longer exist, remove a debug
104 if( system("test -d \"" $1 "\"") ) continue
e597cd36 »
2009-02-18 rename some variables for clarity, and credit autojump's author by name
105 for( i in a ) tolower($1) !~ tolower(a[i]) && $1 = ""
1e253496 »
2009-02-20 support for sorting by most recently accessed with --r and --lr options
106 if( $1 ) print $f "\t" $1
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
107 }
108 }
109 ' $jfile 2>/dev/null | sort -nr | head -n 1 | cut -f 2)
110 [ "$cd" ] && cd "$cd"
111 fi
112 }
116a4c36 »
2009-03-05 allow / in search terms, and documentation
113 # tab completion for j
1ad373a6 »
2009-02-18 ugly hack to deal with completions after several arguments. Basically…
114 complete -C 'j --complete "$COMP_LINE"' j
116a4c36 »
2009-03-05 allow / in search terms, and documentation
115 # populate directory list. avoid clobbering other PROMPT_COMMANDs.
8d2f4d15 »
2009-02-17 dunno why it thinks this is different
116 PROMPT_COMMAND='j --add "$(pwd -P)";'"$PROMPT_COMMAND"
Something went wrong with that request. Please try again.