Introduction
Fzf can make you become a real ‘Terminal Wizard’ and increase your productivy. I will tell you some magic secrets of the best software written with golang(Docker sucks).
Getting started
You can install fzf in many ways; package managers(pacman btw), source build, flatpack, etc. I will show the source build way, because it is more generic.
1
2
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
Basic Features
Now, we have the most basic FZF feature that is… Search for file!!. If you type fzf inside a terminal emulator and then type your file name, you will see fzf in action.
Change directories
Here we will start to see the magic fzf can make. You can traverse directories in such many ways;
- Using a GUI program(The normie way)
- Using fzf(The epick Haskell wizard way)
For that one magic trick, you will just need to type that command, and then you can traverse between directories in an easy way.
1
2
cd $(find * -type d | fzf)
- Find * -type d – gives us a list of all directories listed on that directory.
- fzf - will use the input to filter through the list.
- cd - maybe that’s will change the directory.
Git Navigation
The second trick(My favorite), is the ability to search through the git history with fzf.
First, you’ll need to install the git delta plugin and config it. Check Delta. Now, it’s time to write some git alias(just a standard alias, but for the git command), open the .gitconfig file, and let’s go hack some things.
You can find a more chad version of that file on my dotfiles repo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# /home/user/.gitignore
[core]
pager = delta
[interactive]
diffFilter = delta --color-only
[delta]
navigate = true # use n and N to move between diff sections
light = false # set to true if you're in a terminal w/ a light background color (e.g. the default macOS terminal)
[alias]
browse = !" \
function gfzf() { \
local filter=\"$@\"; \
local -a show_filter; \
while [[ \"$#\" -gt 0 ]]; do \
if [[ \"$1\" == \"--\" ]] || [[ \"$1\" != '-'* ]] || [[ \"${#show_filter[@]}\" -gt 0 ]]; then \
show_filter+=( \"$1\" ); \
fi; \
shift; \
done; \
if [[ \"${show_filter[0]}\" == '--' ]]; then \
show_filter=( \"${show_filter[@]:1}\" ); \
fi; \
export LESS='-R'; \
export BAT_PAGER='less -S -R -M -i'; \
git log \
--graph --color=always --abbrev=7 --glob=\"refs/heads/*\" \
--format=format:\"%C(bold blue)%h%C(reset) %C(dim white)%an%C(reset)%C(bold yellow)%d%C(reset) %C(white)%s%C(reset) %C(bold green)(%ar)%C(reset)\" $filter | \
fzf --ansi --no-sort --layout=reverse --tiebreak=index \
--preview=\"f() { \
set -- \\$(echo -- \\$@ | rg -o '\\b[a-f0-9]{7,}\\b'); \
[ \\$# -eq 0 ] || git show --color=always --format=fuller \\$1 -- \"${show_filter[@]}\" | delta --line-numbers; \
}; \
f {}\" \
--preview-window=right:60%; \
}; \
gfzf \
"
That alias get the git log history, and then passes the content to ab fzf filter window. Cmon bro, it;s time to learn Bash
Autocomplete
Now, you know that fzf can filter files. That’s meaning we can filter through things like process and ssh host, because.. Unix is a file!!. Here are some of the options you can use by default:
1
2
3
4
5
6
7
8
9
10
# /home/user/.gitignore
kill -9 <TAB>
ssh <TAB>
telnet <TAB>
unset <TAB>
eport <TAB>
unalias <TAB>
If you are unemployed, you can use something like fzf-tab
Preview Files
Another cool thing to try out is the preview feature of the fzf. It allows you to preview files before you open them. To access that magic world, you need to pass –preview- to fzf. Let’s try:
The command i type is:
1
2
# bat btw
fzf --preview 'bat --style=numbers --color=always --line-range :200 {}'
Of course, you can integrate that feature with things like Vim, Telescope and Harppon and have a nice search string/file workflow
Conclusion
I hope that article gives you some insights about the tools behind the IDEs and Vim/Emacs distribution workflow we have today. I will give some useful alias in the footnotes. Goodbye.
1
2
3
4
5
6
7
# bash | zsh
alias sd="cd ~ && cd \$(find * -type d | fzf)"
alias gbr="git browse"
# fish
alias sd "cd ~ && cd (find * -type d | fzf)"
alias gbr "git browse"