Changed the help for the -j option.
[selector.git] / bash-selector.sh
1 #!/bin/bash
2
3 #  selector is a simple command line utility for selection of strings
4 #  with a dynamic pattern-matching.
5 #
6 #  Copyright (c) 2011, 2012 Francois Fleuret
7 #  Written by Francois Fleuret <francois@fleuret.org>
8 #
9 #  This file is part of selector.
10 #
11 #  selector is free software: you can redistribute it and/or modify
12 #  it under the terms of the GNU General Public License version 3 as
13 #  published by the Free Software Foundation.
14 #
15 #  selector is distributed in the hope that it will be useful, but
16 #  WITHOUT ANY WARRANTY; without even the implied warranty of
17 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 #  General Public License for more details.
19 #
20 #  You should have received a copy of the GNU General Public License
21 #  along with selector.  If not, see <http://www.gnu.org/licenses/>.
22
23 #  This script installs two key-bindings:
24 #
25 #  Alt-r for selector-based command history
26 #
27 #  Alt-c for selector-based directoy history
28 #
29 #  Note that you have to call it with "source bash-selector.sh"
30 #  otherwise the key-bindings will not be effective in your current
31 #  bash
32
33 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
34     echo "This script must be called with 'source $(basename $0)'" >&2
35     exit 1
36 fi
37
38 ######################################################################
39 # Selector-based command history
40 ######################################################################
41
42 function selector-history () {
43     selector --bash -j -y -u -c 7,4,0,3 -q <(history)
44 }
45
46 ######################################################################
47 # Selector-based directory history
48 ######################################################################
49
50 # The file where we will keep track of the directories
51
52 export SELECTOR_CD_HISTORY
53
54 [[ "${SELECTOR_CD_HISTORY}" ]] || SELECTOR_CD_HISTORY=${HOME}/.selector-cd-history
55
56 # The function to use in place of the standard "cd"
57
58 function selector-cd () {
59     if [[ -z "$1" ]]; then
60         cd
61     else
62         cd "$@"
63     fi
64
65     if [[ -f ${SELECTOR_CD_HISTORY} ]]; then
66         TMP=$(mktemp /tmp/selector-cd.XXXXXX)
67         tail -999 < ${SELECTOR_CD_HISTORY} > ${TMP}
68         cat ${TMP} > ${SELECTOR_CD_HISTORY}
69         rm -f ${TMP}
70     fi
71
72     echo $PWD | sed -e "s!^${HOME}!~!" >> ${SELECTOR_CD_HISTORY}
73 }
74
75 function selector-cd-search () {
76     if [[ -f ${SELECTOR_CD_HISTORY} ]]; then
77         PATH_TEMP=$(mktemp /tmp/selector-cd-path.XXXXXX)
78         selector -j -y -u -t "cd" -l 1000 -d -i -c 7,2,0,3 -o ${PATH_TEMP} -q ${SELECTOR_CD_HISTORY}
79         NEW_PATH="$(cat ${PATH_TEMP} | sed -e 's!~!'${HOME}'!')"
80         if [[ -s "${NEW_PATH}" ]]; then
81             selector-cd "$(cat ${PATH_TEMP} | sed -e 's!^~!'${HOME}'!')"
82         fi
83         \rm ${PATH_TEMP}
84     else
85         echo "No cd history file ${SELECTOR_CD_HISTORY}." >&2
86     fi
87 }
88
89 alias cd=selector-cd
90
91 ######################################################################
92 # The key-bindings themselves
93 ######################################################################
94
95     # M-t appends the selected history line and the end of the current
96     # one bind '"\C-[t":"\C-a\C-kselector-history\C-m\C-a\C-y\C-e"'
97
98 if [[ "$1" ]]; then
99
100     while [[ "$1" ]]; do
101
102         case "$1" in
103
104             --hist)
105                 # M-r puts the selected history line in place of the current one
106                 bind '"\C-[r":"\C-a\C-kselector-history\C-m"'
107                 ;;
108
109             --cd)
110                 # M-c provides a dynamic list of directories to cd into
111                 bind '"\C-[c":"\C-a\C-kselector-cd-search\C-m"'
112                 ;;
113
114             *)
115                 echo "Unknown argument $1" >&2
116                 ;;
117         esac
118
119         shift
120
121     done
122
123 else
124
125     echo "source bash-selector.sh <--hist|--cd> [...]"
126     echo
127     echo "Defines bash functions, and installs key-bindings and aliases to use selector"
128     echo "for history search with M-r and/or intelligent cd history with M-c."
129
130 fi