diff options
Diffstat (limited to 'shell')
-rw-r--r-- | shell/completion.bash | 39 | ||||
-rw-r--r-- | shell/key-bindings.bash | 34 | ||||
-rw-r--r-- | shell/key-bindings.fish | 57 | ||||
-rw-r--r-- | shell/key-bindings.zsh | 44 |
4 files changed, 91 insertions, 83 deletions
diff --git a/shell/completion.bash b/shell/completion.bash index e609d2be..79160987 100644 --- a/shell/completion.bash +++ b/shell/completion.bash @@ -5,6 +5,8 @@ # / __/ / /_/ __/ # /_/ /___/_/-completion.bash # +# - $FZF_TMUX (default: 1) +# - $FZF_TMUX_HEIGHT (default: '40%') # - $FZF_COMPLETION_TRIGGER (default: '**') # - $FZF_COMPLETION_OPTS (default: empty) @@ -14,9 +16,10 @@ _fzf_orig_completion_filter() { } _fzf_opts_completion() { - local cur opts + local cur prev opts COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" opts=" -x --extended -e --extended-exact @@ -25,20 +28,35 @@ _fzf_opts_completion() { -d --delimiter +s --no-sort --tac + --tiebreak -m --multi --no-mouse - +c --no-color - +2 --no-256 + --color --black --reverse + --no-hscroll + --inline-info --prompt -q --query -1 --select-1 -0 --exit-0 -f --filter --print-query + --expect + --toggle-sort --sync" + case "${prev}" in + --tiebreak) + COMPREPLY=( $(compgen -W "length begin end index" -- ${cur}) ) + return 0 + ;; + --color) + COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) ) + return 0 + ;; + esac + if [[ ${cur} =~ ^-|\+ ]]; then COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 @@ -65,7 +83,8 @@ _fzf_handle_dynamic_completion() { } _fzf_path_completion() { - local cur base dir leftover matches trigger cmd + local cur base dir leftover matches trigger cmd fzf + [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf" cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g') COMPREPLY=() trigger=${FZF_COMPLETION_TRIGGER:-**} @@ -81,7 +100,7 @@ _fzf_path_completion() { leftover=${leftover/#\/} [ "$dir" = './' ] && dir='' tput sc - matches=$(find -L "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do + matches=$(find -L "$dir"* $1 2> /dev/null | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do printf "%q$3 " "$item" done) matches=${matches% } @@ -105,7 +124,8 @@ _fzf_path_completion() { } _fzf_list_completion() { - local cur selected trigger cmd src + local cur selected trigger cmd src fzf + [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf" read -r src cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g') trigger=${FZF_COMPLETION_TRIGGER:-**} @@ -114,7 +134,7 @@ _fzf_list_completion() { cur=${cur:0:${#cur}-${#trigger}} tput sc - selected=$(eval "$src | fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ') + selected=$(eval "$src | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ') selected=${selected% } tput rc @@ -149,9 +169,10 @@ _fzf_dir_completion() { _fzf_kill_completion() { [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1 - local selected + local selected fzf + [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf" tput sc - selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ') + selected=$(ps -ef | sed 1d | $fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ') tput rc if [ -n "$selected" ]; then diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index 90112475..362e4428 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -1,6 +1,6 @@ # Key bindings # ------------ -__fsel() { +__fzf_select__() { command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ -o -type f -print \ -o -type d -print \ @@ -12,7 +12,11 @@ __fsel() { if [[ $- =~ i ]]; then -__fsel_tmux() { +__fzfcmd() { + [ ${FZF_TMUX:-1} -eq 1 ] && echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" +} + +__fzf_select_tmux__() { local height height=${FZF_TMUX_HEIGHT:-40%} if [[ $height =~ %$ ]]; then @@ -20,13 +24,17 @@ __fsel_tmux() { else height="-l $height" fi - tmux split-window $height "cd $(printf %q "$PWD");bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'" + tmux split-window $height "cd $(printf %q "$PWD");bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fzf_select__)\"'" } -__fcd() { +__fzf_cd__() { local dir dir=$(command find -L ${1:-.} \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ - -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m) && printf 'cd %q' "$dir" + -o -type d -print 2> /dev/null | sed 1d | cut -b3- | $(__fzfcmd) +m) && printf 'cd %q' "$dir" +} + +__fzf_history__() { + HISTTIMEFORMAT= history | $(__fzfcmd) +s --tac +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r | sed "s/ *[0-9]* *//" } __use_tmux=0 @@ -38,16 +46,16 @@ if [ -z "$(set -o | \grep '^vi.*on')" ]; then # CTRL-T - Paste the selected file path into the command line if [ $__use_tmux -eq 1 ]; then - bind '"\C-t": " \C-u \C-a\C-k$(__fsel_tmux)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"' + bind '"\C-t": " \C-u \C-a\C-k$(__fzf_select_tmux__)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"' else - bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"' + bind '"\C-t": " \C-u \C-a\C-k$(__fzf_select__)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"' fi # CTRL-R - Paste the selected command from history into the command line - bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"' + bind '"\C-r": " \C-e\C-u$(__fzf_history__)\e\C-e\er"' # ALT-C - cd into the selected directory - bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"' + bind '"\ec": " \C-e\C-u$(__fzf_cd__)\e\C-e\er\C-m"' else bind '"\C-x\C-e": shell-expand-line' bind '"\C-x\C-r": redraw-current-line' @@ -55,18 +63,18 @@ else # CTRL-T - Paste the selected file path into the command line # - FIXME: Selected items are attached to the end regardless of cursor position if [ $__use_tmux -eq 1 ]; then - bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"' + bind '"\C-t": "\e$a \eddi$(__fzf_select_tmux__)\C-x\C-e\e0P$xa"' else - bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "' + bind '"\C-t": "\e$a \eddi$(__fzf_select__)\C-x\C-e\e0Px$a \C-x\C-r\exa "' fi bind -m vi-command '"\C-t": "i\C-t"' # CTRL-R - Paste the selected command from history into the command line - bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"' + bind '"\C-r": "\eddi$(__fzf_history__)\C-x\C-e\e$a\C-x\C-r"' bind -m vi-command '"\C-r": "i\C-r"' # ALT-C - cd into the selected directory - bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"' + bind '"\ec": "\eddi$(__fzf_cd__)\C-x\C-e\C-x\C-r\C-m"' bind -m vi-command '"\ec": "i\ec"' fi diff --git a/shell/key-bindings.fish b/shell/key-bindings.fish index 3a86decb..b86a4ef3 100644 --- a/shell/key-bindings.fish +++ b/shell/key-bindings.fish @@ -7,18 +7,6 @@ function fzf_key_bindings set -g TMPDIR /tmp end - function __fzf_list - command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ - -o -type f -print \ - -o -type d -print \ - -o -type l -print 2> /dev/null | sed 1d | cut -b3- - end - - function __fzf_list_dir - command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) \ - -prune -o -type d -print 2> /dev/null | sed 1d | cut -b3- - end - function __fzf_escape while read item echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' ' @@ -26,25 +14,17 @@ function fzf_key_bindings end function __fzf_ctrl_t - if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ] - # FIXME need to handle directory with double-quotes - tmux split-window (__fzf_tmux_height) "cd \"$PWD\";fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'" - else - __fzf_list | fzf -m > $TMPDIR/fzf.result - and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape) - commandline -f repaint - rm -f $TMPDIR/fzf.result - end - end - - function __fzf_ctrl_t_tmux - __fzf_list | fzf -m > $TMPDIR/fzf.result - and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result | __fzf_escape) + command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ + -o -type f -print \ + -o -type d -print \ + -o -type l -print 2> /dev/null | sed 1d | cut -b3- | eval (__fzfcmd) -m > $TMPDIR/fzf.result + and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape) + commandline -f repaint rm -f $TMPDIR/fzf.result end function __fzf_ctrl_r - history | fzf +s +m > $TMPDIR/fzf.result + history | eval (__fzfcmd) +s +m --tiebreak=index --toggle-sort=ctrl-r > $TMPDIR/fzf.result and commandline (cat $TMPDIR/fzf.result) commandline -f repaint rm -f $TMPDIR/fzf.result @@ -52,25 +32,26 @@ function fzf_key_bindings function __fzf_alt_c # Fish hangs if the command before pipe redirects (2> /dev/null) - __fzf_list_dir | fzf +m > $TMPDIR/fzf.result + command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) \ + -prune -o -type d -print 2> /dev/null | sed 1d | cut -b3- | eval (__fzfcmd) +m > $TMPDIR/fzf.result [ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ] and cd (cat $TMPDIR/fzf.result) commandline -f repaint rm -f $TMPDIR/fzf.result end - function __fzf_tmux_height - if set -q FZF_TMUX_HEIGHT - set height $FZF_TMUX_HEIGHT - else - set height 40% - end - if echo $height | \grep -q -E '%$' - echo "-p "(echo $height | sed 's/%$//') + function __fzfcmd + set -q FZF_TMUX; or set FZF_TMUX 1 + + if [ $FZF_TMUX -eq 1 ] + if set -q FZF_TMUX_HEIGHT + echo "fzf-tmux -d$FZF_TMUX_HEIGHT" + else + echo "fzf-tmux -d40%" + end else - echo "-l $height" + echo "fzf" end - set -e height end bind \ct '__fzf_ctrl_t' diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh index 6eb80839..17608670 100644 --- a/shell/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -5,38 +5,29 @@ __fsel() { command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ -o -type f -print \ -o -type d -print \ - -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do + -o -type l -print 2> /dev/null | sed 1d | cut -b3- | $(__fzfcmd) -m | while read item; do printf '%q ' "$item" done echo } +__fzfcmd() { + [ ${FZF_TMUX:-1} -eq 1 ] && echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" +} + if [[ $- =~ i ]]; then -if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then - fzf-file-widget() { - local height - height=${FZF_TMUX_HEIGHT:-40%} - if [[ $height =~ %$ ]]; then - height="-p ${height%\%}" - else - height="-l $height" - fi - tmux split-window $height "cd $(printf %q "$PWD");zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'" - } -else - fzf-file-widget() { - LBUFFER="${LBUFFER}$(__fsel)" - zle redisplay - } -fi +fzf-file-widget() { + LBUFFER="${LBUFFER}$(__fsel)" + zle redisplay +} zle -N fzf-file-widget bindkey '^T' fzf-file-widget # ALT-C - cd into the selected directory fzf-cd-widget() { cd "${$(command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ - -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m):-.}" + -o -type d -print 2> /dev/null | sed 1d | cut -b3- | $(__fzfcmd) +m):-.}" zle reset-prompt } zle -N fzf-cd-widget @@ -44,11 +35,18 @@ bindkey '\ec' fzf-cd-widget # CTRL-R - Paste the selected command from history into the command line fzf-history-widget() { - local selected - if selected=$(fc -l 1 | fzf +s --tac +m -n2..,.. -q "$LBUFFER"); then + local selected restore_no_bang_hist + if selected=$(fc -l 1 | $(__fzfcmd) +s --tac +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r -q "$LBUFFER"); then num=$(echo "$selected" | head -1 | awk '{print $1}' | sed 's/[^0-9]//g') - LBUFFER=!$num - zle expand-history + if [ -n "$num" ]; then + LBUFFER=!$num + if setopt | grep nobanghist > /dev/null; then + restore_no_bang_hist=1 + unsetopt no_bang_hist + fi + zle expand-history + [ -n "$restore_no_bang_hist" ] && setopt no_bang_hist + fi fi zle redisplay } |