Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fish] Leading characters: don't strip ., expand ~/ #4136

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions shell/key-bindings.fish
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function fzf_key_bindings
function fzf-history-widget -d "Show command history"
test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%
begin
set -l FISH_MAJOR (string split '.' -- $version)[1]
set -l FISH_MINOR (string split '.' -- $version)[2]
set -l FISH_MAJOR (string split -f 1 -- '.' $version)
set -l FISH_MINOR (string split -f 2 -- '.' $version)

# merge history from other sessions before searching
test -z "$fish_private_mode"; and builtin history merge
Expand Down Expand Up @@ -150,17 +150,20 @@ function fzf_key_bindings
set -l prefix (string match -r -- '^-[^\s=]+=' $commandline)
set commandline (string replace -- "$prefix" '' $commandline)

# Enable home directory expansion of leading ~/
set commandline (string replace -r -- '^~/' '\$HOME/' $commandline)

# escape special characters, except for the $ sign of valid variable names,
# so that after eval, the original string is returned, but with the
# variable names replaced by their values.
set commandline (string escape -n -- $commandline)
set commandline (string replace -r -a '\x5c\$(?=[\w])' '\$' -- $commandline)
set commandline (string replace -r -a -- '\x5c\$(?=[\w])' '\$' $commandline)

# eval is used to do shell expansion on paths
eval set commandline $commandline

# Combine multiple consecutive slashes into one
set commandline (string replace -r -a '/+' '/' -- $commandline)
set commandline (string replace -r -a -- '/+' '/' $commandline)

if test -z "$commandline"
# Default to current directory with no --query
Expand All @@ -172,12 +175,12 @@ function fzf_key_bindings
# BUG: on combined expressions, if a left argument is a single `!`, the
# builtin test command of fish will treat it as the ! operator. To
# overcome this, have the variable parts on the right.
if test "." = "$dir" -a "." != (string sub -l 1 -- $commandline)
if test "." = "$dir" -a "./" != (string sub -l 2 -- $commandline)
# 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
set fzf_query (string replace -r "^$dir/?" '' -- $commandline)
set fzf_query (string replace -r -- "^$dir/?" '' $commandline)
end
end

Expand All @@ -190,7 +193,7 @@ function fzf_key_bindings
set dir $argv

# Strip trailing slash, unless $dir is root dir (/)
set dir (string replace -r '(?<!^)/$' '' -- $dir)
set dir (string replace -r -- '(?<!^)/$' '' $dir)

# Iteratively check if dir exists and strip tail end of path
while test ! -d "$dir"
Expand Down
Loading