Skip to content

Commit 02892b8

Browse files
committed
fish: Fixes and improvements to CTRL-R
Fixes: - Commands with trailing newlines - Very long previews Improvements: - SHIFT-DELETE performance - Bind ALT-T: cycle command prefix (timestamp, date/time, none) - Set comment color for prefix
1 parent eacef5e commit 02892b8

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,17 @@ the following key bindings in bash, zsh, and fish.
506506
```
507507
- Can be disabled by setting `FZF_CTRL_T_COMMAND` to an empty string when
508508
sourcing the script
509-
- `CTRL-R` - Paste the selected command from history onto the command-line
509+
- `CTRL-R` - Paste the selected command from history onto the command-line. With fish shell, it is possible to select multiple commands.
510510
- If you want to see the commands in chronological order, press `CTRL-R`
511511
again which toggles sorting by relevance
512512
- Press `ALT-R` to toggle "raw" mode where you can see the surrounding items
513513
of a match. In this mode, you can press `CTRL-N` and `CTRL-P` to move
514514
between the matching items only.
515515
- Press `CTRL-/` or `ALT-/` to toggle line wrapping
516+
- Fish shell only:
517+
- Press `SHIFT-DELETE` to delete the selected commands
518+
- Press `ALT-ENTER` to reformat and insert the selected commands
519+
- Press `ALT-T` to cycle through command prefix (timestamp, date/time, none)
516520
- Set `FZF_CTRL_R_OPTS` to pass additional options to fzf
517521
```sh
518522
# CTRL-Y to copy the command into clipboard using pbcopy
@@ -521,6 +525,13 @@ the following key bindings in bash, zsh, and fish.
521525
--color header:italic
522526
--header 'Press CTRL-Y to copy command into clipboard'"
523527
```
528+
```fish
529+
# Fish shell: Set date/time as default prefix
530+
set -gx FZF_CTRL_R_OPTS "--with-nth 1,3.. --bind 'alt-t:change-with-nth(2..|3..|1,3..)'"
531+
532+
# Or display no prefix by default
533+
set -gx FZF_CTRL_R_OPTS "--with-nth 3.. --bind 'alt-t:change-with-nth(2..|1,3..|3..)'"
534+
```
524535
- Can be disabled by setting `FZF_CTRL_R_COMMAND` to an empty string when
525536
sourcing the script
526537
- Custom override via a non-empty `FZF_CTRL_R_COMMAND` is not yet supported and will emit a warning

shell/key-bindings.fish

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,36 +183,27 @@ function fzf_key_bindings
183183
set -l -- fzf_query (string escape -- $command_line[$current_line])
184184

185185
set -lx -- FZF_DEFAULT_OPTS (__fzf_defaults '' \
186-
'--nth=2..,.. --scheme=history --multi --no-multi-line --no-wrap --wrap-sign="\t\t\t↳ " --preview-wrap-sign="↳ "' \
187-
'--bind=\'shift-delete:execute-silent(for i in (string split0 -- <{+f}); eval builtin history delete --exact --case-sensitive -- (string escape -n -- $i | string replace -r "^\d*\\\\\\t" ""); end)+reload(eval $FZF_DEFAULT_COMMAND)\'' \
188-
'--bind="alt-enter:become(string join0 -- (string collect -- {+2..} | fish_indent -i))"' \
186+
'--with-nth=2.. --nth=2..,.. --scheme=history --multi --no-multi-line' \
187+
'--no-wrap --wrap-sign="\t\t\t↳ " --preview-wrap-sign="↳ " --freeze-left=1' \
188+
'--bind="enter:become(set -g fzf_temp {+sf3..}; string trim -r -c \\\\n -- <$fzf_temp; unlink $fzf_temp &>/dev/null)"' \
189+
'--bind="alt-enter:become(set -g fzf_temp {+sf3..}; string join0 -- (string split0 -- <$fzf_temp | fish_indent -i); unlink $fzf_temp &>/dev/null)"' \
190+
'--bind="alt-t:change-with-nth(1,3..|3..|2..)"' \
191+
'--bind="shift-delete:execute-silent(eval builtin history delete -Ce -- (string escape -n -- (string split0 -- <{+sf3..})))+reload(eval $FZF_DEFAULT_COMMAND)"' \
189192
"--bind=ctrl-r:toggle-sort,alt-r:toggle-raw --highlight-line $FZF_CTRL_R_OPTS" \
190-
'--accept-nth=2.. --delimiter="\t" --tabstop=4 --read0 --print0 --with-shell='(status fish-path)\\ -c)
193+
'--delimiter="\t" --tabstop=4 --read0 --print0 --with-shell='(status fish-path)\\ -c)
191194

192195
# Add dynamic preview options if preview command isn't already set by user
193196
if string match -qvr -- '--preview[= ]' "$FZF_DEFAULT_OPTS"
194-
# Convert the highlighted timestamp using the date command if available
195-
set -l -- date_cmd '{1}'
196-
if type -q date
197-
if date -d @0 '+%s' 2>/dev/null | string match -q 0
198-
# GNU date
199-
set -- date_cmd '(date -d @{1} \\"+%F %a %T\\")'
200-
else if date -r 0 '+%s' 2>/dev/null | string match -q 0
201-
# BSD date
202-
set -- date_cmd '(date -r {1} \\"+%F %a %T\\")'
203-
end
204-
end
205-
206-
# Prepend the options to allow user customizations
197+
# Prepend the options to allow user overrides
207198
set -p -- FZF_DEFAULT_OPTS \
208-
'--bind="focus,resize:bg-transform:if test \\"$FZF_COLUMNS\\" -gt 100 -a \\\\( \\"$FZF_SELECT_COUNT\\" -gt 0 -o \\\\( -z \\"$FZF_WRAP\\" -a (string length -- {}) -gt (math $FZF_COLUMNS - 4) \\\\) -o (string collect -- {2..} | fish_indent | count) -gt 1 \\\\); echo show-preview; else echo hide-preview; end"' \
209-
'--preview="string collect -- (test \\"$FZF_SELECT_COUNT\\" -gt 0; and string collect -- {+2..}) \\"\\n# \\"'$date_cmd' {2..} | fish_indent --ansi"' \
199+
'--bind="focus,multi,resize:bg-transform:if test \\"$FZF_COLUMNS\\" -gt 100 -a \\\\( \\"$FZF_SELECT_COUNT\\" -gt 0 -o \\\\( -z \\"$FZF_WRAP\\" -a (string join0 -- <{f3..} | string length) -gt (math $FZF_COLUMNS - (switch $FZF_WITH_NTH; case 2..; echo 13; case 1,3..; echo 25; case 3..; echo 1; end)) \\\\) -o (string split0 -- <{sf3..} | fish_indent | count) -gt 1 \\\\); echo show-preview; else echo hide-preview; end"' \
200+
'--preview="test \\"$FZF_SELECT_COUNT\\" -gt 0; and string split0 -- <{+sf3..} | fish_indent --only-indent --ansi; and echo -n \\\\n; string collect -- \\\\#\\\\ {1} (string split0 -- <{sf3..}) | fish_indent --ansi"' \
210201
'--preview-window="right,50%,wrap-word,follow,info,hidden"'
211202
end
212203

213204
set -lx FZF_DEFAULT_OPTS_FILE
214205

215-
set -lx -- FZF_DEFAULT_COMMAND 'builtin history -z --show-time="%s%t"'
206+
set -lx -- FZF_DEFAULT_COMMAND 'builtin history -z --show-time=(set_color $fish_color_comment)"%F %a %T%t%s%t"(set_color $fish_color_normal)'
216207

217208
# Enable syntax highlighting colors on fish v4.3.3 and newer
218209
if set -l -- v (string match -r -- '^(\d+)\.(\d+)(?:\.(\d+))?' $version)

0 commit comments

Comments
 (0)