Fixed the substitution of HOME.
[selector.git] / bash-selector.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 # This script installs two key-bindings:
21 #
22 # Alt-r for selector-based command history
23 #
24 # Alt-c for selector-based directoy history
25 #
26 # Note that you have to call it with ". bash-selector.sh" otherwise
27 # the key-bindings will not be effective in your current bash
28
29 ######################################################################
30 # Selector-based command history
31 ######################################################################
32
33 function selector-history () {
34     selector --bash -u -c 7,4,0,3 -q <(history)
35 }
36
37 ######################################################################
38 # Selector-based directory history
39 ######################################################################
40
41 # The file where we will keep track of the directories
42
43 export SELECTOR_CD_HISTORY
44
45 [[ "${SELECTOR_CD_HISTORY}" ]] || SELECTOR_CD_HISTORY=${HOME}/.selector-cd-history
46
47 # The function to use in place of the standard "cd"
48
49 function selector-cd () {
50     if [[ -z "$1" ]]; then
51         cd
52     else
53         cd "$@"
54     fi
55     TMP=$(mktemp /tmp/selector-cd.XXXXXX)
56     tail -1000 < ${SELECTOR_CD_HISTORY} > ${TMP}
57     echo $PWD | sed -e "s!^${HOME}!~!" >> ${TMP}
58     cat ${TMP} > ${SELECTOR_CD_HISTORY}
59     rm -f ${TMP}
60 }
61
62 function selector-cd-search () {
63     PATH_TEMP=$(mktemp /tmp/selector-cd-path.XXXXXX)
64     selector -u -t "cd" -l 10000 -d -i -c 7,2,0,3 -o ${PATH_TEMP} -q ${SELECTOR_CD_HISTORY}
65     NEW_PATH="$(cat ${PATH_TEMP} | sed -e 's!~!'${HOME}'!')"
66     if [[ -s "${NEW_PATH}" ]]; then
67         selector-cd "$(cat ${PATH_TEMP} | sed -e 's!^~!'${HOME}'!')"
68     fi
69     \rm ${PATH_TEMP}
70 }
71
72 alias cd=selector-cd
73
74 ######################################################################
75 # The key-bindings themselves
76 ######################################################################
77
78 # M-r puts the selected history line in place of the current one
79
80 bind '"\C-[r":"\C-a\C-kselector-history\C-m"'
81
82 # M-t appends the selected history line and the end of the current one
83 # bind '"\C-[t":"\C-a\C-kselector-history\C-m\C-a\C-y\C-e"'
84
85 # M-c provides a dynamic list of directories to cd into
86
87 bind '"\C-[c":"\C-a\C-kselector-cd-search\C-m"'