fzf - Find CLI Application and Execute

01/01/2023, Sun
Categories: #fzf
Tags: #cli-tools

Run the Application when Found

When you have an application you wish to search for that you know starts with a certain letter, you type the letter into your Bash shell and press tab to display all the suggestions. Most likely, there will be more applications than one would be able to identify readily by eye.

To make the task of finding your application easier, one can use fzf to perform real-time filtering of your application search.

First, we need to display all the applications on your system with the following.

echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 -executable -type f -printf '%P\n' | sort -u

Next, the output of all the applications will be piped to fzf. Upon typing enough characters to narrow down to the entry of your choice, execute the command by pressing ctrl+x.

echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 -executable -type f -printf '%P\n' | sort -u | fzf '--bind=enter:accept,ctrl-x:execute({})+accept'

To make entering all this into the shell with less hassle, create a Bash function which also takes in arguments to provide the application arguments or options for checking if is the application that you are after.

findapp() {
  echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 -executable -type f -printf '%P\n' | sort -u | fzf "--bind=enter:accept,ctrl-x:execute({} $1)+accept"
}

# Displays the 'help' information of the application 
# findapp --help