DEV Community

hikerpig
hikerpig

Posted on • Originally published at hikerpig.cn on

万物皆可 fzf

fzf 是一个速度和适用范围都极好的工具,借助 UNIX 管道,可以接入日常几乎所有终端 CLI 操作中,对所有的 list 都能有美好的可交互式模糊过滤体验。

It's an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

一些技巧

使用 -m 参数支持多选

cat $(fzf -m)
Enter fullscreen mode Exit fullscreen mode

然后就可以实用 tab 或者 shift+tab 实现多选。在使用 cat/rm 等命令时很好用。

使用 ** 作为 trigger

unset **<Tab>
unalias **<Tab>
Enter fullscreen mode Exit fullscreen mode

善用 --preview

交互式地选择 git branch。

# git interactive checkout
gcb() {
  result=$(git branch -a --color=always | grep -v '/HEAD\s' | sort |
    fzf --height 50% --border --ansi --tac --preview-window right:70% \
      --preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
    sed 's/^..//' | cut -d' ' -f1)

  if [[$result != ""]]; then
    if [[$result == remotes/*]]; then
      git checkout --track $(echo $result | sed 's#remotes/##')
    else
      git checkout "$result"
    fi
  fi
}
Enter fullscreen mode Exit fullscreen mode

实用脚本

fzf-tab 替换 zsh 的默认补全选择菜单

Aloxaf/fzf-tab ,如其自我介绍

Replace zsh's default completion selection menu with fzf!

这个脚本可以将 zsh 的默认 completion 结果转给 fzf 显示,让你在选择补全结果的时候获得 fzf 加持。

若使用 zimfw 安装,在 .zimrc 内添加:

zmodule Aloxaf/fzf-tab
Enter fullscreen mode Exit fullscreen mode

asciicast

fzf-fasd, 配合 fasd 可代替 z

wookayin/fzf-fasd: 🌸 fzf + fasd integration ,实现了类似 z 的效果, z<Tab> 后使用 fzf 选择最近进入过的目录。

其他零零散散

bindkey | fzf # 搜索当前 shell 里所有绑定了的快捷键

fzf --preview "bat {} --color=always" # 快速预览当前以及子目录下文件内容,不在乎颜色的话 bat 可以换成 cat

cd $(find . -type d | fzf) # 可选择进入某个深层子目录
cd $(fd --type directory | fzf) # 与上面类似,不过使用更便捷的 fd
Enter fullscreen mode Exit fullscreen mode

参考文章

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay