2020-03-31 13:18:09 +00:00
|
|
|
# ____ ____
|
|
|
|
# / __/___ / __/
|
|
|
|
# / /_/_ / / /_
|
|
|
|
# / __/ / /_/ __/
|
|
|
|
# /_/ /___/_/ key-bindings.fish
|
|
|
|
#
|
|
|
|
# - $FZF_TMUX_OPTS
|
|
|
|
# - $FZF_CTRL_T_COMMAND
|
|
|
|
# - $FZF_CTRL_T_OPTS
|
|
|
|
# - $FZF_CTRL_R_OPTS
|
|
|
|
# - $FZF_ALT_C_COMMAND
|
|
|
|
# - $FZF_ALT_C_OPTS
|
|
|
|
|
2023-10-12 16:06:55 +00:00
|
|
|
status is-interactive; or exit 0
|
|
|
|
|
|
|
|
|
2015-03-13 04:08:42 +00:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
|
|
|
function fzf_key_bindings
|
|
|
|
|
2024-04-19 13:40:38 +00:00
|
|
|
function __fzf_defaults
|
|
|
|
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
|
|
|
|
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
|
|
|
|
test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%
|
|
|
|
echo "--height $FZF_TMUX_HEIGHT --bind=ctrl-z:ignore" $argv[1]
|
|
|
|
command cat "$FZF_DEFAULT_OPTS_FILE" 2> /dev/null
|
|
|
|
echo $FZF_DEFAULT_OPTS $argv[2]
|
|
|
|
end
|
|
|
|
|
2017-02-16 08:18:01 +00:00
|
|
|
# Store current token in $dir as root for the 'find' command
|
2016-12-14 06:37:27 +00:00
|
|
|
function fzf-file-widget -d "List files and folders"
|
2017-06-25 12:16:15 +00:00
|
|
|
set -l commandline (__fzf_parse_commandline)
|
2024-03-31 11:37:20 +00:00
|
|
|
set -lx dir $commandline[1]
|
2017-06-25 12:16:15 +00:00
|
|
|
set -l fzf_query $commandline[2]
|
2021-03-08 13:59:56 +00:00
|
|
|
set -l prefix $commandline[3]
|
2015-03-13 04:08:42 +00:00
|
|
|
|
2020-02-28 08:50:36 +00:00
|
|
|
test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%
|
2017-01-15 07:15:51 +00:00
|
|
|
begin
|
2024-04-19 13:40:38 +00:00
|
|
|
set -lx FZF_DEFAULT_OPTS (__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path --walker-root='$dir'" "$FZF_CTRL_T_OPTS")
|
2024-03-13 11:56:31 +00:00
|
|
|
set -lx FZF_DEFAULT_COMMAND "$FZF_CTRL_T_COMMAND"
|
2024-04-19 13:40:38 +00:00
|
|
|
set -lx FZF_DEFAULT_OPTS_FILE ''
|
2024-03-13 11:56:31 +00:00
|
|
|
eval (__fzfcmd)' -m --query "'$fzf_query'"' | while read -l r; set result $result $r; end
|
2017-01-15 07:15:51 +00:00
|
|
|
end
|
2016-12-14 06:37:27 +00:00
|
|
|
if [ -z "$result" ]
|
|
|
|
commandline -f repaint
|
|
|
|
return
|
2017-06-12 09:24:45 +00:00
|
|
|
else
|
2016-12-14 06:37:27 +00:00
|
|
|
# Remove last token from commandline.
|
|
|
|
commandline -t ""
|
|
|
|
end
|
|
|
|
for i in $result
|
2021-03-08 13:59:56 +00:00
|
|
|
commandline -it -- $prefix
|
2016-12-14 06:37:27 +00:00
|
|
|
commandline -it -- (string escape $i)
|
|
|
|
commandline -it -- ' '
|
|
|
|
end
|
2015-04-21 15:30:09 +00:00
|
|
|
commandline -f repaint
|
2015-03-13 04:08:42 +00:00
|
|
|
end
|
|
|
|
|
2016-12-14 06:37:27 +00:00
|
|
|
function fzf-history-widget -d "Show command history"
|
2020-02-28 08:50:36 +00:00
|
|
|
test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%
|
2017-01-15 07:15:51 +00:00
|
|
|
begin
|
2024-04-19 13:40:38 +00:00
|
|
|
set -lx FZF_DEFAULT_OPTS (__fzf_defaults "" "--scheme=history --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m")
|
|
|
|
set -lx FZF_DEFAULT_OPTS_FILE ''
|
2017-07-15 09:50:23 +00:00
|
|
|
|
2018-06-27 10:02:16 +00:00
|
|
|
set -l FISH_MAJOR (echo $version | cut -f1 -d.)
|
|
|
|
set -l FISH_MINOR (echo $version | cut -f2 -d.)
|
2017-07-15 09:50:23 +00:00
|
|
|
|
|
|
|
# history's -z flag is needed for multi-line support.
|
|
|
|
# history's -z flag was added in fish 2.4.0, so don't use it for versions
|
|
|
|
# before 2.4.0.
|
|
|
|
if [ "$FISH_MAJOR" -gt 2 -o \( "$FISH_MAJOR" -eq 2 -a "$FISH_MINOR" -ge 4 \) ];
|
2019-07-13 05:47:51 +00:00
|
|
|
history -z | eval (__fzfcmd) --read0 --print0 -q '(commandline)' | read -lz result
|
2017-07-15 09:50:23 +00:00
|
|
|
and commandline -- $result
|
|
|
|
else
|
|
|
|
history | eval (__fzfcmd) -q '(commandline)' | read -l result
|
|
|
|
and commandline -- $result
|
|
|
|
end
|
2017-01-15 07:15:51 +00:00
|
|
|
end
|
2015-03-13 04:08:42 +00:00
|
|
|
commandline -f repaint
|
|
|
|
end
|
|
|
|
|
2016-12-14 06:37:27 +00:00
|
|
|
function fzf-cd-widget -d "Change directory"
|
2017-06-25 12:16:15 +00:00
|
|
|
set -l commandline (__fzf_parse_commandline)
|
2024-03-31 11:37:20 +00:00
|
|
|
set -lx dir $commandline[1]
|
2017-06-25 12:16:15 +00:00
|
|
|
set -l fzf_query $commandline[2]
|
2021-03-08 13:59:56 +00:00
|
|
|
set -l prefix $commandline[3]
|
2017-06-25 12:16:15 +00:00
|
|
|
|
2020-02-28 08:50:36 +00:00
|
|
|
test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%
|
2017-01-15 07:15:51 +00:00
|
|
|
begin
|
2024-04-19 13:40:38 +00:00
|
|
|
set -lx FZF_DEFAULT_OPTS (__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path --walker-root='$dir'" "$FZF_ALT_C_OPTS")
|
|
|
|
set -lx FZF_DEFAULT_OPTS_FILE ''
|
2024-03-13 11:56:31 +00:00
|
|
|
set -lx FZF_DEFAULT_COMMAND "$FZF_ALT_C_COMMAND"
|
|
|
|
eval (__fzfcmd)' +m --query "'$fzf_query'"' | read -l result
|
2017-06-25 12:16:15 +00:00
|
|
|
|
|
|
|
if [ -n "$result" ]
|
2022-09-20 07:58:43 +00:00
|
|
|
cd -- $result
|
2017-06-25 12:16:15 +00:00
|
|
|
|
|
|
|
# Remove last token from commandline.
|
|
|
|
commandline -t ""
|
2021-03-08 13:59:56 +00:00
|
|
|
commandline -it -- $prefix
|
2017-06-25 12:16:15 +00:00
|
|
|
end
|
2017-01-15 07:15:51 +00:00
|
|
|
end
|
2017-06-25 12:16:15 +00:00
|
|
|
|
2015-03-13 04:08:42 +00:00
|
|
|
commandline -f repaint
|
|
|
|
end
|
|
|
|
|
2015-04-21 15:55:39 +00:00
|
|
|
function __fzfcmd
|
2020-02-28 08:50:36 +00:00
|
|
|
test -n "$FZF_TMUX"; or set FZF_TMUX 0
|
|
|
|
test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%
|
2020-03-31 13:18:09 +00:00
|
|
|
if [ -n "$FZF_TMUX_OPTS" ]
|
|
|
|
echo "fzf-tmux $FZF_TMUX_OPTS -- "
|
|
|
|
else if [ $FZF_TMUX -eq 1 ]
|
|
|
|
echo "fzf-tmux -d$FZF_TMUX_HEIGHT -- "
|
2015-03-13 04:08:42 +00:00
|
|
|
else
|
2017-01-15 07:15:51 +00:00
|
|
|
echo "fzf"
|
2015-03-13 04:08:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-24 04:55:48 +00:00
|
|
|
bind \cr fzf-history-widget
|
2024-03-17 07:06:48 +00:00
|
|
|
if not set -q FZF_CTRL_T_COMMAND; or test -n "$FZF_CTRL_T_COMMAND"
|
|
|
|
bind \ct fzf-file-widget
|
|
|
|
end
|
|
|
|
if not set -q FZF_ALT_C_COMMAND; or test -n "$FZF_ALT_C_COMMAND"
|
|
|
|
bind \ec fzf-cd-widget
|
|
|
|
end
|
2015-03-30 21:10:47 +00:00
|
|
|
|
|
|
|
if bind -M insert > /dev/null 2>&1
|
2016-04-24 04:55:48 +00:00
|
|
|
bind -M insert \cr fzf-history-widget
|
2024-03-17 07:06:48 +00:00
|
|
|
if not set -q FZF_CTRL_T_COMMAND; or test -n "$FZF_CTRL_T_COMMAND"
|
|
|
|
bind -M insert \ct fzf-file-widget
|
|
|
|
end
|
|
|
|
if not set -q FZF_ALT_C_COMMAND; or test -n "$FZF_ALT_C_COMMAND"
|
|
|
|
bind -M insert \ec fzf-cd-widget
|
|
|
|
end
|
2015-03-30 21:10:47 +00:00
|
|
|
end
|
2017-06-25 12:16:15 +00:00
|
|
|
|
2021-03-08 13:59:56 +00:00
|
|
|
function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath, fzf query, and optional -option= prefix'
|
|
|
|
set -l commandline (commandline -t)
|
|
|
|
|
|
|
|
# strip -option= from token if present
|
|
|
|
set -l prefix (string match -r -- '^-[^\s=]+=' $commandline)
|
|
|
|
set commandline (string replace -- "$prefix" '' $commandline)
|
|
|
|
|
2017-06-25 12:16:15 +00:00
|
|
|
# eval is used to do shell expansion on paths
|
2021-03-08 13:59:56 +00:00
|
|
|
eval set commandline $commandline
|
2017-06-25 12:16:15 +00:00
|
|
|
|
|
|
|
if [ -z $commandline ]
|
|
|
|
# Default to current directory with no --query
|
|
|
|
set dir '.'
|
|
|
|
set fzf_query ''
|
|
|
|
else
|
|
|
|
set dir (__fzf_get_dir $commandline)
|
|
|
|
|
2020-10-12 03:58:37 +00:00
|
|
|
if [ "$dir" = "." -a (string sub -l 1 -- $commandline) != '.' ]
|
2017-06-25 12:16:15 +00:00
|
|
|
# if $dir is "." but commandline is not a relative path, this means no file path found
|
|
|
|
set fzf_query $commandline
|
|
|
|
else
|
|
|
|
# Also remove trailing slash after dir, to "split" input properly
|
2020-10-12 03:58:37 +00:00
|
|
|
set fzf_query (string replace -r "^$dir/?" -- '' "$commandline")
|
2017-06-25 12:16:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
echo $dir
|
|
|
|
echo $fzf_query
|
2021-03-08 13:59:56 +00:00
|
|
|
echo $prefix
|
2017-06-25 12:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_get_dir -d 'Find the longest existing filepath from input string'
|
|
|
|
set dir $argv
|
|
|
|
|
|
|
|
# Strip all trailing slashes. Ignore if $dir is root dir (/)
|
2020-10-12 03:58:37 +00:00
|
|
|
if [ (string length -- $dir) -gt 1 ]
|
|
|
|
set dir (string replace -r '/*$' -- '' $dir)
|
2017-06-25 12:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Iteratively check if dir exists and strip tail end of path
|
|
|
|
while [ ! -d "$dir" ]
|
|
|
|
# If path is absolute, this can keep going until ends up at /
|
|
|
|
# If path is relative, this can keep going until entire input is consumed, dirname returns "."
|
2020-10-12 03:58:37 +00:00
|
|
|
set dir (dirname -- "$dir")
|
2017-06-25 12:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
echo $dir
|
|
|
|
end
|
|
|
|
|
2015-03-13 04:08:42 +00:00
|
|
|
end
|