Initial commit.
authorFrancois Fleuret <francois@fleuret.org>
Mon, 24 Oct 2011 19:56:20 +0000 (21:56 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Mon, 24 Oct 2011 19:56:20 +0000 (21:56 +0200)
bash-selector.sh [new file with mode: 0755]

diff --git a/bash-selector.sh b/bash-selector.sh
new file mode 100755 (executable)
index 0000000..abf4059
--- /dev/null
@@ -0,0 +1,65 @@
+#!/bin/bash
+
+#########################################################################
+# This program is free software: you can redistribute it and/or modify  #
+# it under the terms of the version 3 of the GNU General Public License #
+# as published by the Free Software Foundation.                         #
+#                                                                       #
+# This program is distributed in the hope that it will be useful, but   #
+# WITHOUT ANY WARRANTY; without even the implied warranty of            #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
+# General Public License for more details.                              #
+#                                                                       #
+# You should have received a copy of the GNU General Public License     #
+# along with this program. If not, see <http://www.gnu.org/licenses/>.  #
+#                                                                       #
+# Written by and Copyright (C) Francois Fleuret                         #
+# Contact <francois.fleuret@idiap.ch> for comments & bug reports        #
+#########################################################################
+
+set -e
+
+# Selector based history
+
+function selector-history () {
+    selector --bash -u -c 7,4,0,3 -q <(history)
+}
+
+# Maintains a list of visited directories and provide a selector-based
+# command to go back to any of them.
+
+export SELECTOR_CD_HISTORY
+
+[[ "${SELECTOR_CD_HISTORY}" ]] || SELECTOR_CD_HISTORY=${HOME}/.selector-cd-history
+
+function selector-cd () {
+    if [[ -z "$1" ]]; then
+        cd
+    else
+        cd "$@"
+    fi
+    echo $PWD | sed -e "s!${HOME}!~!" >> ${SELECTOR_CD_HISTORY}
+}
+
+function selector-cd-search () {
+    PATH_TEMP=$(mktemp /tmp/selector-cd-path.XXXXXX)
+    selector -u -t "cd" -l 10000 -d -i -c 7,2,0,3 -o ${PATH_TEMP} -q ${SELECTOR_CD_HISTORY}
+    NEW_PATH="$(cat ${PATH_TEMP} | sed -e 's!~!'${HOME}'!')"
+    if [[ -s "${NEW_PATH}" ]]; then
+        selector-cd "$(cat ${PATH_TEMP} | sed -e 's!~!'${HOME}'!')"
+    fi
+    \rm ${PATH_TEMP}
+}
+
+alias cd=selector-cd
+
+# M-r puts the selected history line in place of the current one
+
+bind '"\C-[r":"\C-a\C-kselector-history\C-m"'
+
+# M-t appends the selected history line and the end of the current one
+# bind '"\C-[t":"\C-a\C-kselector-history\C-m\C-a\C-y\C-e"'
+
+# M-c provides a dynamic list of directories to cd into
+
+bind '"\C-[c":"\C-a\C-kselector-cd-search\C-m"'