2020-03-31 22:18:09 +09: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 18:06:55 +02:00
2015-03-13 13:08:42 +09:00
# Key bindings
# ------------
function fzf_key_bindings
2024-04-19 22:40:38 +09:00
function __fzf_defaults
2025-02-13 11:22:35 +02:00
# $argv[1]: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
# $argv[2..]: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
test -n " $FZF_TMUX_HEIGHT " ; or set -l FZF_TMUX_HEIGHT 40 %
string join ' ' -- \
"--height $FZF_TMUX_HEIGHT --min-height=20+ --bind=ctrl-z:ignore" $argv [ 1 ] \
( test -r " $FZF_DEFAULT_OPTS_FILE " ; and string join -- ' ' < $FZF_DEFAULT_OPTS_FILE ) \
$FZF_DEFAULT_OPTS $argv [ 2 ..- 1 ]
2024-04-19 22:40:38 +09:00
end
2017-02-16 09:18:01 +01:00
# Store current token in $dir as root for the 'find' command
2016-12-14 12:07:27 +05:30
function fzf -file-widget -d "List files and folders"
2017-06-25 05:16:15 -07:00
set -l commandline ( __fzf_parse_commandline )
2024-03-31 20:37:20 +09:00
set -lx dir $commandline [ 1 ]
2017-06-25 05:16:15 -07:00
set -l fzf_query $commandline [ 2 ]
2021-03-08 14:59:56 +01:00
set -l prefix $commandline [ 3 ]
2015-03-13 13:08:42 +09:00
2025-02-13 18:56:46 +02:00
set -lx FZF_DEFAULT_OPTS ( __fzf_defaults \
"--reverse --walker=file,dir,follow,hidden --scheme=path --walker-root= $dir " \
" $FZF_CTRL_T_OPTS --multi" )
set -lx FZF_DEFAULT_COMMAND " $FZF_CTRL_T_COMMAND "
set -lx FZF_DEFAULT_OPTS_FILE
if set -l result ( eval ( __fzfcmd ) --query = $fzf_query )
2016-12-14 12:07:27 +05:30
# Remove last token from commandline.
2025-02-13 18:56:46 +02:00
commandline -t ''
for i in $result
commandline -it -- $prefix ( string escape -- $i ) ' '
end
2016-12-14 12:07:27 +05:30
end
2025-02-13 18:56:46 +02:00
2015-04-22 00:30:09 +09:00
commandline -f repaint
2015-03-13 13:08:42 +09:00
end
2016-12-14 12:07:27 +05:30
function fzf -history -widget -d "Show command history"
2025-02-15 10:56:01 +02:00
set -l fzf_query ( commandline | string escape)
2024-06-12 06:56:20 +02:00
2025-02-15 10:56:01 +02:00
set -lx FZF_DEFAULT_OPTS ( __fzf_defaults '' \
'--nth=2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign="\t↳ "' \
"--highlight-line --no-multi $FZF_CTRL_R_OPTS --read0 --print0" \
"--bind='enter:become:string replace -a -- \n\t \n {2..} | string collect'" \
'--with-shell=' ( status fish -path) \\ -c )
2025-01-28 14:34:21 +02:00
2025-02-15 10:56:01 +02:00
set -lx FZF_DEFAULT_OPTS_FILE
set -lx FZF_DEFAULT_COMMAND
if type -q perl
set -a FZF_DEFAULT_OPTS '--tac'
set FZF_DEFAULT_COMMAND 'builtin history -z --reverse | command perl -0 -pe \' s/^ /$. \t /g; s /\n /\n\t /gm\' '
else
set FZF_DEFAULT_COMMAND \
' set -l h ( builtin history -z --reverse | string split0) ; ' \
' for i in ( seq ( count $h ) -1 1 ) ; ' \
' string join0 -- $i \t ( string replace -a -- \n \n\t $h [ $i ] | string collect) ; ' \
' end'
2017-01-15 16:15:51 +09:00
end
2025-02-15 10:56:01 +02:00
# Merge history from other sessions before searching
test -z "$fish_private_mode"; and builtin history merge
set -l result (eval $FZF_DEFAULT_COMMAND \| (__fzfcmd) --query=$fzf_query)
and commandline -- $result
2015-03-13 13:08:42 +09:00
commandline -f repaint
end
2016-12-14 12:07:27 +05:30
function fzf-cd-widget -d "Change directory"
2017-06-25 05:16:15 -07:00
set -l commandline (__fzf_parse_commandline)
2024-03-31 20:37:20 +09:00
set -lx dir $commandline[1]
2017-06-25 05:16:15 -07:00
set -l fzf_query $commandline[2]
2021-03-08 14:59:56 +01:00
set -l prefix $commandline[3]
2017-06-25 05:16:15 -07:00
2025-02-15 12:12:59 +02:00
set -lx FZF_DEFAULT_OPTS (__fzf_defaults \
"--reverse --walker=dir,follow,hidden --scheme=path --walker-root=$dir" \
"$FZF_ALT_C_OPTS --no-multi")
2017-06-25 05:16:15 -07:00
2025-02-15 12:12:59 +02:00
set -lx FZF_DEFAULT_OPTS_FILE
set -lx FZF_DEFAULT_COMMAND "$FZF_ALT_C_COMMAND"
2017-06-25 05:16:15 -07:00
2025-02-15 12:12:59 +02:00
if set -l result (eval (__fzfcmd) --query=$fzf_query)
cd -- $result
commandline -rt -- $prefix
2017-01-15 16:15:51 +09:00
end
2017-06-25 05:16:15 -07:00
2015-03-13 13:08:42 +09:00
commandline -f repaint
end
2015-04-22 00:55:39 +09:00
function __fzfcmd
2025-02-13 11:33:31 +02:00
test -n "$FZF_TMUX_HEIGHT"; or set -l FZF_TMUX_HEIGHT 40%
2024-11-13 13:57:46 +02:00
if test -n "$FZF_TMUX_OPTS"
2020-03-31 22:18:09 +09:00
echo "fzf-tmux $FZF_TMUX_OPTS -- "
2024-11-13 13:57:46 +02:00
else if test "$FZF_TMUX" = "1"
2020-03-31 22:18:09 +09:00
echo "fzf-tmux -d$FZF_TMUX_HEIGHT -- "
2015-03-13 13:08:42 +09:00
else
2017-01-15 16:15:51 +09:00
echo "fzf"
2015-03-13 13:08:42 +09:00
end
end
2016-04-24 13:55:48 +09:00
bind \cr fzf-history-widget
2024-03-17 03:06:48 -04: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 23:10:47 +02:00
2025-01-09 18:16:24 +08:00
bind -M insert \cr fzf-history-widget
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
2015-03-30 23:10:47 +02:00
end
2017-06-25 05:16:15 -07:00
2021-03-08 14:59:56 +01:00
function __fzf_parse_commandline -d ' Parse the current command line token and return split of existing filepath, fzf query, and optional -option = prefix'
2025-02-13 12:46:49 +02:00
set -l dir ' .'
set -l query
set -l commandline (commandline -t | string unescape -n)
2021-03-08 14:59:56 +01:00
2025-02-13 12:46:49 +02:00
# Strip -option= from token if present
2021-03-08 14:59:56 +01:00
set -l prefix (string match -r -- ' ^ -[^ \s =] += ' $commandline)
set commandline (string replace -- "$prefix" '' $commandline)
2024-12-19 11:14:54 +02:00
# Enable home directory expansion of leading ~/
set commandline (string replace -r -- ' ^ ~/' ' \$ HOME/' $commandline)
2025-02-13 12:46:49 +02:00
# Escape special characters, except for the $ sign of valid variable names,
# so that the original string with expanded variables is returned after eval.
2024-11-08 09:44:34 +02:00
set commandline (string escape -n -- $commandline)
2025-02-13 12:46:49 +02:00
set commandline (string replace -r -a -- ' \\\\\$ ( ?=[ \w ]) ' ' \$ ' $commandline)
2024-11-08 09:44:34 +02:00
2017-06-25 05:16:15 -07:00
# eval is used to do shell expansion on paths
2021-03-08 14:59:56 +01:00
eval set commandline $commandline
2017-06-25 05:16:15 -07:00
2025-02-13 12:46:49 +02:00
# Combine multiple consecutive slashes into one.
set commandline (string replace -r -a -- ' /+' ' /' $commandline)
2024-11-08 09:44:34 +02:00
2025-02-13 12:46:49 +02:00
if test -n "$commandline"
# Strip trailing slash, unless $dir is root dir (/)
set dir (string replace -r -- ' ( ?< !^) /$' '' $commandline )
2017-06-25 05:16:15 -07:00
2025-02-13 12:46:49 +02:00
# Set $dir to the longest existing filepath
while not test -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 "."
set dir ( dirname -- $dir )
end
if test " $dir " = '.' ; and test ( string sub -l 2 -- $commandline ) != './'
# If $dir is "." but commandline is not a relative path, this means no file path found
2017-06-25 05:16:15 -07:00
set fzf_query $commandline
else
# Also remove trailing slash after dir, to "split" input properly
2025-02-11 16:19:40 +02:00
set fzf_query ( string replace -r -- "^ $dir /?" '' $commandline )
2017-06-25 05:16:15 -07:00
end
end
2025-02-13 12:46:49 +02:00
string escape -n -- " $dir " " $fzf_query " " $prefix "
2017-06-25 05:16:15 -07:00
end
2015-03-13 13:08:42 +09:00
end