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
Provenance
- Commit
0508e70- Tree
0a1b382- Author
Pierre Neidhardt <ambrevar@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
8a502af
2 files changed+66 added−34 removed
modified:shell/key-bindings.fish+38 added-31 removed
1 # Key bindings2 # ------------3 function 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- end4 10- 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")15 16- 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#^\./##'"23+24+ 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+ end29+30+ 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 end40 28- 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 end46 35- 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 end55 56 function __fzfcmd57 set -q FZF_TMUX; or set FZF_TMUX 149-58 if [ $FZF_TMUX -eq 1 ]59 if set -q FZF_TMUX_HEIGHT60 echo "fzf-tmux -d$FZF_TMUX_HEIGHT"61 else13 unmodified lines75 bind -M insert \cr fzf-history-widget76 bind -M insert \ec fzf-cd-widget77 end78 end71-modified:README.md+28 added-3 removed
432 unmodified lines433 #### Fish shell434 435 It's [a known bug of fish](https://github.com/fish-shell/fish-shell/issues/1362)436 that it doesn't allow reading from STDIN in command substitution, which means437-simple `vim (fzf)` won't work as expected. The workaround is to store the result438-of fzf to a temporary file.437+simple `vim (fzf)` won't work as expected. The workaround is to use the `read`438+fish command:439 440 ```sh441-fzf > $TMPDIR/fzf.result; and vim (cat $TMPDIR/fzf.result)441+fzf | read -l result; and vim $result442 ```443 444+or, for multiple results:445+446+```sh447+fzf -m | while read -l r; set result $result $r; end; and vim $result448+```449+450+The globbing system is different in fish and thus `**` completion will not work.451+However, the `CTRL-T` command will use the last token on the commandline as the452+root folder for the recursive search. For instance, hitting `CTRL-T` at the end453+of the following commandline454+455+```sh456+ls /var/457+```458+459+will list all files and folders under `/var/`.460+461+When using a custom `FZF_CTRL_T_COMMAND`, use the unexpanded `$dir` variable to462+make use of this feature. `$dir` defaults to `.` when the last token is not a463+valid directory. Example:464+465+```sh466+set -l FZF_CTRL_T_COMMAND "command find -L \$dir -type f 2> /dev/null | sed '1d; s#^\./##'"467+```468+469 License470 -------471 472 [MIT](LICENSE)5 unmodified lines