So you've installed `fzf`. Now what? (2023)
So you’ve installed fzf. Now what? (2023)
你已经安装了 fzf。接下来做什么?(2023)
Software engineers are, if not unique, then darn near unique in the ease with which we can create tools to improve our own professional lives; this however can come at a steep cost over time for people who constantly flit back and forth between different tools without investing the time to learn their own kit in depth. As someone with a healthy respect for the tacit knowledge of people better than me, I think a great 80/20 heuristic is “Learn the oldies first”: venerable Unix tools like cat, ls, cd, grep, and cut. (sed and awk, too, if you have the good fortune of landing yourself in an actual modern sysadmin role.)
软件工程师在利用工具改善职业生活方面有着得天独厚的优势;然而,对于那些在不同工具之间频繁切换,却又不愿花时间深入学习的人来说,这可能会带来高昂的长期成本。作为一个尊重他人隐性知识的人,我认为一个很好的 80/20 法则是“先学老牌工具”:比如 cat、ls、cd、grep 和 cut 等古老的 Unix 工具。(如果你有幸从事现代系统管理员的工作,sed 和 awk 也是必不可少的。)
But there are tools whose return on investment is so immediate, and whose value prop is so unique, that the 80/20 heuristic breaks down entirely for them. fzf is one of them. And it makes me sad to see so many people download it, run it as-is at the command line, and then just shake their heads and walk away, saying “I don’t get it”.
但有些工具的投资回报率非常直接,其价值主张也极其独特,以至于 80/20 法则在它们身上完全失效。fzf 就是其中之一。看到许多人下载它,在命令行直接运行,然后摇摇头走开说“我不明白这有什么用”,这让我感到很难过。
Here I want to change that. Pretend you live on a more-or-less standard Ubuntu box. You’ve just installed fzf using the standard install script – now what?
在这里,我想改变这种状况。假设你正在使用一台标准的 Ubuntu 系统,并且刚刚通过标准安装脚本安装了 fzf——接下来该做什么?
First try Ctrl+R…
首先试试 Ctrl+R……
In most terminals, Linux and Windows alike, Ctrl+R gives you backwards search for your commands. The reason you, like me, may not have heard about this until you had already been hacking away for ten flippin’ years at the shell is because the base version kind of sucks for 2 reasons:
- You need to give an exact match to get what you’re trying to remember.
- You get only one preview, so if you miss that exact match by even one character, you’re on a wild goose chase.
在大多数终端(无论是 Linux 还是 Windows)中,Ctrl+R 都可以让你反向搜索历史命令。你(像我一样)可能直到在 Shell 中折腾了十年才听说这个功能,原因在于基础版本实在太难用了,主要有两个原因:
- 你必须提供精确匹配才能找到你想找的内容。
- 你只能看到一个预览结果,所以如果你错了一个字符,就会陷入无休止的盲目搜索中。
fzf is a bit of a weird program because installing it actually overwrites a whole bunch of keyboard shortcuts, in the interest of making them better. Normally I would hate this. But…… This is a considerable improvement on the baseline.
fzf 是一个有点奇怪的程序,因为它在安装时会覆盖掉一大堆键盘快捷键,目的是为了让它们变得更好。通常我会讨厌这种做法,但……这确实比基础版本有了显著的提升。
(Do note that installing fzf via a package manager like apt may not give you this! There’s a reason I keep hinting that you should really use the install script!, even if you and I agree curl | bash is the work of Super Moloch!)
(请注意,通过 apt 等包管理器安装 fzf 可能不会获得这些功能!这就是我一直暗示你应该使用安装脚本的原因!即使你我都同意 curl | bash 这种做法简直是恶魔行径!)
… now try Alt+C
……现在试试 Alt+C
Let’s say you boot into an empty terminal. You’re trying to quickly find your nascent SaaS side hustle repos and cd to it - but it’s been weeks since you’ve been there, your actual full time job has been unusually fun and engaging… How do you find it?
假设你打开了一个新的终端。你试图快速找到你刚起步的 SaaS 副业仓库并进入该目录——但你已经几周没去过那里了,而且你的全职工作最近异常有趣……你该如何找到它?
Answer: With fzf. fzf rewrites Alt+C into a souped-up fuzzy-cd shortcut that lets you hop around very quickly when all you remember is the vague name of the directory in question.
答案是:使用 fzf。fzf 将 Alt+C 重写为一个增强版的模糊 cd 快捷键,当你只记得目录名称的大致拼写时,它能让你非常快速地跳转到目标位置。
The base fzf command
基础 fzf 命令
Okay, we’ve got the shortcuts out of the way. Honestly these two guys alone provide the majority of the value I get out of fzf - but let’s look at what the command, by itself, does. It fuzzy-finds file locations! Relative ones, at least, to your own directory. This… isn’t that useful, by itself.
好了,快捷键的部分说完了。老实说,光是这两个功能就提供了我从 fzf 中获得的大部分价值——但让我们看看这个命令本身能做什么。它能模糊查找文件路径!至少是相对于你当前目录的路径。这……单独使用时其实没那么有用。
But try vi $(fzf), and…!!! And you get a fuzzy-open-in-editor experience! (There’s nothing special about vi in this regard, btw. You can call it with emacs, nano, code, whatever floats your boat!)
但试试 vi $(fzf),然后……!!!你就能获得一种“模糊搜索并用编辑器打开”的体验!(顺便说一下,vi 在这方面没什么特别的。你可以用 emacs、nano、code 或者任何你喜欢的编辑器来调用它!)
vi $(find . '/' | fzf): For finding random config files
vi $(find . '/' | fzf):用于查找随机配置文件
The other day I was trying to hack together baby’s first live-reload with a Firefox extension, entr, and nginx. And I found myself asking: Where the heck is nginx.conf? I reviewed my options. I could:
- Use my half-remembered knowledge of the FHS to guess around, with trees and greps, or
- Just know and commit it to memory and feel superior to everyone else, or
- Just pipe find . ‘/’ to fzf and start searching.
前几天,我试图用 Firefox 扩展、entr 和 nginx 拼凑出一个简单的实时重载功能。我问自己:nginx.conf 到底在哪?我评估了我的选择:
- 用我半生不熟的 FHS(文件系统层级标准)知识,通过 tree 和 grep 到处猜测;
- 把它背下来,然后对别人产生优越感;
- 直接把
find . '/'的结果通过管道传给 fzf,然后开始搜索。
I like this clip a lot because it shows some of the subtle tradeoffs of using fzf, as well as one of the more advanced searching features - searching for conf$ will filter out any line that doesn’t end in conf. Notice that fzf temporarily wigs out when find hits a whole lot of “Permission denied” errors - but then recovers itself a few seconds later. Are those extra few seconds worth the tradeoff for being able to find config files in such a braindead manner? It is for me.
我很喜欢这个片段,因为它展示了使用 fzf 的一些微妙权衡,以及更高级的搜索功能之一——搜索 conf$ 将过滤掉任何不以 conf 结尾的行。请注意,当 find 遇到大量“权限被拒绝”错误时,fzf 会暂时卡住,但几秒钟后就会恢复。为了能以这种“无脑”的方式找到配置文件,多花这几秒钟值得吗?对我来说是值得的。
Even less friction: vi **<TAB>
更低的操作门槛:vi **<TAB>
About halfway between “overwrite a keyboard shortcut” and “use fzf as-is” is using two stars for fuzzy tab completion. Here’s using it to do something quite similar to vi $(fzf), as above: You do have to hit Enter one more time after you actually get the command, fair warning.
介于“覆盖键盘快捷键”和“直接使用 fzf”之间的方法,是使用两个星号进行模糊 Tab 补全。以下是使用它来完成类似于上面 vi $(fzf) 的操作:需要提醒的是,在获得命令后,你还需要多按一次回车键。
I’m not yet in the habit of using this all that often, for a few reasons. First, it doesn’t work with my real home shell for me for some reason, although on bash and zsh it’s perfectly fine. Second, my only real use case at home is as a replacement for $(fzf), and I just find explicitly calling the boy easier to remember.
我还没养成经常使用这个功能的习惯,原因有几个。首先,由于某种原因,它在我的主力 Shell 中无法工作,尽管在 bash 和 zsh 上它运行得很好。其次,我在家唯一的实际用途就是替代 $(fzf),而且我觉得显式地调用它更容易记忆。
mv $(fzf) $(fzf) - real amnesiac hours
mv $(fzf) $(fzf) - 真正的“失忆时刻”
For when you neither remember exactly what you’re moving, nor where you’re moving it to, but you remember the abstract concept of distance over time well enough to know it simply must be done, and something extremely specific about the nature of each item to be shunted. I am embarrassed to admit how often I reach for this when I’m trying to stick GIFs in my Github READMEs.
当你既不记得要移动什么文件,也不记得要把它移到哪里,但你清楚地知道这事儿必须得做,并且对要移动的每个项目的性质有极其具体的了解时,这个命令就派上用场了。我很不好意思承认,当我想把 GIF 插入到 Github README 中时,我有多频繁地使用这个技巧。