From f2d62ffae4673213682d6823240650387835d66a Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Fri, 22 May 2020 17:33:45 +0100 Subject: [PATCH] Changes behaviour of ctrl t in zsh Now pressing ctrl t will sometimes search directories if more appropriate. Pressing alt+c will now to whatever ctrl+t doesn't do if I ever need the alternative type. --- shells/zsh/includes/fzf.zsh | 98 ++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/shells/zsh/includes/fzf.zsh b/shells/zsh/includes/fzf.zsh index 5523a1ad..ae9d60d8 100644 --- a/shells/zsh/includes/fzf.zsh +++ b/shells/zsh/includes/fzf.zsh @@ -2,11 +2,10 @@ export FZF_DEFAULT_COMMAND="fd --type f --hidden --follow --color=always --exclude .git --exclude .PlayOnLinux --exclude \"PlayOnLinux\'s virtual drives\"" export FZF_DEFAULT_OPTS="--reverse --ansi --height 40%" export FZF_CTRL_R_OPTS="" -# this is the argument completeion optionm, use the same command -export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND" -export FZF_CTRL_T_OPTS="$FZF_DEFAULT_OPTS --ansi --preview \"bat --style=numbers --color=always {}\"" -#export FZF_COMPLETION_TRIGGER='' +export FZF_CTRL_T_OPTS="$FZF_DEFAULT_OPTS --ansi --preview \"fzf-preview {}\"" +export FZF_ALT_C_OPTS="$FZF_DEFAULT_OPTS --ansi --preview \"fzf-preview {}\"" +# Ctrl t and alt c command are set below sourced="False" if [ -e /usr/share/fzf/key-bindings.zsh ]; then @@ -18,61 +17,60 @@ elif [ -e /usr/share/doc/fzf/examples/key-bindings.zsh ]; then source /usr/share/doc/fzf/examples/completion.zsh sourced="True" fi - -if [ "$sourced" = "True" ]; then +if [ "$sourced" = "True" ]; then - # on_word_replace(){ - # setopt localoptions noshwordsplit noksh_arrays noposixbuiltins - # local word="${LBUFFER##* }${RBUFFER%% *}" - # if [ -n "$word" ]; then - # local changeto=$(jhswap "$word" ) - # local lastWord="$changeto" - # local LWORDS=$(echo $LBUFFER | tr ' ' '\n' | wc -l) - # local RWORDS=$(echo $RBUFFER | tr ' ' '\n' | wc -l) - # if [ "$LWORDS" -gt "1" ]; then - # LBUFFER="${LBUFFER% *} $lastWord" - # else - # LBUFFER="$lastWord" - # fi - # if [ "$RWORDS" -gt "1" ]; then - # RBUFFER=" ${RBUFFER#* }" - # else - # RBUFFER="" - # fi - # zle reset-prompt - # zle -R - # return 0 - # fi - - # } - - _fzf_complete_yay(){ - local tokens=(${(z)LBUFFER}) - if [ "${tokens[-1]}" = "-S" -a "${LBUFFER[-1]}" = " " ]; then - notify-send "complete" - fi - return 1 + fzf_files(){ + # The directory can be passed as arg1 + dir="${1:-$PWD}" + fd --type f --hidden --follow --color=always --exclude .git --exclude .PlayOnLinux --exclude "PlayOnLinux's virtual drives" . "$dir" } + fzf_dirs(){ + dir="${1:-$PWD}" + fd --type d --hidden --follow --color=always --exclude .git --exclude .PlayOnLinux --exclude "PlayOnLinux's virtual drives" . "$dir" + } + # This function is used to provide the ctrl+t completion for fzf + # I find having ctrl+t for files and alt+c for directories quite jaring. + # This function will try to determine (based on the command) if it should complete files or directories. + # If it get's it wrong, alt_c will still work by doing the oposite of what it thinks + fzf_crl_t(){ + local tokens cmd ret=1 lastWord oposite="${1:-false}" + # http://zsh.sourceforge.net/FAQ/zshfaq03.html + # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags + + # Use zsh's shell parsing to split lbuffer into items + # This takes into account quoting and escaping + tokens=(${(z)LBUFFER}) + + + # Assume the first element + # TODO: make this work for multiple commands chained with | or > or && etc. + # TODO: make this work when command prepended with variables eg a=2 foo bar + # foo is the command name + cmd="${tokens[1]}" + lastWord="${tokens[-1]}" + + if [ "${LBUFFER[-1]}" != " " ]; then + current="$lastWord" + else + current="$PWD" + fi + # An array of commands that should use dir search instead of file search + dirCommands=("cd" "find" "fd") + if [ "$oposite" = "false" ]; then + (( ${dirCommands[(I)$cmd]} )) && fzf_dirs "$current" || fzf_files "$current" + else + (( ${dirCommands[(I)$cmd]} )) && fzf_files "$current" || fzf_dirs "$current" + fi - - - - - - - - - - - - - + } + export FZF_CTRL_T_COMMAND="fzf_crl_t" + export FZF_ALT_C_COMMAND="fzf_crl_t true" fi