From: Francois Fleuret Date: Mon, 24 Oct 2011 19:56:20 +0000 (+0200) Subject: Initial commit. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=683fae45816f0a0118a94cef7e546b7566c29dd9;p=selector.git Initial commit. --- diff --git a/bash-selector.sh b/bash-selector.sh new file mode 100755 index 0000000..abf4059 --- /dev/null +++ b/bash-selector.sh @@ -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 . # +# # +# Written by and Copyright (C) Francois Fleuret # +# Contact 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"'