Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Customize FZF's CTRL + T Binding with FZF_CTRL_T_OPTS to Preview Files

customize-fzf-ctrl-t-binding-to-preview-files.jpg

This lets us quickly preview files in less or whatever tool you prefer.

Quick Jump:

A few years ago I made a post on how to preview files with FZF where we set up a pf (preview file) alias to call FZF with a few flags and I still use that method today.

In this post we’ll re-use the same preview flags to also work with FZF’s built-in CTRL + T key bind which lets you fuzzy match files in a directory and if you select a file it outputs the match into your prompt without executing it.

But, if you hit escape instead of enter to select the file then nothing is added to your prompt which makes CTRL + T a quick way to preview files as an alternative to the pf alias.

We can do all of this by setting FZF_CTRL_T_OPTS which is provided by FZF. I’ve added it to my .zshrc file so it’s always available:

# These flags are explained in the post about previewing files with FZF.
export FZF_CTRL_T_OPTS="--preview='less {}' --height=100% --bind shift-up:preview-page-up,shift-down:preview-page-down"

Now we can simplify the pf alias to use alias pf="fzf "${FZF_CTRL_T_OPTS}". Here’s the commit in my dotfiles which makes that change. I did make a last minute adjustment to add --height=100% too which isn’t included in the linked commit.

# Demo

Timestamps

  • 0:09 – Quick recap on previewing files
  • 0:26 – CTRL + T
  • 1:05 – Figuring out how to customize CTRL + T
  • 1:41 – Going over how to implement it
  • 3:20 – Making the CTRL + T preview height taller

Do you use FZF to preview files? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments