fzf命令行使用
by Alexey Samoshkin
通过阿列克谢·萨莫什金(Alexey Samoshkin)
In this tutorial, I’ll help you take your command line habits to a next level with fzf
. Start searching files like a pro. Learn less known features, like changing directory, searching through a command history, looking up the host name to SSH into, killing a process, having instant file’s preview with syntax highlighting, and more…
在本教程中,我将帮助您使用fzf
将命令行习惯提升到一个新的水平。 开始像专业人士一样搜索文件。 了解鲜为人知的功能,例如更改目录,搜索命令历史记录,查找要用于SSH的主机名,终止进程,使用语法高亮显示即时文件的预览等等。
Today I’m going to tell you about a tool that can change your tech life into “before” and “after” parts. Trust me — it’s not a hyperbole. Meet fzf, which stands for fuzzy finder. As the definition claims, it’s a general purpose command-line fuzzy finder. That doesn’t sound very descriptive and attractive for those who are hearing about it for the first time. But it’s a very popular project ranked at around 21,000 stars on the Github. So it’s time to clear the fog and get a deeper insight.
今天,我将向您介绍一种工具,该工具可以将您的技术生涯变成“之前”和“之后”部分。 相信我-这不是夸张。 认识fzf ,它代表模糊查找器。 顾名思义, 它是一种通用的命令行模糊查找器 。 对于那些第一次听说它的人来说,这听起来并不具有描述性和吸引力。 但这是一个非常受欢迎的项目,在Github上排名约21,000星。 因此,现在该清除雾气并获得更深刻的见解了。
This post accompanies my recent screencast on the topic. So, if you’re a person who learns by watching, check it out. Otherwise, check it out too ?, because tools like fzf
are best introduced with a live demo rather than tons of text.
这篇文章伴随着我最近关于该主题的截屏视频 。 因此,如果您是一个通过观看学习的人,请查看一下。 否则,也要检查一下吗,因为最好将f zf
类的工具与实时演示结合使用,而不要大量文本介绍。
As the fzf
project page doesn’t have any demo video so far, I’ve called this post “missing demo found”. But now, this video has already been pulled into the fzf
repo and has become a part of the project’s readme.
由于fzf
项目页面到目前为止没有任何演示视频,因此我将这篇文章称为“找不到演示”。 但是现在,该视频已经被拉到fzf
,并已成为该项目自述文件的一部分。
People who are used to a command line environment are likely familiar with the concept of Unix filters. This is when several independent utilities are composed together into a pipeline to produce the desired output step by step. For instance, this pipeline produces a list of strings:
习惯命令行环境的人可能熟悉Unix过滤器的概念。 在这种情况下,几个独立的实用程序一起组成了一个管道,以逐步生成所需的输出。 例如,此管道生成字符串列表:
$ yes | head -10 | awk '{ print NR, NR % 2 == 0 ? "even" : "odd" }'
1 odd2 even3 odd4 even5 odd6 even7 odd8 even9 odd10 even
Each program acts as a filter. In simple terms, fzf
is just another Unix filter. It reads lines from stdin
, launches an interactive finder dialogue, and finally writes selected items to stdout
. The key point and difference from tools like GNU find
, is its interactive finder dialogue that filters items instantly as you type.
每个程序都充当过滤器。 简单来说, fzf
只是另一个Unix过滤器。 它从stdin
读取行,启动交互式finder对话框,最后将选定的项目写入stdout
。 与GNU find
类的工具的关键点和不同之处在于,它的交互式finder对话框可在您键入时立即过滤项目。
It might not sound too practical so far, but the primary use case for fzf
is to search for files on the command line. With fuzzy matching and instant feedback, you’re only few characters away from finding the right file no matter how deeply it’s lost in the directory hierarchy. No need to jump back to your file manager, traverse the directory hierarchy, copy a file’s path and paste it back to the shell. Compare “file manager” vs “fzf” workflows below.
到目前为止,听起来可能不太实用,但是fzf
的主要用例是在命令行上搜索文件。 借助模糊匹配和即时反馈,无论在目录层次结构中丢失的文件有多深,您只需几个字符即可找到正确的文件。 无需跳回到文件管理器,遍历目录层次结构,复制文件的路径并将其粘贴回外壳。 比较下面的“文件管理器”和“ fzf”工作流程。
fzf
supports fuzzy matching so you can just type several characters in a row and it will match lines with those characters scattered across the string. Alternatively, prefix a search term with a single quote, like 'string
, to opt for exact matches only, or run as fzf --exact
.
fzf
支持模糊匹配,因此您可以连续输入几个字符,它将匹配那些分散在字符串中的字符的行。 或者,在搜索词前加单引号,例如'string
,仅选择完全匹配,或以fzf --exact
运行。
It does not support regular expressions or glob patterns, so *.sh
pattern would not work. But keep it simple — productivity and speed is your goal today. You don’t have spare time to compose and type correct regular expressions. Instead just type several words, or even parts of the words, delimited by a space, and that would cover >90% of the use cases. For the remaining 10%, us
e ^ an
d $ to match the start and end of the string respectively, and us
e ! to negate matching.
它不支持正则表达式或glob模式,因此*.sh
模式将不起作用。 但请保持简单-生产力和速度是您今天的目标。 您没有时间来编写和键入正确的正则表达式。 取而代之的是只键入几个单词,甚至单词的一部分,以一个空格分隔,这将覆盖超过90%的用例。 对于剩余的10%,u s
e ^ a n
d $分别与字符串的开头和结尾匹配,并且u s
e! 否定匹配。
Printing selected files to a command line is not super useful, so usually search is combined with some further action. For instance, you might open it with Vim, or pipe selected items to the next program.
将选择的文件打印到命令行不是超级有用,因此通常将搜索与其他操作结合在一起。 例如,您可以使用Vim打开它,或将选定的项目通过管道传送到下一个程序。
# Open file in a Vimvim -o `fzf`
# Print info for each selected filefzf | xargs ls -l
To make it more convenient, fuzzy completion can be triggered if the word before the cursor ends with the trigger sequence which is by default **
. For example, type vim ~/path/**
and press TAB
. Voilà, fzf steps in!
为了更加方便,如果光标之前的单词以触发序列(默认为**
结束,则可以触发模糊补全。 例如,输入vim ~/path/**
并按TAB
。 Voilà,fzf介入!
**
sequence triggers fzf
finder and it resembles *
which is for native shell expansion. At some point, you might even forget about fzf
’s presence and get the impression it’s a native shell feature.
**
序列触发fzf
finder,它类似于*
,用于本地shell扩展。 在某些时候,您甚至可能会忘记fzf
的存在,而给人的印象是它是本机shell功能。
Format is as follows, where FUZZY_PATTERN
is optional.
格式如下 ,其中FUZZY_PATTERN
是可选的。
COMMAND [DIRECTORY/][FUZZY_PATTERN]**<TAB>
If you don’t like the **
sequence, you can use keyboard shortcuts. CTRL+T
triggers fzf
and pastes the selected file onto the command line, whereas ALT+C
changes into the selected directory.
如果您不喜欢**
序列,则可以使用键盘快捷键。 CTRL+T
触发fzf
并将所选文件粘贴到命令行,而ALT+C
更改到所选目录。
Ok, enough about searching files. Let’s talk about other useful applications. Changing your working directory is such a common operation. But nevertheless I’m always stuck a bit trying to recall and type the right directory’s path, making several mistakes along the way. It slows down my tempo. Even zsh completions do not compensate for it. But with fzf
, changing directories is a breeze, no matter how deep and far it is. Just type cd **
and you’re almost there.
好的,关于搜索文件就足够了。 让我们谈谈其他有用的应用程序。 更改工作目录是一种常见的操作。 但是,尽管如此,我总是想尝试重新调用并键入正确目录的路径,这在过程中会犯一些错误。 它减慢了我的节奏。 即使zsh补全也无法弥补它。 但是,使用fzf
,更改目录无论深度和远都非常容易。 只需输入cd **
,您就快到了。
Notice that while fzf is indexing your directory tree, you can start searching right away. Changing directories is my favourite use case, and fuzzy matching really excels here. It requires from you the same effort to change a directory whether it’s one level deep or ten levels deep.
请注意,当fzf正在索引目录树时,您可以立即开始搜索。 更改目录是我最喜欢的用例,模糊匹配在这里确实很出色。 无论是深一层还是十层,都需要您付出同样的努力来更改目录。
ALT+C
shortcut is an alternate way to trigger fzf
in a change directory mode.
ALT+C
快捷键是在更改目录模式下触发fzf
的另一种方法。
You might be using Ctrl+R
keyboard shortcut to search through your command history. Great, but how about supercharging it with fuzzy finder? Look and compare.
您可能正在使用Ctrl+R
键盘快捷键来搜索命令历史记录。 很好,但是如何使用模糊查找器对其进行增压? 看比较。
It pastes the selected item on the command line, so you can tweak it further.
它将选定的项目粘贴在命令行上,因此您可以对其进行进一步的调整。
If you’re a backend developer and working with a number of remote servers, you might appreciate ssh+fzf
combo. Use the same double star trigger sequence and type ssh **
. It pulls recently used IP addresses and host names from your ~/.ssh/config
and brings up an interactive finder.
如果您是后端开发人员并且使用许多远程服务器,则可能会喜欢ssh+fzf
组合。 使用相同的双星触发序列并键入ssh **
。 它从~/.ssh/config
提取最近使用的IP地址和主机名,并显示一个交互式查找器。
Sometimes we want to send a signal to a process, but first we need to obtain its PID by name. Usually one uses pgrep <process_na
me> to resolve PID followed by a kill <pro
cess_pid> referring that P
ID. With fzf you can combine both steps. Typ
e kill <TAB> and fzf steps in listing all your processes. No need to switch to a dedicated process monitor any more, like “Activity Monitor” on Mac.
有时我们想向进程发送信号,但是首先我们需要按名称获取其PID。 通常情况下,使用pgrep <process_na
me>解析PID,然后by a kill <pro
process_pid>引用t P
ID。 使用fzf,您可以将两个steps. Typ
结合在一起steps. Typ
steps. Typ
è杀<TAB>并列出你所有的过程FZF步骤。 无需再切换到专用的过程监视器,例如Mac上的“活动监视器”。
Suppose you’re searching for files, but sometimes the file name itself does not tell you enough. So you might want to peek into a file’s content for a moment to make a decision. fzf
covers you here as well.
假设您正在搜索文件,但是有时文件名本身并不能告诉您足够的信息。 因此,您可能需要暂时查看文件的内容以做出决定。 fzf
在这里也涵盖了您。
By default I have preview window off, but I toggle it on when I want to peek into the files. Plus, I’ve enhanced it with a coloured output and syntax highlighting using bat as a preview command.
默认情况下,我关闭了预览窗口,但是当我想浏览文件时会打开它。 另外,我还用彩色输出和使用bat作为预览命令的语法突出显示功能对其进行了增强。
There are two main ways to customize the stock version of fzf
:
自定义fzf
原始版本有两种主要方法:
Change the behaviour of the finder dialogue (preview window, keyboard shortcuts, dimensions, custom actions, etc). See FZF_DEFAULT_OPTS
environment variable.
更改查找程序对话框的行为(预览窗口,键盘快捷键,尺寸,自定义操作等)。 请参阅FZF_DEFAULT_OPTS
环境变量。
Change the underlying find backend. By default it uses GNU find
utility, but you can switch to the more advanced tools like fd or ripgrep. First, it’s faster than the stock find
utility. Second, those tools respect .gitignore
rules, so you don’t get node_modules
or .git
files in your search results. You can also use git ls-tree
to list files when you’re in a Git repo. See FZF_DEFAULT_COMMAND
environment variable.
更改基础的查找后端。 默认情况下,它使用GNU find
实用程序,但是您可以切换到fd或ripgrep之类的更高级的工具。 首先,它比股票find
实用程序要快。 其次,这些工具遵守.gitignore
规则,因此您不会在搜索结果中获得node_modules
或.git
文件。 在Git存储库中时, 还可以使用 git ls-tree
列出文件。 请参阅FZF_DEFAULT_COMMAND
环境变量。
Configuration is done through the environment variables. Here is the snippet from my setup. Not sure if it would work as is when copy-n-pasted, most likely there’re some other missing dependencies. But this is just to give you an idea of how the configuration looks.
通过环境变量完成配置。 这是我的设置中的代码段。 不知道复制粘贴时它是否可以正常工作,很可能还有其他一些缺少的依赖项。 但这只是为了让您了解配置的外观。
So far we’ve seen command-line usage only. But fzf
is also shipped as a Vim plugin. And trust me, if you are a Vimmer, its worth adding to your vimrc
. Vim usage is out of the scope of this post, otherwise no one would read it ?. But I talk about it thoroughly in the second part of the my video, also linked at the beginning of the post.
到目前为止,我们仅看到命令行用法。 但是fzf
也作为Vim插件提供 。 如果您是Vimmer,请相信我,它值得添加到您的vimrc
。 Vim的使用超出了本文的范围,否则没人会读它吗? 但是,我在视频的第二部分(在帖子开头也进行了链接)中进行了详尽的讨论。
If you’re impatient, here is a small excerpt to draw your attention. Use :grep
command for project-wide text search, open matches in fzf
window in a full screen mode, further filter matches within fzf
, and jump to the selected match. And don’t forget about instant file preview at the precise line position. That’s awesome, isn’t it?
如果您不耐烦,请参考以下摘录。 使用:grep
命令进行项目范围的文本搜索,以全屏模式在fzf
窗口中打开匹配fzf
,进一步在fzf
内过滤匹配fzf
,然后跳转到选定的匹配项。 并且不要忘记在精确的行位置进行即时文件预览。 太棒了,不是吗?
I hope you’re impressed of how super awesome fzf is. Chances are you already willing to install and give it a try.
我希望您对fzf的超赞印象深刻。 您可能已经愿意安装并尝试一下。
Keep in mind that fzf
is not only for file searching, although it’s the primary feature of it. As you can see, “search + action” is a general principle, but it’s abstract and powerful enough to work with any kind of list: files, directories, processes, host names, etc. Remember at the beginning of this post — fzf is just a Unix filter. Unlock your imagination, feed it any list, and tailor it for your own needs. You might take some inspiration from a wealth of examples.
请记住, fzf
不仅是文件搜索的主要功能,而且它不仅用于文件搜索。 如您所见,“ search + action ”是一个通用原则,但它抽象且强大,足以与任何类型的列表一起使用:文件,目录,进程,主机名等。请记住本文开头— fzf是只是Unix过滤器 。 释放您的想象力,提供任何清单,然后根据自己的需要进行调整。 您可能会从大量示例中得到一些启发。
BTW, appreciate how superior Unix philosophy is. fzf
is a great example of a program that adheres to these principles.
顺便说一句,请欣赏Unix哲学的优越性。 fzf
是遵守这些原则的程序的一个很好的例子。
Stick with those principles in your software development career. Knowing fundamental principles that stand behind the rapid birth and death of multiple tools, languages, and frameworks out there is what differentiates professional developers from amateurs.
在您的软件开发生涯中坚持这些原则。 了解多种工具,语言和框架Swift诞生和消失背后的基本原理,是使专业开发人员与业余开发人员区别开来的原因。
翻译自: https://www.freecodecamp.org/news/fzf-a-command-line-fuzzy-finder-missing-demo-a7de312403ff/
fzf命令行使用