Last active
May 10, 2016 11:13
-
-
Save pasela/9643be6205d2d00cd874c956b4490595 to your computer and use it in GitHub Desktop.
[shell] interactive file selection and interactive change directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# specify your favorite filtering tool | |
if [[ -z "$INTERACTIVE_SELECTOR" ]]; then | |
if type fzy >/dev/null 2>&1; then | |
export INTERACTIVE_SELECTOR=fzy | |
elif type peco >/dev/null 2>&1; then | |
export INTERACTIVE_SELECTOR=peco | |
elif type fzf >/dev/null 2>&1; then | |
export INTERACTIVE_SELECTOR=fzf | |
fi | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias isel=interactive-select | |
function icd() | |
{ | |
cd "$(interactive-select -dh "$@")" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# select file and/or directory interactively using $INTERACTIVE_SELECTOR. | |
# | |
# USAGE: | |
# interactive-select [-dfhH] [BASE_DIR] | |
# | |
# OPTIONS: | |
# -d directory only | |
# -f file only | |
# -h hide dot-files | |
# -H include dot-files | |
# | |
# ARGS | |
# BASE_DIR base directory (default: .) | |
# | |
# ENV | |
# INTERACTIVE_SELECTOR interactive filtering tool such as peco, fzf, fzy... | |
if [[ -z "$INTERACTIVE_SELECTOR" ]]; then | |
echo "INTERACTIVE_SELECTOR not defined" >&2 | |
exit 1 | |
fi | |
while getopts dfhH OPT | |
do | |
case $OPT in | |
d) DIR_ONLY=1 ;; | |
f) FILE_ONLY=1 ;; | |
h) HIDE_DOT_FILES=1 ;; | |
H) HIDE_DOT_FILES= ;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
base=${1:-.} | |
FIND_OPT=(-print) | |
if [[ -n "$DIR_ONLY" && -n "$FILE_ONLY" ]]; then | |
: # do nothing | |
elif [[ -n "$DIR_ONLY" ]]; then | |
FIND_OPT=(-type d "${FIND_OPT[@]}") | |
elif [[ -n "$FILE_ONLY" ]]; then | |
FIND_OPT=(-type f "${FIND_OPT[@]}") | |
fi | |
if [[ -n "$HIDE_DOT_FILES" ]]; then | |
FIND_OPT=(-type d -name . -or -name ".*" -prune -or "${FIND_OPT[@]}") | |
fi | |
selected=$(cd "$base" && find . "${FIND_OPT[@]}" | \ | |
sed -e 's#^./##' | $INTERACTIVE_SELECTOR) && echo "$base/$selected" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
小難しいことしている理由
.git
とか邪魔(-h
で除外、-H
で含める(-h
上書き用))-f
)、ディレクトリだけ(-d
)とか簡単に指定したいfindが複雑になった過程
普通に
find
すると指定したパスも付いた状態で出力されるので絞り込むときに余計にマッチしたりして邪魔になる。-printf
の%P
を使うとちょうど指定したパス以下のみ出力してくれる……が、-printf
はGNU版にしかなかった。$ find /usr/local/lib -type d -printf "%P\n" zsh guilt pkgconfig java
仕方ないので
sed
で先頭だけ削ろうとしたら~/foo
やシンボリックリンクの時にうまくいかなかったのでcd
してからfind .
するようにした。そうしたら、ドットファイルを除外する
-name ".*" -prune
がカレントの.
に引っかかるせいなのか何も出てこなくなった。なので先頭に
-name . -or
を入れることになった。で、
./
をsed
で削って…という感じでこんなに長くなった。