junegunn / fzf

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

Bash: How do I escape execute-silent(nvim {}) in multi-line FZF_DEFAULT_OPTS? #3114

Closed eggbean closed 1 year ago

eggbean commented 1 year ago

I have FZF_DEFAULT_OPTS set like this, which has always worked fine, but I want to add --bind=ctrl-o:'execute-silent(nvim {})', but I do not know how to escape the {} or whatever that needs to be escaped, as it this doesn't work:

export FZF_DEFAULT_OPTS=" \
  --ansi \
  --reverse \
  --bind=ctrl-a:toggle-all \
..........
  --bind=ctrl-u:preview-page-up \
  --bind=ctrl-o:execute-silent(nvim {})+abort \  <----How do I fix this?
  --bind=alt-bs:clear-query \
..........
  --color spinner:#8BE9FD \
  --color header:#8BE9FD \
"

The introduction of this new key binding breaks the whole variable.

junegunn commented 1 year ago
export FZF_DEFAULT_OPTS="
  --ansi
  --reverse
  --bind=ctrl-a:toggle-all
  --bind=ctrl-u:preview-page-up
  --bind='ctrl-o:execute(nvim {})+abort'
  --bind=alt-bs:clear-query
  --color spinner:#8BE9FD
  --color header:#8BE9FD
"
eggbean commented 1 year ago

Thank you for replying, but using your line above now nvim does execute, it seems to immediately exit, and fzf doesn't abort, so needs to be cancelled. It would have been a nice way to open files in the text editor, but it doesn't seem to be possible.

Also, my variable doesn't work at all without the trailing backslash line endings. I am using tmux with fzf appearing in a popup window, if that makes any difference.

export FZF_DEFAULT_OPTS=" \
  --ansi \
  --reverse \
  --bind=ctrl-a:toggle-all \
  --bind=ctrl-alt-j:preview-down \
  --bind=ctrl-alt-k:preview-up \
  --bind=ctrl-d:preview-page-down \
  --bind=ctrl-u:preview-page-up \
  --bind='ctrl-o:execute($EDITOR {})+abort' \
  --bind=alt-bs:clear-query \
  --bind=ctrl-h:deselect \
  --bind=ctrl-l:select \
  --color fg:#F8F8F2 \
  --color fg+:#F8F8F2 \
  --color bg:-1 \
  --color bg+:-1 \
  --color hl:#50FA7B \
  --color hl+:#FFB86C \
  --color info:#BD93F9 \
  --color prompt:#50FA7B \
  --color pointer:#FF79C6 \
  --color marker:#FF5555 \
  --color spinner:#8BE9FD \
  --color header:#8BE9FD \
"
junegunn commented 1 year ago

Works for me fine without backslashes. Try changing the command to $EDITOR {} > /dev/tty.

https://github.com/junegunn/fzf/issues/1360#issuecomment-966054123

eggbean commented 1 year ago

Thanks, that works. However, the text editor opens in the tmux popup window instead of in the original pane. I suppose I should have expected this. Cheers.

junegunn commented 1 year ago

opens in the tmux popup window

fzf is a filter program that is designed to be used in shell scripts and this is what I would do.

# This should work well
result=$(fzf-tmux -p) && vim "$result"

IMO, relying on execute+abort instead feels a bit like a hack. And like I mentioned above, fzf isn't just for files so setting up a global binding that opens an entry in the editor is not recommended.

But if you really want, you can do something like tmux new-window $EDITOR {}.

eggbean commented 1 year ago

That works great. I won't use this keybinding when I am not using fzf to show text files, so I think it will be okay.

Thanks for your help.