Skip to content
Myr

Overhaul fish functions (#759)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Replace the "temp file" workaround with the "read" function: it's simpler and faster. Use proper escaping, remove the custom function. The "file" widget uses last token as root for the "find" command. This replaces the equivalent of '**' completion in bash/zsh. The "$dir" non-expanded variable can be used in FZF_CTRL_T_COMMAND to set the root.

authored by Pierre Neidhardt · committed by Junegunn Choi · parent 8a502af · Browse files · 2 files+66 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+66 added−34 removed

  • other:+66 added−34 removed

2 files+66 added−34 removed

modified:shell/key-bindings.fish+38 added-31 removed
1# Key bindings2# ------------3function fzf_key_bindings4 # Due to a bug of fish, we cannot use command substitution,5 # so we use temporary file instead6 if [ -z "$TMPDIR" ]7 set -g TMPDIR /tmp8 end410 function __fzf_escape11 while read item12 echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '5 # Store last token in $dir as root for the 'find' command6 function fzf-file-widget -d "List files and folders"7 set -l dir (commandline -t)8 # The commandline token might be escaped, we need to unescape it.9 set dir (eval "printf '%s' $dir")10 if [ ! -d "$dir" ]11 set dir .12 end14 end13 # Some 'find' versions print undesired duplicated slashes if the path ends with slashes.14 set dir (string replace --regex '(.)/+$' '$1' "$dir")1516 function fzf-file-widget16 # "-path \$dir'*/\\.*'" matches hidden files/folders inside $dir but not17 # $dir itself, even if hidden.18 set -q FZF_CTRL_T_COMMAND; or set -l FZF_CTRL_T_COMMAND "18 command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \19 -o -type f -print \20 -o -type d -print \21 -o -type l -print 2> /dev/null | sed 1d | cut -b3-"22 eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m $FZF_CTRL_T_OPTS > $TMPDIR/fzf.result"23 and for i in (seq 20); commandline -i (cat $TMPDIR/fzf.result | __fzf_escape) 2> /dev/null; and break; sleep 0.1; end19 command find -L \$dir \\( -path \$dir'*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \20 -o -type f -print \21 -o -type d -print \22 -o -type l -print 2> /dev/null | sed '1d; s#^\./##'"2324 eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m $FZF_CTRL_T_OPTS" | while read -l r; set result $result $r; end25 if [ -z "$result" ]26 commandline -f repaint27 return28 end2930 if [ "$dir" != . ]31 # Remove last token from commandline.32 commandline -t ""33 end34 for i in $result35 commandline -it -- (string escape $i)36 commandline -it -- ' '37 end38 commandline -f repaint25 rm -f $TMPDIR/fzf.result39 end4028 function fzf-history-widget29 history | eval (__fzfcmd) +s +m --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS -q '(commandline)' > $TMPDIR/fzf.result30 and commandline -- (cat $TMPDIR/fzf.result)41 function fzf-history-widget -d "Show command history"42 history | eval (__fzfcmd) +s +m --tiebreak=index $FZF_CTRL_R_OPTS -q '(commandline)' | read -l result43 and commandline -- $result44 commandline -f repaint32 rm -f $TMPDIR/fzf.result45 end4635 function fzf-cd-widget47 function fzf-cd-widget -d "Change directory"48 set -q FZF_ALT_C_COMMAND; or set -l FZF_ALT_C_COMMAND "37 command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \38 -o -type d -print 2> /dev/null | sed 1d | cut -b3-"39 # Fish hangs if the command before pipe redirects (2> /dev/null)40 eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)" +m $FZF_ALT_C_OPTS > $TMPDIR/fzf.result"41 [ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]42 and cd (cat $TMPDIR/fzf.result)49 command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \50 -o -type d -print 2> /dev/null | sed 1d | cut -b3-"51 eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)" +m $FZF_ALT_C_OPTS" | read -l result52 [ "$result" ]; and cd $result53 commandline -f repaint44 rm -f $TMPDIR/fzf.result54 end5556 function __fzfcmd57 set -q FZF_TMUX; or set FZF_TMUX 14958 if [ $FZF_TMUX -eq 1 ]59 if set -q FZF_TMUX_HEIGHT60 echo "fzf-tmux -d$FZF_TMUX_HEIGHT"61 else
13 unmodified lines
75 bind -M insert \cr fzf-history-widget76 bind -M insert \ec fzf-cd-widget77 end78end71
modified:README.md+28 added-3 removed
432 unmodified lines
433#### Fish shell434435It's [a known bug of fish](https://github.com/fish-shell/fish-shell/issues/1362)436that it doesn't allow reading from STDIN in command substitution, which means437simple `vim (fzf)` won't work as expected. The workaround is to store the result438of fzf to a temporary file.437simple `vim (fzf)` won't work as expected. The workaround is to use the `read`438fish command:439440```sh441fzf > $TMPDIR/fzf.result; and vim (cat $TMPDIR/fzf.result)441fzf | read -l result; and vim $result442```443444or, for multiple results:445446```sh447fzf -m | while read -l r; set result $result $r; end; and vim $result448```449450The globbing system is different in fish and thus `**` completion will not work.451However, the `CTRL-T` command will use the last token on the commandline as the452root folder for the recursive search. For instance, hitting `CTRL-T` at the end453of the following commandline454455```sh456ls /var/457```458459will list all files and folders under `/var/`.460461When using a custom `FZF_CTRL_T_COMMAND`, use the unexpanded `$dir` variable to462make use of this feature. `$dir` defaults to `.` when the last token is not a463valid directory. Example:464465```sh466set -l FZF_CTRL_T_COMMAND "command find -L \$dir -type f 2> /dev/null | sed '1d; s#^\./##'"467```468469License470-------471472[MIT](LICENSE)
5 unmodified lines