Using FZF to Preview Text Files on the Command Line and within Vim
Here's a video going over how to set up a Shell alias to preview files. It also works out of the box with Vim using the fzf.vim plugin.
FZF is extremely useful on the command line to reverse search your history with CTRL+r. I’ve made a video about that a few years ago, but FZF can also fuzzy find files using CTRL+t.
You can take this one step further to have FZF recursively find all files in a
directory and then you can quickly preview them with paging support . You can
also set up an alias to make it easy to access and even get syntax highlighting
if you use the bat
tool.
# Demo
Alias
alias pf="fzf --preview='less {}' --bind shift-up:preview-page-up,shift-down:preview-page-down"
Timestamps
- 0:08 – I’ve made videos in the past about reverse searching your history with FZF
- 0:18 – Running the FZF command on its own
- 0:37 – Using the built in CTRL+t bind with FZF to fuzzy search files on the command line
- 0:57 – Previewing files on the command line with FZF using less
- 1:59 – Using different commands to preview files, such as head for the first 30 lines
- 2:24 – Using bat to get syntax highlighting in your previewed files
- 4:06 – Creating custom FZF binds to page through results and setting up a Shell alias
- 5:59 – You can preview files with the FZF Vim plugin by default too
- 7:08 – Recap and any questions?
Reference Links
- https://nickjanetakis.com/blog/fuzzy-search-your-bash-history-in-style-with-fzf
- https://github.com/junegunn/fzf
- https://changelog.com/podcast/451
- https://github.com/sharkdp/bat
- pf alias in my dotfiles
- https://github.com/junegunn/fzf.vim
What types of files are you going to preview using this method? Let me know below.