ambevar-dotfiles/.config/fish/fzf.fish

110 lines
3.6 KiB
Fish
Raw Normal View History

2016-10-13 11:09:04 +02:00
fzf_key_bindings
bind \cT transpose-chars
bind \e\ct fzf-file-widget
bind \ec capitalize-word
bind \eC fzf-cd-widget
2016-10-14 13:47:06 +02:00
function __fzf-select -d 'fzf commandline and print unescaped selection back to commandline'
2016-10-13 11:09:04 +02:00
set -l cmd (commandline)
[ $cmd ]; or return
2016-10-19 06:59:23 +02:00
eval $cmd | eval (__fzfcmd) -m --tac --tiebreak=index --toggle-sort=ctrl-r | string join ' ' | read -l result
2016-10-13 11:09:04 +02:00
[ "$result" ]; and commandline -- $result
commandline -f repaint
end
bind \e\cm __fzf-select
2016-10-13 11:09:04 +02:00
2016-10-14 13:47:06 +02:00
function __fzf-complete -d 'fzf completion and print selection back to commandline'
set -l complist (complete -C(commandline -c))
set -l result
switch (count $complist)
case 0
return
case 1
2016-10-14 13:47:06 +02:00
set result (printf '%s' "$complist[1]" | cut -f1)
case '*'
2016-10-19 06:59:23 +02:00
string join -- \n $complist | sort | eval (__fzfcmd) -m --tac --tiebreak=index --toggle-sort=ctrl-r | cut -f1 | while read -l r; set result $result $r; end
end
2016-10-14 13:47:06 +02:00
if [ -z "$result" ]
commandline -f repaint
return
end
for i in (seq (count $result))
set -l r $result[$i]
2016-10-14 13:47:06 +02:00
## We need to escape the result. We unescape 'r' first in case 'r' to
## prevent double escaping.
switch (string sub -s 1 -l 1 -- (commandline -t))
case "'"
commandline -t -- (string escape -- (eval "printf '%s' '$r'"))
case '"'
set -l quoted (string escape -- (eval "printf '%s' '$r'"))
set -l len (string length $quoted)
commandline -t -- '"'(string sub -s 2 -l (math $len - 2) (string escape -- (eval "printf '%s' '$r'")))'"'
case '~'
commandline -t -- (string sub -s 2 (string escape -n -- (eval "printf '%s' '$r'")))
2016-10-14 13:47:06 +02:00
case '*'
commandline -t -- (string escape -n -- (eval "printf '%s' '$r'"))
2016-10-14 13:47:06 +02:00
end
[ $i -lt (count $result) ]; and commandline -i ' '
end
commandline -f repaint
end
bind \t __fzf-complete
2016-10-13 11:09:04 +02:00
## DONE: Report missing (commandline) upstream.
## TODO: Report use of 'read'.
function fzf-history-widget
history | eval (__fzfcmd) +m --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS -q '(commandline)' | read -l result
and commandline -- $result
commandline -f repaint
end
## Like original but uses last token as root for 'find'.
## If last token is a path, you can use it as $cwd in FZF_CTRL_T_COMMAND to
## restrict search to this path. $cwd will be suppressed from commandline to
## ensure clean output from the search results.
## TODO: Report upstream. Makes '**' obsolete for bash and zsh.
function fzf-file-widget
set -l cwd_esc (commandline -t)
## The commandline token might be escaped, we need to unescape it.
set -l cwd (eval "printf '%s' $cwd_esc")
if [ ! -d "$cwd" ]
2016-10-13 11:09:04 +02:00
set cwd .
end
set -q FZF_CTRL_T_COMMAND; or set -l FZF_CTRL_T_COMMAND "
command find -L $cwd_esc \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
-o -type f -print \
-o -type d -print \
-o -type l -print 2> /dev/null | sed 1d"
2016-10-13 11:09:04 +02:00
2016-10-16 14:04:03 +02:00
eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m $FZF_CTRL_T_OPTS" | while read -l r; set result $result $r; end
if [ -n "$result" ]
if [ "$cwd" != . ]
## Remove last token from commandline.
commandline -t ""
2016-10-13 11:09:04 +02:00
end
2016-10-14 13:47:06 +02:00
for i in $result
commandline -it -- (string escape (eval "printf '%s' '$i'"))
commandline -it -- ' '
2016-10-14 13:47:06 +02:00
end
2016-10-13 11:09:04 +02:00
end
2016-10-14 13:47:06 +02:00
commandline -f repaint
2016-10-13 11:09:04 +02:00
end
function fzf-bcd-widget -d 'cd backwards'
pwd | nawk -v RS=/ '/\n/ {exit} {p=p $0 "/"; print p}' | eval (__fzfcmd) +m --tac | read -l result
[ "$result" ]; and cd $result
2016-10-13 11:09:04 +02:00
commandline -f repaint
end
bind \e\cL fzf-bcd-widget
function fzf-cdhist-widget -d 'cd to one of the previously visited location'
string join \n $dirprev | eval (__fzfcmd) +m | read -l result
[ "$result" ]; and cd $result
2016-10-13 11:09:04 +02:00
commandline -f repaint
end
bind \er fzf-cdhist-widget