Make more Powerful Bash command line with fzf

You can use more powerful autocompletion or searching text data with fzf on junegunn/fzf: A command-line fuzzy finder.

Very easy to install.

git clone --depth 1 ~/.fzf

For more useful infomation, you should glance through official document of examples.

Examples · junegunn/fzf Wiki

  • Search syntax

Token Match type Description
sbtrkt fuzzy-match Items that match sbtrkt
‘wild exact-match (quoted) Items that include wild
^music prefix-exact-match Items that start with music
.mp3$ suffix-exact-match Items that end with .mp3
!fire inverse-exact-match Items that do not include fire
!^music inverse-prefix-exact-match Items that do not start with music
!.mp3$ inverse-suffix-exact-match Items that do not end with .mp3

On multi-select mode (-m), TAB and Shift-TAB to mark multiple items

When you search with no any special character, that is fuzzy mode.
If you’d like to search with exact term, add a single quote in the front. like that: 'something'

So, let’s begin how to use fzf.

Powerful text searching

You can use this by pipe. search fuge text by one by one. e.g. --help option of some command.

cat <<EOT  |fzf

Powerful command history searching

ctrl-R is a known feature in Bash. fzf make more useful command history with ctrl-R.

Powerful autocompletion

Autocompletion with fzf is doble tab or doble asterisk.

cd **<TAB>
cd ~/github/fzf**<TAB>
head /docker/**<TAB>

# or

kill -9 <TAB>

Make your own autocompletion with fzf

In fzf, You can create your own autocompletion rules more easier.

Like this, You can assign plain text data to autocompletion candidate.

_fzf_complete_history() { _fzf_complete "--multi --reverse" "$@" < <( echo very echo wow echo such echo history ) } [ -n "$BASH" ] && complete -F _fzf_complete_history -o default -o bashdefault history

Also, You can assign output of some command to.

_fzf_complete_sym() {
  _fzf_complete "--multi --reverse" "$@" < <(command ls -1 ${HOME}/sym)

[ -n "$BASH" ] && complete -F _fzf_complete_sym -o default -o bashdefault sym

Actually examples of I usually used

  • Useful man

I used Linux as WSL on Windows 10. So when I want to check document of a command by man, I have to use something like Xserver system.
So, I always use man command with > ~/tmp.txt && sublime_text ~/tmp.txt.

_fzf_complete_man() {
  _fzf_complete "--multi --reverse" "$@" < <(
    # view the man pages with sublime via x-server
    echo "> ~/tmp.txt && sublime_text ~/tmp.txt "

[ -n "$BASH" ] && complete -F _fzf_complete_man -o default -o bashdefault man
  • Useful cd

I make place symlinks in ~/sym directory. In the case of that I want to cd to subdirectory of the sym directory, I can cd to the directory with sym **[the-subdir-name].


: <<'HELP'
e.g. sym
e.g. sym hinabita
e.g. sym **

  [ ! -z ${1} ] && cd "${HOME}/sym/${1}" || cd "${HOME}/sym"

_fzf_complete_sym() {
  _fzf_complete "--multi --reverse" "$@" < <(command ls -1 ${HOME}/sym)

[ -n "$BASH" ] && complete -F _fzf_complete_sym -o default -o bashdefault sym

Articles maybe related

How to copy content to clipboard and Get content of clipboard on Bash

Close Menu