4端末用のFZFトリック


ようこそ、あなたはおそらくコマンドラインのための最良のツールを使用して興味を持っている- FZF.FZFはファジィ探索のためのコマンドラインツールである.さて、最初は、魅力的でないかもしれません.しかし、私と一緒にベアー、それは非常にセクシーであり、それはあなたの人生を簡単にあなたのシェルを飛躍する.
あなたがターミナルの中にいるときはいつでも、あなたはあなたのそでを保つことができる4つのトリックを準備しました.しかし最初に、FZFをどのように設定できるかを調べましょう.

写真でDevon Janse van Rensburg on Unsplash

Getting started

If you haven’t already, you can install fzf using Git:

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

Or, if you are on macOS and want to skip all the fuss, you can use the following command:

brew install fzf

# To install useful key bindings and fuzzy completion:
$(brew --prefix)/opt/fzf/install
If you’re still having trouble, try out the fzf docs about installing .

Mic Check

After installing, let’s go and try the basic fzf functionality. You can try to run fzf inside your terminal. You should see something like this:


ファジーファインダーは、あなたの端末のウィンドウを介して起動し、任意のファイルを検索することができます.クールなことは、fzfファイルをインデックスとして入力する場合です.あなたのディレクトリがファイルとdirsに関して多くの複雑さを持っていないならば、あなたは下にインデックスを通知しません.

1. Fuzzyily Change Directories

The first trick today is using fzf to quickly cd into a directory. You can press ALT + c ( OPTION + c on macOS), and you will get an interactive prompt. Take a look below:


それは、あなたが今すぐにいるものの中にあるすべてのディレクトリの内容をインデクシングし始めます.あなたは、必要なディレクトリの入力を開始することができます矢印キーで必要なものを選択し、Enterキーを押してそれを取得します.便利、右?

2. Swoosh Through History

The second trick and one of my favorites, to be honest, is the ability to search through the command history with fzf. To get started, press CTRL + r in your terminal. Then, search for a command you typed previously. You should see something like this:


あなたはすぐにいくつかの時間前に入力コマンドをジャンプすることができます.私はそれが日常的に非常に便利見つける.

3. Autocomplete Anything

You can utilize fzf almost anywhere in your terminal. One of the default features is to allow you to find the process to kill quickly. You can try it out by typing kill -9 and pressing TAB . If that doesn’t work, type kill -9 ** and TAB . You can now search for the process or multiple processes you want to end.

You can also quickly search through hostnames to SSH. To do this, type ssh ** and press TAB . You will get the interactive search to find the wanted hostname.

To sum up, here are some of the options you can use by default:

kill -9 **<TAB>

ssh **<TAB>
telnet **<TAB>

unset **<TAB>
export **<TAB>
unalias **<TAB>

If you get tired of typing ** , you can change the fzf trigger by setting FZF_COMPLETION_TRIGGER variable. For example:

# Use ~~ as the trigger sequence instead of the default **
export FZF_COMPLETION_TRIGGER='~~'
If you want to take this to the next step, try out the fzf-tab プラグイン.私はZSHとああ私のZSHを使用しているので、それは非常に簡単に設定することでした.基本的にプラグインはFZFをデフォルトのタブ補完に差し込みます.私はあなたがそれを試してお勧めします.

4. Preview Files Before Selecting Them

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 it, you need to pass --preview- to fzf. Let’s see it in action:


私が呼ぶコマンドは
fzf --preview 'bat --style=numbers --color=always --line-range :500 {}'
しかし、あなたはこれをすべてカスタマイズできます.私のコマンドはbat , the cat クローンを見つけることができますGitHub . また、シンプルで行くことができますcat コマンド
fzf --preview 'cat {}'
または、これを少しプッシュする場合は、特定のコマンドを完了すると、デフォルトのプレビューオプションを設定することができます.ディレクトリやファイルのツリービューを表示できます.

あなたは同じようなものを得ることができますtree . 私はMacOSでそれをインストールしなければならなかったbrew install tree . それから、私は_fzf_comprun シェル初期化にロードする関数.bashrc or .zshrc ). 以下のようになります.
_fzf_comprun() {
  local command=$1
  shift

  case "$command" in
    cd) fzf "$@" --preview 'tree -C {} | head -200' ;;
    *) fzf "$@" ;;
  esac
}
他のケースでプレビューを表示するために別のコマンドが欲しいかもしれないので、あなたが望むならば、より多くのコマンドでより多くのケースを加えることができます.例えば、cat and bat ファイルのためにうまく動作しますが、sshにディレクトリやホスト名をプレビューすることはできません.

5. BONUS: Integrate with Vim

If you’re already using Vim (or you’re planning), I must tell you that fzf fits with Vim perfectly. The fuzzy search, preview windows, and customization options are so lovely you’d going to wish to have it in every other situation.

If you’re interested in setting up Vim and fzf, check out my blog post about improving Vim workflow with fzf .

Sum Up

fzf is a great tool to have in your toolbelt, especially if you’re dwelling inside the terminal. I hope this blog post taught you something new or inspired you to try some of these tricks.

If you liked what you read, consider subscribing to my newsletter 新しいポストが来るとき、更新されるようになってください.また、あなたは、以下のようにTwitterであなたの友人とこのようにコメントしたり、共有することができます
次の1つで、あなたをキャッチ.