Ble.sh: [fzf] Want to avoid multiline mode for a command

Created on 15 Apr 2020  ·  13Comments  ·  Source: akinomyoga/ble.sh


Hi, just downloaded this last night and was pumped to see it worked perfectly out of the box.
My only grievance is that:
export FZF_ALT_C_COMMAND="bfs -type d -nohidden"
enters multiline every time, I would like the command to just run upon alt-c.

I have tried the following adjustments to no avail:
export FZF_ALT_C_COMMAND="bfs -type d -nohidden | tr -d '\n'"
export FZF_ALT_C_COMMAND="bfs -type d -nohidden; xdotool key ctrl+j"

If it matters here is some system info of mine:

BASH: bash 5.0.16
OS: 5.4.28-1-MANJARO
Terminal: kitty 0.17.2 or neovim terminal
bfs version: 1.6
fzf version: 0.21.1

Again I love the program and would recommend it to others in its current state, but just for feedback's sake I wanted to mention I do notice increased latency for random things like from entering ls to the output being displayed.
Thanks!

Information Needed compatibility

All 13 comments

Thank you for the report!

What is happening

I dug into the problem and found that fzf uses the following hacky binding for M-c.

bind -m emacs-standard '"\ec": " \C-b\C-k \C-u`__fzf_cd__`\e\C-e\er\C-m\C-y\C-h\e \C-y\ey\C-x\C-x\C-d"'

It first (1)saves the current command-line contents, (2)input cd ... and RET, and then (3)restore the original command line contents. To achieve (3), it generates many key inputs after RET. ble.sh detects many key inputs in a very short time span as a paste operation by default, so it actually considers RET as a part of the pasted text. This is the reason why it falls into the multiline mode without executing the cd command.

Configuration

In this case, you have two options with ble-0.4 (master branch). If you are using ble-0.3.2, you need to switch to the version of master branch.

Option 1: Disable paste detection

The simpler option is to change the setting of the paste detection of ble.sh. This can be configured by a ble option accept_line_threshold. To entirely disable the paste detection please put the following setting in your .blerc:

bleopt accept_line_threshold=-1

Option 2: Increase the threshold for paste detection

To increase the number of inputs allowed for non-paste input, please put the following setting in your .blerc (to make the current fzf binding for M-c work, it seems that it should be greater or equal to 12):

bleopt accept_line_threshold=15

Option 3: Use ble.sh key-binding setting (Recommended)

Edit: I decided to provide this setting in contrib submodule, so that you don't have to write these settings for yourself. Please check the description here for the usage.

Outdated

The other option is to use ble.sh native way of key bindings rather than to hack Bash-native bind. This is more robust and reliable. To do this, you can edit your .fzf.bash. The line of source /path/to/fzf/shell/key-bindings.bash can be replaced by the following script (please replace /path/to/fzf by the actual path to fzf directory):

# .fzf.bash

# Key bindings
# ------------
if ((_ble_bash)); then
  # When ble.sh is loaded:
  ble/function#push bind :
  source "/path/to/fzf/shell/key-bindings.bash"
  ble/function#pop bind
  if [[ $- == *i* ]]; then
    # CTRL-T - Paste the selected file path into the command line
    ble-bind -m emacs   -x C-t fzf-file-widget
    ble-bind -m vi_imap -x C-t fzf-file-widget
    ble-bind -m vi_nmap -s C-t 'i\C-t'

    # CTRL-R - Paste the selected command from history into the command line
    ble-bind -m emacs   -x C-r fzf-history-widget
    ble-bind -m vi_imap -x C-r fzf-history-widget
    ble-bind -m vi_nmap -s C-r 'i\C-r'
    function fzf-history-widget {
      READLINE_LINE=$(history -p "$(__fzf_history__)")
      READLINE_POINT=${#READLINE_LINE}
    }

    # ALT-C - cd into the selected directory
    ble-bind -m emacs   -c M-c 'eval "$(__fzf_cd__)"'
    ble-bind -m vi_imap -c M-c 'eval "$(__fzf_cd__)"'
    ble-bind -m vi_nmap -c M-c 'eval "$(__fzf_cd__)"'
  fi
else
  # If ble.sh is not loaded, just source the script.
  source "/path/to/fzf/shell/key-bindings.bash"
fi

@dylankb @Brendonk13 There was another problem related to the integration with fzf (#41). One needs to write some codes to make ble.sh and fzf work together, so I decided to provide such settings under a submodule contrib.

If you are interested, please run ble-update twice for the devel version (master branch), and now you can use simple settings described here for fzf integration and here for fzf-git bindings. Also, if you have any improvements to these settings or any other interesting ble.sh settings, feel free to open a PR for the contrib repository.

Very cool, thanks mentioning this. Not to hijack the thread, but I ran ble-update twice to get to 0.4.0-devel2+f290115 and tried switching over but I'm getting errors when I start a new session.

$ ble/function#advice: __fzf_generic_path_completion is not a function.
ble/function#advice: _fzf_complete is not a function.
ble/function#advice: _fzf_complete_kill is not a function.
-bash: /usr/local/shell/key-bindings.bash: No such file or directory

This is the setup code I used from the documentation for option 2 you linked to.

# in .blerc
if [[ ! "$PATH" == *"/usr/local/opt/fzf/bin"* ]]; then
  export PATH="${PATH:+${PATH}:}/usr/local/opt/fzf/bin"
fi
ble-import -d contrib/fzf-completion
ble-import -d contrib/fzf-key-bindings

And the path/to/fzf/bin I'm using should be a valid path.

$ ls /usr/local/opt/fzf/bin
fzf fzf-tmux

I also tried the customize and load your own .fzf.bash file approach (option 1 in the docs) but I got the same errors.

Thank you! It seems it fails to detect the correct fzf base directory. Currently it tries to determine the fzf directory based on the path of command fzf. From your message, I guess you have fzf also in /usr/local/bin/fzf. Is it a symbolic link to /usr/local/opt/fzf/bin/fzf? I update the detection code based on the assumption it is a symbolic link (https://github.com/akinomyoga/blesh-contrib/commit/682fdb72f4f148d4d60b5edf3aa9f6974d0e147f). Could you try the latest version with ble-update?

In case the auto detection of fzf path doesn't work, please add the following line before ble-import.

_ble_contrib_fzf_base=/usr/local/opt/fzf

I ran ble-update and now everything looks to work great - no additional config was necessary. Thanks for the quick fix!

@dylankb Thank you for testing!

@Brendonk13 Hi, could I ask if your problem has been solved? As already described above, please follow the description here for integration with fzf. If you have problems please let me know here. Anyway, thank you for your report.

One issue I've noticed when I've tried to use C-r to searching through history and pasting the selection on the command line does not work with the new contrib set up. When I enter C-r I am able to select a command from history but when I hit Return on the selection nothing is pasted into the command line. This does work if I disable ble.sh. It looks like there is code around this here. https://github.com/akinomyoga/blesh-contrib/blob/master/fzf-key-bindings.bash#L16

@dylankb Thank you! It seems fzf has been recently changed at https://github.com/junegunn/fzf/commit/7c447bbdc7adb1fe0c848e171780f68cacf3e80e (Feb 28). The related PR is https://github.com/junegunn/fzf/pull/1886. Actually, I made the contrib setting when you opened #41 (Feb 9) which was before the fzf change. I added a fix https://github.com/akinomyoga/blesh-contrib/commit/85fbd04d91c4964da474e517c0243b1aefcbd707. You can update it by ble-update!

Awesome, that fixed it!

Nice. Thank you for testing!

ping @Brendonk13 Thank you for the report! It has been about five months after you created an issue, but there are no replies from you. If you don't have a plan to provide us further information, could you please close the issue? If you are thinking of making some responses but are busy for now, please let us know that! Thank you again for your help!

@Brendonk13 I'll close this issue.

Was this page helpful?
0 / 5 - 0 ratings