junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
61.96k stars 2.34k forks source link

Issue with single key binding to toggle file and directory mode #3833

Closed backpropper closed 1 month ago

backpropper commented 1 month ago

Checklist

Output of fzf --version

0.52.1

OS

Shell

Problem / Steps to reproduce

I tried to use the command defined here Toggling with a single key binding. While the command works, the starting window shows a weird ')' at the top of the Files with some of the original command displaying in the preview window as shown below

image

Can you please guide me on how what might be wrong here?

junegunn commented 1 month ago

Are you running the exact command from the page, or a modified version of it? I can confirm that it works as expected both on bash and zsh. Do you have any (global) alias that might affect the code?

backpropper commented 1 month ago

Thanks for following up. After restarting a new shell, it did work for me out of the box.

fd --type file |
  fzf --prompt 'Files>' \
      --header 'CTRL-T: Switch between Files/Directories' \
      --bind 'ctrl-t:transform:[[ ! $FZF_PROMPT =~ Files ]] && \
              echo "change-prompt(Files>)+reload(fd --type file)" || \
              echo "change-prompt(Directories>)+reload(fd --type directory)"' \
      --preview '[[ $FZF_PROMPT =~ Files ]] && bat --color=always {} || tree -C {}'

What I'm trying to do is to make it an alias or replace FZF_CTRL_T_COMMAND with it. When I try to convert this cmd to make it compatible with either of them , I get those syntax errors. I'm aware I can source it as a separate script but I wanted to keep it within my .fzfrc. Is it possible to do so?

junegunn commented 1 month ago

This will do.

FZF_CTRL_T_COMMAND='fd --type file'
read -d '' FZF_CTRL_T_OPTS << "EOF"
  --prompt 'Files>' \
  --multi \
  --header 'CTRL-T: Switch between Files/Directories' \
  --bind 'ctrl-t:transform:[[ ! $FZF_PROMPT =~ Files ]] && \
          echo "change-prompt(Files>)+reload(fd --type file)" || \
          echo "change-prompt(Directories>)+reload(fd --type directory)"' \
  --preview '[[ $FZF_PROMPT =~ Files ]] && bat --color=always {} || tree -C {}'
EOF
backpropper commented 1 month ago

Thanks a lot! This is awesome!!

backpropper commented 1 month ago

I found the original condition where I was getting this behavior: So if I do C-T in my home directory (~) I get this weird thing at the top whereas if I do it on any other directory it works fine.

esn89 commented 3 weeks ago

@junegunn

This will do.

FZF_CTRL_T_COMMAND='fd --type file'
read -d '' FZF_CTRL_T_OPTS << "EOF"
  --prompt 'Files>' \
  --multi \
  --header 'CTRL-T: Switch between Files/Directories' \
  --bind 'ctrl-t:transform:[[ ! $FZF_PROMPT =~ Files ]] && \
          echo "change-prompt(Files>)+reload(fd --type file)" || \
          echo "change-prompt(Directories>)+reload(fd --type directory)"' \
  --preview '[[ $FZF_PROMPT =~ Files ]] && bat --color=always {} || tree -C {}'
EOF

I am on fish shell right now with my settings in ~/.config/fish/conf.d/fzf.fish:

I gave this a shot but the toggling doesn't seem to work:

set -gx FZF_DEFAULT_COMMAND 'fd --type f --color=always' set -gx FZF_DEFAULT_OPTS '--color=fg:#d3c6aa,fg+:#d3c6aa,bg:#2e383c,bg+:#495156 --color=hl:#a7c080,hl+:#83c092,info:#afaf87,marker:#dbbc7f --color=prompt:#e67e80,spinner:#d699b6,pointer:#7fbbb3,header:#87afaf --color=border:#7a8478,label:#d3c6aa,query:#d3c6aa --border="sharp" --border-label="fzf" --border-label-pos="0" --preview-window="border-rounded" --prompt="> " --marker=">" --pointer="◆" --separator="─" --scrollbar="│" --layout="reverse" --info="right" --preview "bat --style=numbers --color=always --theme=\"Catppuccin Frappe\" --line-range :500 {}"' set -gx FZF_CTRL_T_COMMAND 'fd --type file' set -gx FZF_CTRL_T_OPTS '--prompt "Files>" --multi --header "CTRL-T: Switch between Files/Directories" --bind "ctrl-t:transform:[[ ! $FZF_PROMPT =~ Files ]] && echo \"change-prompt(Files>)+reload(fd --type file)" || echo "change-prompt(Directories>)+reload(fd --type directory)"\" --preview "[[ $FZF_PROMPT =~ Files ]] && bat --color=always {} || tree -C {}"'



I don't get an error, but hitting "Ctrl-T" nothing ever changes:

[imgur](https://imgur.com/a/Io6Obgl)
backpropper commented 3 weeks ago

Try ChatGPT and others?

LangLangBart commented 3 weeks ago

I gave this a shot but the toggling doesn't seem to work:

What is the output of echo $SHELL? I'm asking because this syntax is not supported in fish: [[ … =~ … ]].

If SHELL is set to fish, you can add the --with-shell "zsh -c" flag or adopt the regex-specific syntax in fish ^1.

Also, in your assignment for FZF_CTRL_T_OPTS, I see unescaped double quotes inside double-quoted strings. Is this legal in fish?

esn89 commented 2 weeks ago

I gave this a shot but the toggling doesn't seem to work:

What is the output of echo $SHELL? I'm asking because this syntax is not supported in fish: [[ … =~ … ]].

If SHELL is set to fish, you can add the --with-shell "zsh -c" flag or adopt the regex-specific syntax in fish 1.

Also, in your assignment for FZF_CTRL_T_OPTS, I see unescaped double quotes inside double-quoted strings. Is this legal in fish?

Footnotes

1. https://fishshell.com/docs/current/cmds/string.html#match-regex-examples [↩](#user-content-fnref-1-76abf7b928ac7fc5533838b54784843e)

Thanks for the detailed response. Let me give this a shot.

echo $SHELL gives me:

echo $SHELL
/opt/local/bin/fish