key-bindings.fish
This file
Path
shell/key-bindings.fish
223 lines · 8.8 KiB
Last changed by
fish: Improve custom CTRL-T command execution (#4909)bitraidbefore adoptionad151d8
History of this fileTake it
1# ____ ____2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/5# /_/ /___/_/ key-bindings.fish6#7# - $FZF_CTRL_T_COMMAND8# - $FZF_CTRL_T_OPTS9# - $FZF_CTRL_R_COMMAND10# - $FZF_CTRL_R_OPTS11# - $FZF_ALT_C_COMMAND12# - $FZF_ALT_C_OPTS1314# Key bindings15# ------------16function fzf_key_bindings1718 # The oldest supported fish version is 3.4.0. For this message being able to be19 # displayed on older versions, the command substitution syntax $() should not20 # be used anywhere in the script, otherwise the source command will fail.21 if string match -qr -- '^[12]\\.|^3\\.[0-3]' $version22 echo "fzf key bindings script requires fish version 3.4.0 or newer." >&223 return 124 else if not command -q fzf25 echo "fzf was not found in path." >&226 return 127 end2829 function __fzf_defaults30 # $argv[1]: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS31 # $argv[2..]: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS32 test -n "$FZF_TMUX_HEIGHT"; or set -l FZF_TMUX_HEIGHT 40%33 string join ' ' -- \34 "--height $FZF_TMUX_HEIGHT --min-height=20+ --bind=ctrl-z:ignore" $argv[1] \35 (test -r "$FZF_DEFAULT_OPTS_FILE"; and string join -- ' ' <$FZF_DEFAULT_OPTS_FILE) \36 $FZF_DEFAULT_OPTS $argv[2..]37 end3839 function __fzfcmd40 test -n "$FZF_TMUX_HEIGHT"; or set -l FZF_TMUX_HEIGHT 40%41 if test -n "$FZF_TMUX_OPTS"42 echo "fzf-tmux $FZF_TMUX_OPTS -- "43 else if test "$FZF_TMUX" = "1"44 echo "fzf-tmux -d$FZF_TMUX_HEIGHT -- "45 else46 echo "fzf"47 end48 end4950 function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath, fzf query, and optional -option= prefix'51 set -l fzf_query ''52 set -l prefix ''53 set -l dir '.'5455 set -l -- match_regex '(?<fzf_query>[\\s\\S]*?(?=\\n?$)$)'56 set -l -- prefix_regex '^-[^\\s=]+=|^-(?!-)\\S'5758 # Don't use option prefix if " -- " is preceded.59 string match -qv -- '* -- *' (string sub -l (commandline -Cp) -- (commandline -p))60 and set -- match_regex "(?<prefix>$prefix_regex)?$match_regex"6162 # Set $prefix and expanded $fzf_query with preserved trailing newlines.63 if string match -qr -- '^\\d\\d+|^[4-9]' $version64 # fish v4.0.0 and newer65 string match -q -r -- $match_regex (commandline --current-token --tokens-expanded | string collect -N)66 else67 string match -q -r -- $match_regex (commandline --current-token --tokenize | string collect -N)68 eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\\(?=~)|\\\\(?=\\$\\w)' '')69 end7071 if test -n "$fzf_query"72 # Normalize path in $fzf_query, set $dir to the longest existing directory.73 if string match -qr -- '^\\d\\d+|^4|^3\\.[5-9]' $version74 # fish v3.5.0 and newer75 set -- fzf_query (path normalize -- $fzf_query)76 set -- dir $fzf_query77 while not path is -d $dir78 set -- dir (path dirname $dir)79 end80 else81 string match -q -r -- '(?<fzf_query>^[\\s\\S]*?(?=\\n?$)$)' \82 (string replace -r -a -- '(?<=/)/|(?<!^)/+(?!\\n)$' '' $fzf_query | string collect -N)83 set -- dir $fzf_query84 while not test -d "$dir"85 set -- dir (dirname -z -- "$dir" | string split0)86 end87 end8889 if not string match -q -- '.' $dir; or string match -qr -- '^\\.(/|$)' $fzf_query90 # Strip $dir from $fzf_query - preserve trailing newlines.91 if string match -qr -- '^\\d\\d+|^[4-9]' $version92 # fish v4.0.0 and newer93 string match -q -r -- '^'(string escape --style=regex -- $dir)'/?(?<fzf_query>[\\s\\S]*)' $fzf_query94 else95 string match -q -r -- '^/?(?<fzf_query>[\\s\\S]*?(?=\\n?$)$)' \96 (string replace -- "$dir" '' $fzf_query | string collect -N)97 end98 end99 end100101 string escape -n -- "$dir" "$fzf_query" "$prefix"102 end103104 # Store current token in $dir as root for the 'find' command105 function fzf-file-widget -d "List files and folders"106 set -l commandline (__fzf_parse_commandline)107 set -l dir $commandline[1]108 set -l fzf_query $commandline[2]109 set -l prefix $commandline[3]110111 set -lx FZF_DEFAULT_OPTS (__fzf_defaults \112 "--reverse --walker=file,dir,follow,hidden --scheme=path" \113 "--multi $FZF_CTRL_T_OPTS --print0")114115 set -lx FZF_DEFAULT_OPTS_FILE116117 set -lx FZF_DEFAULT_COMMAND118119 if test -n "$FZF_CTRL_T_COMMAND"120 set -f result (eval $FZF_CTRL_T_COMMAND \| (__fzfcmd) --query=$fzf_query | string split0)121 else122 set -f result (eval (__fzfcmd) --walker-root=$dir --query=$fzf_query | string split0)123 end124125 test -n "$result"126 and commandline -rt -- (string join -- ' ' $prefix(string escape -n -- $result))' '127128 commandline -f repaint129 end130131 function fzf-history-widget -d "Show command history"132 set -l -- command_line (commandline)133 set -l -- current_line (commandline -L)134 set -l -- total_lines (count $command_line)135 set -l -- fzf_query (string escape -- $command_line[$current_line])136137 set -lx -- FZF_DEFAULT_COMMAND "builtin history -z --show-time=(set_color $fish_color_comment 2>/dev/null; or set_color normal)\"%F %a %T%t%s%t\"(set_color normal)"138139 # Enable syntax highlighting colors on fish v4.3.3 and newer140 if string match -qr -- '^\\d\\d+|^4\\.[4-9]|^4\\.3\\.[3-9]' $version141 set -a -- FZF_DEFAULT_COMMAND "--color=always"142 end143144 set -lx -- FZF_DEFAULT_OPTS (__fzf_defaults '' \145 '--with-nth=2.. --nth=2..,.. --scheme=history --multi --no-multi-line' \146 '--no-wrap --wrap-sign="\t\t\t↳ " --preview-wrap-sign="↳ " --freeze-left=1' \147 '--bind="alt-enter:become(set -g fzf_temp {+sf3..}; string join0 -- (string split0 -- <$fzf_temp | fish_indent -i); unlink $fzf_temp &>/dev/null)"' \148 '--bind="alt-t:change-with-nth(1,3..|3..|2..)"' \149 "--bind='shift-delete:execute-silent(eval builtin history delete -Ce -- (string escape -n -- (string split0 -- <{+sf3..})))+reload($FZF_DEFAULT_COMMAND)'" \150 "--bind=ctrl-r:toggle-sort,alt-r:toggle-raw --highlight-line $FZF_CTRL_R_OPTS" \151 '--accept-nth=3.. --delimiter="\t" --tabstop=4 --ansi --read0 --print0 --with-shell='(status fish-path)\\ -c)152153 # Add dynamic preview options if preview command isn't already set by user154 if string match -qvr -- '--preview[= ]' "$FZF_DEFAULT_OPTS"155 # Prepend the options to allow user overrides156 set -p -- FZF_DEFAULT_OPTS \157 '--bind="focus,multi,resize:bg-transform:if test \\"$FZF_COLUMNS\\" -gt 100 -a \\\\( \\"$FZF_SELECT_COUNT\\" -gt 0 -o \\\\( -z \\"$FZF_WRAP\\" -a (string join0 -- <{f3..} | string length) -gt (math $FZF_COLUMNS - (switch $FZF_WITH_NTH; case 2..; echo 13; case 1,3..; echo 25; case 3..; echo 1; end)) \\\\) -o (string split0 -- <{sf3..} | fish_indent | count) -gt 1 \\\\); echo show-preview; else; echo hide-preview; end"' \158 '--preview="test \\"$FZF_SELECT_COUNT\\" -gt 0; and string split0 -- <{+sf3..} | fish_indent (string match -q -- 3.\\\\* $version; or echo -- --only-indent) --ansi; and echo -n \\\\n; string collect -- \\\\#\\\\ {1} (string split0 -- <{sf3..}) | fish_indent --ansi"' \159 '--preview-window="right,50%,wrap-word,follow,info,hidden"'160 end161162 set -lx FZF_DEFAULT_OPTS_FILE163164 # Merge history from other sessions before searching165 test -z "$fish_private_mode"; and builtin history merge166167 if set -l result (eval $FZF_DEFAULT_COMMAND \| (__fzfcmd) --query=$fzf_query | string split0)168 if test "$total_lines" -eq 1169 commandline -- $result170 else171 set -l a (math $current_line - 1)172 set -l b (math $current_line + 1)173 commandline -- $command_line[1..$a] $result174 commandline -a -- '' $command_line[$b..-1]175 end176 end177178 commandline -f repaint179 end180181 function fzf-cd-widget -d "Change directory"182 set -l commandline (__fzf_parse_commandline)183 set -lx dir $commandline[1]184 set -l fzf_query $commandline[2]185 set -l prefix $commandline[3]186187 set -lx FZF_DEFAULT_OPTS (__fzf_defaults \188 "--reverse --walker=dir,follow,hidden --scheme=path" \189 "$FZF_ALT_C_OPTS --no-multi --print0")190191 set -lx FZF_DEFAULT_OPTS_FILE192 set -lx FZF_DEFAULT_COMMAND "$FZF_ALT_C_COMMAND"193194 if set -l result (eval (__fzfcmd) --query=$fzf_query --walker-root=$dir | string split0)195 cd -- $result196 commandline -rt -- $prefix197 end198199 commandline -f repaint200 end201202 if not set -q FZF_CTRL_R_COMMAND; or test -n "$FZF_CTRL_R_COMMAND"203 if test -n "$FZF_CTRL_R_COMMAND"204 echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2205 end206 bind \cr fzf-history-widget207 bind -M insert \cr fzf-history-widget208 end209210 if not set -q FZF_CTRL_T_COMMAND; or test -n "$FZF_CTRL_T_COMMAND"211 bind \ct fzf-file-widget212 bind -M insert \ct fzf-file-widget213 end214215 if not set -q FZF_ALT_C_COMMAND; or test -n "$FZF_ALT_C_COMMAND"216 bind \ec fzf-cd-widget217 bind -M insert \ec fzf-cd-widget218 end219220end221222# Run setup223fzf_key_bindings