DEV Community

Sam Chen
Sam Chen

Posted on

Here is a tutorial post designed for **dev.to**, focusing on a **Tool Discovery**.

It’s written to be practical, opinionated, and immediately useful for developers who live in the terminal.


Title: Stop Grepping and Start Fuzzily Finding: The bat + fzf Workflow That Changed My Debugging Life

Tags: tooling, terminal, productivity, vim, devops

Cover Image: A screenshot of a terminal showing a fuzzy search menu with syntax-highlighted code previews (or a simple meme of a confused developer vs. a happy one).


I used to be a "grep and scroll" developer. You know the type. I’d run a massive grep -r "error" . on a 10,000-file monorepo, watch the terminal flood, and then spend 5 minutes hunting for the actual line number I needed.

It was slow. It was messy. I was doing it wrong.

Then I discovered a combination of two tools that felt like putting on glasses for the first time. They aren't new, but if you haven't paired them up, you are missing out on the single best developer experience in the terminal.

The Duo: bat (a cat clone with wings) + fzf (a general-purpose fuzzy finder).

This isn’t just about looking at files. This is about searching, previewing, and opening files faster than your IDE can blink.

The Tools in Brief

1. bat (The Cat Upgrade)

Think of cat, but with syntax highlighting, line numbers, git modifications, and automatic paging for long files.

  • Why it matters: When you read code in the terminal, you shouldn't feel like you are reading binary. bat makes logs and scripts instantly readable.

2. fzf (The Fuzzy Finder)

This is the magic. fzf is a command-line filter. You pipe data to it, it opens an interactive search box, and you type a few letters to find what you need.

  • Why it matters: It turns blind searching into visual selection.

The Killer Combo: The "Open Anything" Function

Here is the script that lives in my .zshrc (or .bashrc). I call it fe (fuzzy edit).

# Fuzzy find a file, preview it with bat, and open it with vim (or code)
fe() {
  local file
  file=$(fzf --preview='bat --style=numbers --color=always {}' --preview-window=right:60%)
  if [ -n "$file" ]; then
    $EDITOR "$file"
  fi
}
Enter fullscreen mode Exit fullscreen mode

What this does:

  1. It scans your current directory (recursively).
  2. It opens an interactive fuzzy search.
  3. The Magic: In the right pane, it shows a live preview of the file using bat.
  4. You press Enter and it opens that file in your editor.

Video/GIF Suggestion: (Imagine a quick 10-second loop showing typing fe, typing "auth", seeing live previews of different auth.js files, selecting one, and it opening in VSCode)

Why This Specific Setup Changed My Workflow

I used to open files by tab-completing long paths (vim src/components/User/UserProfile/helpers/...). Now I just type fe and then type the content I remember.

Real-World Example:
You are debugging a bug in a file called paymentHandler.ts, but you don't know where it is. You just remember it had a function called validateCard.

The Old Way:
grep -r "validateCard" . -> find the path -> copy it -> vim /path/to/file.ts

The New Way:
fe -> Type "valCard" -> See the preview of the function instantly -> Enter.

I save about 20-30 seconds every single time I look for a file. Over a day, that's an hour saved just thinking about a path.

Installation & Pro Tips

Installation (MacOS):

brew install fzf bat

# To set fzf as a general CTRL+R (history search) and CTRL+T (file search):
$(brew --prefix)/opt/fzf/install
Enter fullscreen mode Exit fullscreen mode

Installation (Linux):

sudo apt install bat fzf
# Note: On Ubuntu, 'bat' might be installed as 'batcat' due to a naming conflict.
# If so, alias it:
alias bat='batcat'
Enter fullscreen mode Exit fullscreen mode

Pro Tips to take it to Level 10:

  1. Search File Contents (Not just names):
    Use Ripgrep (rg) as an fzf source to search inside files.

    # Fuzzy search *content* of files in the project
    fe() {
      local file
      file=$(rg --line-number --color=always "" |
        fzf --delimiter : --preview='bat --color=always --highlight-line {2} {1}' --preview-window=right:60%)
      # ...logic to open the file at the specific line
    }
    

    This is incredibly powerful for finding variable definitions or error strings.

  2. Git Log Viewer:
    Preview git commits before checking them out.

    git log --oneline | fzf --preview='git show {} --color=always' | cut -d' ' -f1 | xargs git checkout
    

The Verdict

You can keep clicking around in VS Code’s file tree. I’ll be here, typing three letters (fe), fuzzy searching, and already editing the file while you are still scrolling.

Have you tried fzf or bat? What is your favorite hidden gem terminal tool? Let me know in the comments.

Top comments (0)