Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 3914 enhance Ctrl-t [bash shell] #3918

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ EOF
else
echo "eval \"\$(fzf --$shell)\"" >> "$src"
fi
if [[ "$shell" = bash ]]; then
bash_override=shell/bash-override
[ -f "$bash_override" ] && echo >> "$src" && cat "$bash_override" >> "$src"
fi
else
cat >> "$src" << EOF
# Auto-completion
Expand Down
48 changes: 48 additions & 0 deletions shell/bash-override
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Override defaults for improved ctrl-t experience
__fzf_find() {
command ls -1t $1;
command find -L $1 -mindepth 2 \( -path '*/\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \) -prune \
-o -type f -printf '%P\n' \
-o -type d -printf '%P\n' \
-o -type l -printf '%P\n' 2> /dev/null
}

__fzf_select__() {
local cmd opts
local dir=$1
shift
cmd="${FZF_CTRL_T_COMMAND:-"__fzf_find $dir"}"
opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS -m"
eval "$cmd" |
FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) "$@" |
while read -r item; do
if [ "$dir" == "." ]; then
printf '%q ' "$item" # escape special chars
else
printf '%q/%q ' "$dir" "$item"
fi
done
}

fzf-file-widget() {
local trailing_spaces=$(echo -n "${READLINE_LINE:0:$READLINE_POINT}" | sed "s/^.*\S//")
local word=$(echo -n "${READLINE_LINE:0:$READLINE_POINT}" | sed "s/\s*$//"| awk '{print $NF}')
local dir=$(echo "${word/#\~/$HOME}" | sed "s#/\+#/#g; s#/\$##")
if [[ $READLINE_POINT -eq 0 || -n "$trailing_spaces" || ! -d "$dir" ]]; then
local maybe_space=""
[[ $READLINE_POINT -gt 0 && -z "$trailing_spaces" ]] && maybe_space=" "
local selected="$(__fzf_select__ . "$@")"
if [ -n "$selected" ]; then
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$maybe_space$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$((READLINE_POINT + ${#maybe_space} + ${#selected}))
fi
else
local selected="$(__fzf_select__ "$dir" "$@")"
if [ -n "$selected" ]; then
local pre_word=$((READLINE_POINT - ${#word}))
READLINE_LINE="${READLINE_LINE:0:$pre_word}$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$((pre_word + ${#selected}))
fi
fi
}
# end of non default section