Let see how we can use fzf
for getting an input from the user... and i don't just mean to choose from a list of items, as fzf does normally. With the proper arguments/paramaters of fzf we can use it, as an input function, where the user, would be able to type anything, not just select a choice and also confirm his input at the same time. Of course, we can use fzf as normal and also provide selections for the user to chose from, but the result of the parameter will not be the selection, only what he typed.
fzf has the ability to return the query string as result and this is what we will use. We will bind the Enter key to insert the selected item as the query string and the user to accept what he typed, he will have to press CTRL-Y as a method of confirmation, avoiding mistakes. The full command is as follows:
fzf --print-query --bind 'enter:replace-query' --bind 'ctrl-y:replace-query+print-query' --header $'\x1b[1;32m'"Enter: Input selection. CTRL+Y:Accept query and proceed"
- Enter: Sets the selected item in the query string
- CTRL-Y: Accepts the query string as the response/reply of the user
- We put a header with info on how to use it
At the same time, we can feed fzf normally, thus helping the user to avoid mistakes or see possible choices of what he can write. The most useful way to use this functionality for me, is to use it like an input for a filename/path. We can feed fzf with all files in a directory, for the user to choose from and if he wants, he can easily type another filename and don't just select an option.