| 1 | # :pencil: Integration with `fzf` |
| 2 | |
| 3 | ## Configure fzf's `completion` and `key-bindings` |
| 4 | |
| 5 | In the following cases, a special care is needed to make it work with `ble.sh`. |
| 6 | |
| 7 | - When you want to load fzf's `completion.bash` (sometimes installed at |
| 8 | `/etc/bash_completion.d/fzf` or `/usr/share/bash-completion/completions/fzf`) |
| 9 | or `key-bindings.bash`. |
| 10 | - When you want to load fzf's settings by `eval "$(fzf --bash)"` (supported by |
| 11 | `fzf >= 0.48.0`). |
| 12 | |
| 13 | We require those settings to be loaded through our modules :pencil: |
| 14 | [`contrib/integration/fzf-completion`](https://github.com/akinomyoga/blesh-contrib/blob/master/integration/fzf-completion.bash) |
| 15 | and :pencil: |
| 16 | [`contrib/integration/fzf-key-bindings`](https://github.com/akinomyoga/blesh-contrib/blob/master/integration/fzf-key-bindings.bash). |
| 17 | |
| 18 | > [!IMPORTANT] |
| 19 | > If you want to integrate fzf's `completion.bash` with |
| 20 | > [`bash-completion`](https://github.com/scop/bash-completion), |
| 21 | > `bash-completion` needs to be loaded before fzf's `completion.bash` is |
| 22 | > loaded. |
| 23 | |
| 24 | > [!IMPORTANT] |
| 25 | > In the Bash sessions where `ble.sh` is loaded, fzf's settings loaded |
| 26 | > elsewhere needs to be disabled. If you want to support the case without |
| 27 | > `ble.sh`, please enable the fzf's settings conditionally when `ble.sh` is not |
| 28 | > loaded. |
| 29 | |
| 30 | There are several ways to configure them. |
| 31 | |
| 32 | ### Option 1: Set up in `~/.blerc` |
| 33 | |
| 34 | The current recommended way is to directly write settings in your `.blerc` as follows: |
| 35 | |
| 36 | ```bash |
| 37 | # blerc |
| 38 | |
| 39 | # If ble/contrib/integration/fzf cannot find the fzf directory, please set the |
| 40 | # following variable "_ble_contrib_fzf_base" manually. The value |
| 41 | # "/path/to/fzf-directory" should be replaced by a path to the fzf directory |
| 42 | # such as "$HOME/.fzf" or "/usr/share/fzf" that contain |
| 43 | # "shell/{completion,key-bindings}.bash" or "{completion,key-bindings}.bash". |
| 44 | |
| 45 | #_ble_contrib_fzf_base=/path/to/fzf-directory |
| 46 | |
| 47 | |
| 48 | # Note: If you would like to combine fzf-completion with bash_completion, you |
| 49 | # need to load bash_completion earlier than fzf-completion. |
| 50 | |
| 51 | #source -- /path/to/bash_completion.sh |
| 52 | |
| 53 | |
| 54 | # Set up fzf |
| 55 | ble-import -d integration/fzf-completion |
| 56 | ble-import -d integration/fzf-key-bindings |
| 57 | ``` |
| 58 | |
| 59 | #### When you have additional settings for fzf |
| 60 | |
| 61 | The option `-d` of `ble-import` in the above example means the asynchronous |
| 62 | loading of the modules. If you need to immediately load the module, please |
| 63 | drop the option `-d`. For example, if the user needs to call a function |
| 64 | defined in a module, the module can be loaded immediately, so that the user can |
| 65 | use the function. |
| 66 | |
| 67 | ```bash |
| 68 | ble-import integration/fzf-completion |
| 69 | _fzf_set_up_completion foo bar |
| 70 | ``` |
| 71 | |
| 72 | However, in most cases, the user would probably want to reserve the execution |
| 73 | of the commands relying on the module by using the `-C callback` option. In |
| 74 | this way, the reserved command is evaluated when the module is successfully |
| 75 | loaded. |
| 76 | |
| 77 | ```bash |
| 78 | ble-import -d integration/fzf-completion \ |
| 79 | -C '_fzf_set_up_completion foo bar' |
| 80 | ``` |
| 81 | |
| 82 | ### Option 2: Set up in `~/.fzf.bash` |
| 83 | |
| 84 | If you have `~/.fzf.bash` for the fzf settings, you can rewrite your |
| 85 | `.fzf.bash` in the following way. In this case, `.fzf.bash` needs to be |
| 86 | sourced after `ble.sh`. Please replace `/path/to/fzf-directory`, |
| 87 | `/path/to/fzf/...` and `/path/to/bash_completion.sh` if necessary. |
| 88 | |
| 89 | ```bash |
| 90 | # fzf.bash |
| 91 | |
| 92 | # If ble/contrib/integration/fzf cannot find the fzf directory, please set the |
| 93 | # following variable "_ble_contrib_fzf_base" manually. The value |
| 94 | # "/path/to/fzf-directory" should be replaced by a path to the fzf directory |
| 95 | # such as "$HOME/.fzf" or "/usr/share/fzf" that contain |
| 96 | # "shell/{completion,key-bindings}.bash" or "{completion,key-bindings}.bash". |
| 97 | |
| 98 | #_ble_contrib_fzf_base=/path/to/fzf-directory |
| 99 | |
| 100 | # Setup fzf |
| 101 | # --------- |
| 102 | if [[ ! "$PATH" == *"/path/to/fzf/bin"* ]]; then |
| 103 | export PATH="${PATH:+${PATH}:}/path/to/fzf/bin" |
| 104 | fi |
| 105 | |
| 106 | # Auto-completion |
| 107 | # --------------- |
| 108 | if [[ $- == *i* ]]; then |
| 109 | # Note: If you would like to combine fzf-completion with bash_completion, you |
| 110 | # need to load bash_completion earlier than fzf-completion. |
| 111 | |
| 112 | #source -- /path/to/bash_completion.sh |
| 113 | |
| 114 | if [[ ${BLE_VERSION-} ]]; then |
| 115 | ble-import -d integration/fzf-completion |
| 116 | else |
| 117 | source -- /path/to/fzf/shell/completion.bash 2> /dev/null |
| 118 | fi |
| 119 | fi |
| 120 | |
| 121 | # Key bindings |
| 122 | # ------------ |
| 123 | if [[ ${BLE_VERSION-} ]]; then |
| 124 | ble-import -d integration/fzf-key-bindings |
| 125 | else |
| 126 | source -- /path/to/fzf/shell/key-bindings.bash |
| 127 | fi |
| 128 | ``` |
| 129 | |
| 130 | ## :pencil: `integration/fzf-completion` |
| 131 | |
| 132 | The module `integration/fzf-completion` mentioned above also provides a widget. |
| 133 | |
| 134 | ### Widget `fzf-complete` |
| 135 | |
| 136 | This widget performs fzf's completion normally triggered by the key `**`. |
| 137 | |
| 138 | ``` |
| 139 | widget: fzf-complete [type] |
| 140 | |
| 141 | @param[in,opt] type |
| 142 | This specifies the type of the completion. The shell function |
| 143 | "_fzf_${type}_completion" is used to perform the '**' completion. The |
| 144 | values available by default are "path", "file", "dir", "proc", "host", |
| 145 | "var", and "alias". |
| 146 | ``` |
| 147 | |
| 148 | ## :pencil: `integration/fzf-git` |
| 149 | |
| 150 | If you want to use |
| 151 | [`junegunn/fzf-git.sh`](https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236) |
| 152 | (fzf settings for `git` formerly provided as a |
| 153 | [Gist](https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236)) with |
| 154 | `ble.sh`, you instead need to use :pencil: |
| 155 | [`contrib/integration/fzf-git`](https://github.com/akinomyoga/blesh-contrib/blob/master/integration/fzf-git.bash). |
| 156 | |
| 157 | ```bash |
| 158 | # bashrc / fzf.bash |
| 159 | if [[ ${BLE_VERSION-} ]]; then |
| 160 | # If needed. See above for details: |
| 161 | #_ble_contrib_fzf_base=/path/to/fzf-directory |
| 162 | |
| 163 | _ble_contrib_fzf_git_config=key-binding:sabbrev:arpeggio |
| 164 | ble-import -d integration/fzf-git |
| 165 | fi |
| 166 | ``` |
| 167 | |
| 168 | Or you can configure it in `~/.blerc`: |
| 169 | |
| 170 | ```bash |
| 171 | # blerc |
| 172 | |
| 173 | # If needed. See above for details: |
| 174 | #_ble_contrib_fzf_base=/path/to/fzf-directory |
| 175 | |
| 176 | _ble_contrib_fzf_git_config=key-binding:sabbrev:arpeggio |
| 177 | ble-import -d integration/fzf-git |
| 178 | ``` |
| 179 | |
| 180 | The shell variable `_ble_contrib_fzf_git_config` is a colon-separated list of the enabled types of bindings. |
| 181 | The value `key-binding` enables the key bindings of the form <kbd>C-g C-f</kbd>, <kbd>C-g C-b</kbd>, <kbd>C-g C-t</kbd>, <kbd>C-g C-h</kbd>, <kbd>C-g C-r</kbd>, and <kbd>C-g C-s</kbd>. |
| 182 | The value `sabbrev` enables the sabbrev expansion for the words `gf`, `gb`, `gt`, `gh`, `gr`, and `gs`. |
| 183 | The value `arpeggio` enables the simultaneous key combinations of <kbd>g f</kbd>, <kbd>g b</kbd>, <kbd>g t</kbd>, <kbd>g h</kbd>, <kbd>g r</kbd>, and <kbd>g s</kbd>. |
| 184 | The value `old-functions` enables old function names: `is_in_git_repo`, `fzf-down`, `gh`, `gb`, `gt`, `gh`, `gr`, and `gs`. |
| 185 | |
| 186 | ## :pencil: `integration/fzf-menu` |
| 187 | |
| 188 | This module configures so that when the menu completion is started, fzf is |
| 189 | started instead of `menu-complete` built in `ble.sh`. |
| 190 | |
| 191 | ```bash |
| 192 | # blerc |
| 193 | |
| 194 | ble-import -d integration/fzf-menu |
| 195 | ``` |