目次

「ソフトウェア」一覧に戻る

コマンド結果をフィルタリングするfzf



fzfとは

コマンド結果をフィルタリングしてくれます。
previewできるのが特徴です。

インストール

Ubuntu

sudo apt install fzf


設定

fzfのグローバル設定

export FZF_DEFAULT_OPTS="
  --height=60%
  --layout=reverse
  --border=rounded
  --info=inline-right
  --cycle
"

export FZF_DEFAULT_OPTS="
  --height=60%
  --layout=reverse
  --border=rounded
  --info=inline-right
  --prompt='❯ '
  --pointer='▶'
  --marker='✓'
  --cycle
  --scrollbar='│'
"

export FZF_DEFAULT_OPTS="--no-sort --exact --cycle --multi --ansi --reverse --border --sync --bind=ctrl-t:toggle"


export FZF_DEFAULT_OPTS="--no-sort --exact --cycle --multi --ansi --reverse --border --sync --bind=ctrl-t:toggle --bind=ctrl-k:kill-line --bind=?:toggle-preview --bind=down:preview-down --bind=up:preview-up"
Windows
##$PROFLEファイルの確認
$PROFLE

##$PROFILEファイルがなかったら、作成
if (-not (Test-Path $PROFILE)) {
    New-Item -ItemType File -Path $PROFILE -Force
}


##設定の修正
notepad $PROFILE


$env:FZF_DEFAULT_OPTS = @"
--height=60%
--layout=reverse
--border=rounded
--info=inline-right
--prompt="> "
--pointer=">"
--marker="*"
--cycle
--scrollbar="|"
"@

##設定の読み込み
. $PROFILE


利用例

ファイルを探してvimで開く

vim $(fzf)

vim $(find .|fzf)

ブックマークからディレクトリ移動

~/.fzf_bookmarks というテキストファイルにお気に入りのパス(/path/to/dir)を改行区切りで保存しておき、以下の関数を .zshrc に記述します。

function cdmark() {
    local dir=$(cat ~/.fzf_bookmarks | fzf)
    [ -n "$dir" ] && cd "$dir"
}

ブックマークからディレクトリ移動(PowerShell)

function cdmark {
    $dir = Get-Content "$HOME\.fzf_bookmarks" |
        Where-Object { $_.Trim() -ne "" } |
        fzf --prompt="Directory> "

    if (-not $dir) {
        return
    }

    if (Test-Path -LiteralPath $dir -PathType Container) {
        Set-Location -LiteralPath $dir
    }
    else {
        Write-Warning "ディレクトリが存在しません: $dir"
    }
}
function addmark {
    $bookmarkFile = "$HOME\.fzf_bookmarks"
    $path = (Get-Location).Path
    $bookmarks = @()

    if (Test-Path -LiteralPath $bookmarkFile) {
        $bookmarks = Get-Content -LiteralPath $bookmarkFile
    }

    if ($bookmarks -contains $path) {
        Write-Host "すでに登録されています: $path"
        return
    }

    Add-Content -LiteralPath $bookmarkFile -Value $path
    Write-Host "ブックマークへ追加しました: $path"
}

関数をPowerShellプロファイルへ保存

# プロファイルの場所を確認
$PROFILE

# プロファイルを開きます。
notepad $PROFILE

# ファイルが存在しない場合は、先に作成します。
New-Item -ItemType File -Path $PROFILE -Force
notepad $PROFILE

# 関数を設定

#現在のPowerShellですぐ反映する
. $PROFILE

zoxide + fzf を使う(移動履歴の自動ブックマーク)

導入方法:Homebrew 等で zoxide と fzf をインストールします。

使い方:
ターミナルで zi コマンドを叩くだけ。
過去の移動履歴や登録したお気に入りを fzf で絞り込んで即座に cd できます。

ssh-fzf

pecoからfzfに移行した | tsub's blog

function ssh-fzf () {
  local selected_host=$(grep "Host " ~/.ssh/config | grep -v '*' | cut -b 6- | fzf --query "$LBUFFER")

  if [ -n "$selected_host" ]; then
    BUFFER="ssh ${selected_host}"
    zle accept-line
  fi
  zle reset-prompt
}

zle -N ssh-fzf
bindkey '^\' ssh-fzf

history-fzf

pecoからfzfに移行した | tsub's blog

function history-fzf() {
  local tac

  if which tac > /dev/null; then
    tac="tac"
  else
    tac="tail -r"
  fi

  BUFFER=$(history -n 1 | eval $tac | fzf --query "$LBUFFER")
  CURSOR=$#BUFFER

  zle reset-prompt
}

zle -N history-fzf
bindkey '^r' history-fzf


fzfを便利にするソフト

ripgrep

export FZF_DEFAULT_COMMAND='rg --files'
less $(rg .|fzf --preview="cat {1}")   #要修正
sudo apt install ripgrep


fzf-tmux


fzf.vim


参考







「ソフトウェア」一覧に戻る