Improve your bash skills in 3 minutes with one trick!

Improve your bash skills in 3 minutes with one trick!

I know. You have your brilliant IDE. That's great, but the fact is that from time to time you need to do something from the command line. There is one useful tool I've discovered some time ago which solves the problem of navigation through files and commands.

The trick - FZF

I won't bore you with the installation guide, the docs describe that well. I only add you should not skip the key bindings part, it adds FZF to key bindings such as CTRL+R (history search) and CTRL+T (files search).

And that's it - I encourage you to read the documentation. Here is a cheat sheet of things I love the most:

Execute repeatable command

For sure you have some commands you often use, e.g. for authentication to AWS. FZF introduces an addition that displays a list of used commands.

CTRL+R ezgif.com-gif-maker.gif

Find and open the file

Using a similar search, we can find the file and open it in any program.

CTRL+T ezgif.com-gif-maker-2.gif

You can achieve the same with executable_name $(fzf) (e.g. cat $(fzf)) or even fzf --preview 'cat {}'. The documentation says you can customize the list of files using additional flags such as --info or --preview: fzf --info inline --preview 'cat {}'.

You can also use files search with any command adding ** and then pressing TAB: ezgif.com-gif-maker-6.gif

Autocompletions

You wanna kill a process? No problem - use TAB to find the process:

ezgif.com-gif-maker-3.gif

You wanna add your own autocomplete for the executable? Not a problem as well:

Add a proper entry to .fzf.zsh, here is an example of autocomplete I use with docker-compose:

# Setup fzf
# ---------
if [[ ! "$PATH" == */opt/homebrew/opt/fzf/bin* ]]; then
  export PATH="${PATH:+${PATH}:}/opt/homebrew/opt/fzf/bin"
fi

# Auto-completion
# ---------------
_fzf_complete_docker-compose() {
  _fzf_complete --multi --reverse --prompt="Select service> " -- "$@" < <(
    docker-compose ps -a --services
  )
}

[[ $- == *i* ]] && source "/opt/homebrew/opt/fzf/shell/completion.zsh" 2> /dev/null


# Key bindings
# ------------
source "/opt/homebrew/opt/fzf/shell/key-bindings.zsh"

Then refresh your config with source ~/.fzf.zsh and run any compose command - docker-compose stop **[TAB]

Screenshot 2022-06-22 at 20.39.43.png