| 1 |
# ____ ____ |
| 2 |
# / __/___ / __/ |
| 3 |
# / /_/_ / / /_ |
| 4 |
# / __/ / /_/ __/ |
| 5 |
# /_/ /___/_/ key-bindings.fish |
| 6 |
# |
| 7 |
# - $FZF_CTRL_T_COMMAND |
| 8 |
# - $FZF_CTRL_T_OPTS |
| 9 |
# - $FZF_CTRL_R_COMMAND |
| 10 |
# - $FZF_CTRL_R_OPTS |
| 11 |
# - $FZF_ALT_C_COMMAND |
| 12 |
# - $FZF_ALT_C_OPTS |
| 13 |
|
| 14 |
# Key bindings |
| 15 |
# ------------ |
| 16 |
function fzf_key_bindings |
| 17 |
|
| 18 |
# The oldest supported fish version is 3.4.0. For this message being able to be |
| 19 |
# displayed on older versions, the command substitution syntax $() should not |
| 20 |
# be used anywhere in the script, otherwise the source command will fail. |
| 21 |
if string match -qr -- '^[12]\\.|^3\\.[0-3]' $version |
| 22 |
echo "fzf key bindings script requires fish version 3.4.0 or newer." >&2 |
| 23 |
return 1 |
| 24 |
else if not command -q fzf |
| 25 |
echo "fzf was not found in path." >&2 |
| 26 |
return 1 |
| 27 |
end |
| 28 |
|
| 29 |
function __fzf_defaults |
| 30 |
# $argv[1]: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS |
| 31 |
# $argv[2..]: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS |
| 32 |
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 |
end |
| 38 |
|
| 39 |
function __fzfcmd |
| 40 |
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 |
else |
| 46 |
echo "fzf" |
| 47 |
end |
| 48 |
end |
| 49 |
|
| 50 |
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 '.' |
| 54 |
|
| 55 |
set -l -- match_regex '(?<fzf_query>[\\s\\S]*?(?=\\n?$)$)' |
| 56 |
set -l -- prefix_regex '^-[^\\s=]+=|^-(?!-)\\S' |
| 57 |
|
| 58 |
# 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" |
| 61 |
|
| 62 |
# Set $prefix and expanded $fzf_query with preserved trailing newlines. |
| 63 |
if string match -qr -- '^\\d\\d+|^[4-9]' $version |
| 64 |
# fish v4.0.0 and newer |
| 65 |
string match -q -r -- $match_regex (commandline --current-token --tokens-expanded | string collect -N) |
| 66 |
else |
| 67 |
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 |
end |
| 70 |
|
| 71 |
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]' $version |
| 74 |
# fish v3.5.0 and newer |
| 75 |
set -- fzf_query (path normalize -- $fzf_query) |
| 76 |
set -- dir $fzf_query |
| 77 |
while not path is -d $dir |
| 78 |
set -- dir (path dirname $dir) |
| 79 |
end |
| 80 |
else |
| 81 |
string match -q -r -- '(?<fzf_query>^[\\s\\S]*?(?=\\n?$)$)' \ |
| 82 |
(string replace -r -a -- '(?<=/)/|(?<!^)/+(?!\\n)$' '' $fzf_query | string collect -N) |
| 83 |
set -- dir $fzf_query |
| 84 |
while not test -d "$dir" |
| 85 |
set -- dir (dirname -z -- "$dir" | string split0) |
| 86 |
end |
| 87 |
end |
| 88 |
|
| 89 |
if not string match -q -- '.' $dir; or string match -qr -- '^\\.(/|$)' $fzf_query |
| 90 |
# Strip $dir from $fzf_query - preserve trailing newlines. |
| 91 |
if string match -qr -- '^\\d\\d+|^[4-9]' $version |
| 92 |
# fish v4.0.0 and newer |
| 93 |
string match -q -r -- '^'(string escape --style=regex -- $dir)'/?(?<fzf_query>[\\s\\S]*)' $fzf_query |
| 94 |
else |
| 95 |
string match -q -r -- '^/?(?<fzf_query>[\\s\\S]*?(?=\\n?$)$)' \ |
| 96 |
(string replace -- "$dir" '' $fzf_query | string collect -N) |
| 97 |
end |
| 98 |
end |
| 99 |
end |
| 100 |
|
| 101 |
string escape -n -- "$dir" "$fzf_query" "$prefix" |
| 102 |
end |
| 103 |
|
| 104 |
# Store current token in $dir as root for the 'find' command |
| 105 |
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] |
| 110 |
|
| 111 |
set -lx FZF_DEFAULT_OPTS (__fzf_defaults \ |
| 112 |
"--reverse --walker=file,dir,follow,hidden --scheme=path" \ |
| 113 |
"--multi $FZF_CTRL_T_OPTS --print0") |
| 114 |
|
| 115 |
set -lx FZF_DEFAULT_OPTS_FILE |
| 116 |
|
| 117 |
set -lx FZF_DEFAULT_COMMAND |
| 118 |
|
| 119 |
if test -n "$FZF_CTRL_T_COMMAND" |
| 120 |
set -f result (eval $FZF_CTRL_T_COMMAND \| (__fzfcmd) --query=$fzf_query | string split0) |
| 121 |
else |
| 122 |
set -f result (eval (__fzfcmd) --walker-root=$dir --query=$fzf_query | string split0) |
| 123 |
end |
| 124 |
|
| 125 |
test -n "$result" |
| 126 |
and commandline -rt -- (string join -- ' ' $prefix(string escape -n -- $result))' ' |
| 127 |
|
| 128 |
commandline -f repaint |
| 129 |
end |
| 130 |
|
| 131 |
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]) |
| 136 |
|
| 137 |
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)" |
| 138 |
|
| 139 |
# Enable syntax highlighting colors on fish v4.3.3 and newer |
| 140 |
if string match -qr -- '^\\d\\d+|^4\\.[4-9]|^4\\.3\\.[3-9]' $version |
| 141 |
set -a -- FZF_DEFAULT_COMMAND "--color=always" |
| 142 |
end |
| 143 |
|
| 144 |
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) |
| 152 |
|
| 153 |
# Add dynamic preview options if preview command isn't already set by user |
| 154 |
if string match -qvr -- '--preview[= ]' "$FZF_DEFAULT_OPTS" |
| 155 |
# Prepend the options to allow user overrides |
| 156 |
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 |
end |
| 161 |
|
| 162 |
set -lx FZF_DEFAULT_OPTS_FILE |
| 163 |
|
| 164 |
# Merge history from other sessions before searching |
| 165 |
test -z "$fish_private_mode"; and builtin history merge |
| 166 |
|
| 167 |
if set -l result (eval $FZF_DEFAULT_COMMAND \| (__fzfcmd) --query=$fzf_query | string split0) |
| 168 |
if test "$total_lines" -eq 1 |
| 169 |
commandline -- $result |
| 170 |
else |
| 171 |
set -l a (math $current_line - 1) |
| 172 |
set -l b (math $current_line + 1) |
| 173 |
commandline -- $command_line[1..$a] $result |
| 174 |
commandline -a -- '' $command_line[$b..-1] |
| 175 |
end |
| 176 |
end |
| 177 |
|
| 178 |
commandline -f repaint |
| 179 |
end |
| 180 |
|
| 181 |
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] |
| 186 |
|
| 187 |
set -lx FZF_DEFAULT_OPTS (__fzf_defaults \ |
| 188 |
"--reverse --walker=dir,follow,hidden --scheme=path" \ |
| 189 |
"$FZF_ALT_C_OPTS --no-multi --print0") |
| 190 |
|
| 191 |
set -lx FZF_DEFAULT_OPTS_FILE |
| 192 |
set -lx FZF_DEFAULT_COMMAND "$FZF_ALT_C_COMMAND" |
| 193 |
|
| 194 |
if set -l result (eval (__fzfcmd) --query=$fzf_query --walker-root=$dir | string split0) |
| 195 |
cd -- $result |
| 196 |
commandline -rt -- $prefix |
| 197 |
end |
| 198 |
|
| 199 |
commandline -f repaint |
| 200 |
end |
| 201 |
|
| 202 |
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" >&2 |
| 205 |
end |
| 206 |
bind \cr fzf-history-widget |
| 207 |
bind -M insert \cr fzf-history-widget |
| 208 |
end |
| 209 |
|
| 210 |
if not set -q FZF_CTRL_T_COMMAND; or test -n "$FZF_CTRL_T_COMMAND" |
| 211 |
bind \ct fzf-file-widget |
| 212 |
bind -M insert \ct fzf-file-widget |
| 213 |
end |
| 214 |
|
| 215 |
if not set -q FZF_ALT_C_COMMAND; or test -n "$FZF_ALT_C_COMMAND" |
| 216 |
bind \ec fzf-cd-widget |
| 217 |
bind -M insert \ec fzf-cd-widget |
| 218 |
end |
| 219 |
|
| 220 |
end |
| 221 |
|
| 222 |
# Run setup |
| 223 |
fzf_key_bindings |