laitimes

How to quickly find files under the Linux terminal; History - FZF blur takes you to solve

author:A programmer who loves to fish

Prefix conditions

  • I'm used to using zsh + tmux as a terminal tool for both Mac and Linux, so let's talk about installing fzf in zsh mode and how to use it.

Introduce

How to quickly find files under the Linux terminal; History - FZF blur takes you to solve
  • It is an interactive Unix filter for the command line that can be used with any list; Files, command history, processes, hostnames, bookmarks, git commits, and more.
  • Key features:
    • Lightweight and dependent-free
    • Very fast
    • A comprehensive set of features
    • Flexible display pages
    • Support VIM/NEOVIM plug-ins, key bindings, autoprompts, and more

Install

  • fzf provides the source code, and if we only want a tool command without considering compatibility issues with VIM etc., then we can install an executable program directly Ubuntu : echo y | sudo apt-get install fzf Mac : echo y | sudo brew install fzf
  • As I said at the beginning, we need to be in a zsh environment and I'm a VIM controller, so the source code installation has to be operated.
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
           

Copy the code

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install

  • The installation process may involve access to the Internet, so it is best to install it over the wall.

Multi-system installation

Package Manager Linux Distribution Command
.APK Alpine Linux sudo apk add fzf
APT Debian 9+/Ubuntu 19.10+ sudo apt install fzf
Conda conda install -c conda-forge fzf
DNF Fedora sudo dnf install fzf
Nothing NixOS, etc. nix-env -iA nixpkgs.fzf
Pacman Arch Linux sudo pacman -S fzf
pkg FreeBSD pkg install fzf
pkgin NetBSD pkgin install fzf
pkg_add OpenBSD pkg_add fzf
Portage Gentoo emerge --ask app-shells/fzf
XBPS Void Linux sudo xbps-install -S fzf
Zypper openSUSE sudo zypper install fzf

fzf-tmux script

  • As the name suggests, he is known as an FZF script plugin used in a Tmux environment.
How to quickly find files under the Linux terminal; History - FZF blur takes you to solve
  • Looking at the list of current projects, there are only 10 of them, which have been tested to work with fzf and fzf-tmux in a tmux environment.
git branch | fzf-tmux -d 10
           

Copy the code

git branch | fzf-tmux -d 10

Command-line mode shortcut bindings

  • When we install it, we will see that we are setting our shortcuts, of course we are in zsh.
How to quickly find files under the Linux terminal; History - FZF blur takes you to solve
  • There are 3 shortcuts by default. Next, we use the next three shortcuts.

Ctrl + T

  • His role is to put our current folder into it
How to quickly find files under the Linux terminal; History - FZF blur takes you to solve
  • The main function of fzf is fuzzy search, so that we only need to enter key information to search. Here is a combination of GitHub - sharkdp/bat: A cat(1) clone with wings: You can complete a preview of the file. Just install it on GitHub, and then configure the variable FZF_CTRL_T_OPTS in our environment configuration file.
Set FZF_CTRL_T_COMMAND to override the default command
Set FZF_CTRL_T_OPTS to pass additional options to fzf
           
  • I'm installing bat here on Ubuntu, his installation is called batcat, you can follow the advice given on the official website to set up a soft connection for him, it is recommended not to use aliases, aliases will not take effect in some scenarios.
  • Configured in ~/.zshrc
export FZF_CTRL_T_OPTS="
  --preview 'batcat -n --color=always {}'
  --bind 'ctrl-/:change-preview-window(down|hidden|)'"
           
How to quickly find files under the Linux terminal; History - FZF blur takes you to solve
  • I don't know how to scroll the contents of the preview window with shortcut keys at the moment, I can only scroll with the mouse.

Ctrl + R

  • Render the history, paste the selected command into the command line h
How to quickly find files under the Linux terminal; History - FZF blur takes you to solve
  • This is my favorite shortcut. Because sometimes we suddenly want to execute the previous command, but it is too long to remember, although zsh has a plugin that hints through history, but it is also matched according to the beginning, and the blurring function of fzf itself is more flexible. That way I just need to know the keywords.
  • It should be noted here that pressing Ctrl+R once has different effects than pressing twice.
How to quickly find files under the Linux terminal; History - FZF blur takes you to solve
  • The first time we were able to find the + pattern, here my understanding is that the words have been matched as a whole. The second Ctrl+R is not - mode, it is matched by each letter, and the + mode should be more commonly used in the actual use case.
  • He also supports changing execution parameters by configuring environment variables
Set FZF_CTRL_R_OPTS to pass additional options to fzf           
  • The official gives a preview command for all the contents and the configuration of the copy command
export FZF_CTRL_R_OPTS="
  --preview 'echo {}' --preview-window up:3:hidden:wrap
  --bind 'ctrl-/:toggle-preview'
  --bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort'
  --color header:italic
  --header 'Press CTRL-Y to copy command into clipboard'"           
  • My Ubuntu uses xsel to achieve clipboard management, and here pbcopy can be replaced by the corresponding xsel command.
How to quickly find files under the Linux terminal; History - FZF blur takes you to solve

Alt + C

  • One is the file path, one is the history, and the other is the file directory. Because we need to constantly make path jumps through the terminal. So this operation is also very commonly used.
  • Similarly, we can customize the preview of the file directory, and the official preview is according to the tree structure
export FZF_ALT_C_OPTS="--preview 'tree -C {}'"           
How to quickly find files under the Linux terminal; History - FZF blur takes you to solve
  • Alt + C on my mac is occupied by yabai. So here I chose to change to Ctrl+E, which happens to be the shortcut key in Luarnvim to manipulate the file directory, and the shortcut key corresponds to the E key.

Author: zxhtom

Link: https://juejin.cn/post/7296324385623277622

Read on