fzf命令行使用

by Alexey Samoshkin

通过阿列克谢·萨莫什金(Alexey Samoshkin)

为什么要使用命令行模糊查找器fzf (Why you should be using fzf, the command line fuzzy finder)

找到缺少的演示 (Missing demo found)

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 ,并已成为该项目自述文件的一部分。

搜索文件 (Searching for files)

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%, use ^ and $ to match the start and end of the string respectively, and use ! 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

bash和zsh的模糊补全 (Fuzzy completion for bash and zsh)

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更改到所选目录。

变更目录 (Changing directory)

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的另一种方法。

命令历史 (Command history)

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.

它将选定的项目粘贴在命令行上,因此您可以对其进行进一步的调整。

搜索主机名以SSH进入 (Search the host name to SSH into)

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地址和主机名,并显示一个交互式查找器。

向过程发送信号 (Send a signal to a process)

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_name> to resolve PID followed by a kill <process_pid> referring that PID. With fzf you can combine both steps. Type 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上的“活动监视器”。

搜索时预览文件 (Preview files as you search)

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作为预览命令的语法突出显示功能对其进行了增强。

客制化 (Customization)

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.

通过环境变量完成配置。 这是我的设置中的代码段。 不知道复制粘贴时它是否可以正常工作,很可能还有其他一些缺少的依赖项。 但这只是为了让您了解配置的外观。

Fzf和Vim (Fzf and Vim)

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 ,然后跳转到选定的匹配项。 并且不要忘记在精确的行位置进行即时文件预览。 太棒了,不是吗?

结论 (Conclusion)

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是遵守这些原则的程序的一个很好的例子。

  • let the program to do one thing and do it well (single responsibility principle)让程序做一件事情并且做好事(单一责任原则)
  • make it abstract enough to be agnostic of irrelevant details or data types使它足够抽象,以使无关的细节或数据类型不可知
  • compose separate individual programs using standard well-defined interfaces.使用标准定义明确的界面编写单独的程序。

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命令行使用

fzf命令行使用_为什么要使用命令行模糊查找器fzf相关推荐

  1. cygwin用命令安装软件_软件安装、命令行、Github

    软件安装 Git bash这是一个软件,而git 只是一个命令,具体解释看下面的git的安装和配置部分 git bash 安装前下载谷歌浏览器,能fq,到官网或者百度网盘里下载 百度网盘: 链接: h ...

  2. java 命令读取参数_如何读取/处理命令行参数?

    我原本是C程序员. 我看到了许多花招和"技巧"来阅读许多不同的论点. Python程序员可以通过哪些方式做到这一点? 有关 #1楼 标准库中的规范解决方案是argparse ( d ...

  3. 设计模式 命令模式 实例_根据您的命令-命令设计模式

    设计模式 命令模式 实例 命令设计模式是一种广为人知的设计模式,属于行为设计模式("四人帮"的一部分). 顾名思义,它与应用程序中的动作和事件有关. 问题陈述: 假设有一个网页将在 ...

  4. 28和lba48命令格式区别_编译Sass(命令行)

    本文作者:开课吧无忧 图文编辑:开三金 sass编译有很多种方式,如命令行编译模式.编辑器自动编译.编译软件koala.sass-loader等. 今天我们就先来看第一种:命令行编译 刚才我在test ...

  5. 命令发送广播_那些你不知道的ping命令参数

    在一次代码审计的时候,遇到了ping -c这样的命令,直接把我给干懵了.代码如下: ping命令对于熟悉系统命令的人来说都不陌生,但是相信有很多人和我一样,除了使用ping+IP地址的方法来测试联通性 ...

  6. 传奇gm命令怎么用_传奇GM常用命令

    ----- 常用命令 ----- #IF ; 如果 #SAY ; 输出字符串 #ACT ; 执行命令 break ; 结束命令 close ; 结束对话 ------ 条件命令 ----- CHECK ...

  7. 传奇gm命令怎么用_传奇GM管理命令大全

    新增部分游戏相关命令 -------------------------------------------------------------------------------- 注:以下命令在( ...

  8. netsh命令恢复网络_巧用Netsh命令备份和恢复网络设置

    Netsh是Windows 2000/XP/2003操作系统自身提供的命令行脚本实用工具,它允许用户在本地或远程显示或修改当前正在运行的计算机的网络配置.为了存档.备份或配置其他服务器,Netsh也可 ...

  9. ffmpeg中文开发手册_快速调用复杂命令,支持中文注释,命令行备忘工具navi两天就火了...

    晓查 发自 凹非寺 量子位 报道 | 公众号 QbitAI 刚学的一句新命令,才用完就忘了用法?通常情况下,命令后加一句-help就行了. 但是,命令的帮助文档往往内容太太太太多了,在里面找到自己关心 ...

最新文章

  1. 独家 | 从全方位为你比较3种数据科学工具的比较:Python、R和SAS(附链接)
  2. 请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同...
  3. UML中几种类间关系:继承、实现、依赖、关联、聚合、组合的联系与区别
  4. DL之DNN:自定义2层神经网络TwoLayerNet模型(封装为层级结构)利用MNIST数据集进行训练、预测
  5. android 马甲包 app,寻 实力android马甲包上架技术
  6. ITK:使用Deformation Field扭曲图像
  7. linux由哪些部分组成,linux内核处于什么位置?,为你介绍一些Linux操作系统的基础知识(一)...
  8. 学业水平考试网登录_2019山东高中学业水平考试报名系统入口http://xysp.sdzk.cn
  9. Markdown——编辑器语法——背景色
  10. 4.4_factory_method_创建型模式:工厂方法模式
  11. Yosemite上手动增加基于POP3协议的QQ邮箱账记
  12. UltraEdit 与 正则表达式 [转]
  13. c# PropertyGrid 自定义属性排序
  14. 重装战姬电脑版模拟器怎么玩
  15. Python学习笔记:第十四站 百宝箱
  16. 扬帆际海—开网店怎么引流推广?
  17. H3C模拟器---HCL使用心得
  18. Android图文混排-实现EditText图文混合插入上传
  19. 太阳能板最大面积js
  20. 计算机网络自顶向下方法 习题参考答案 第四章

热门文章

  1. SQL 2008 R2卸载(亲测有用)
  2. SQL Server--实体完整性-域完整性-引用完整性-自定义完整性
  3. 三星公布Note7燃损原因 不惜成本历经数月调查揪出电池“元凶”
  4. poj-1062--昂贵的聘礼
  5. 推荐一款可以替代 Postman 的国产 api 管理工具 apipost
  6. C语言快速排序算法及三种优化方式
  7. 像素大厨PxCook for Mac(自动标注切图工具)中文免费版
  8. Python一键下载原神官网漫画,送给同样喜欢原神的人美心善你们
  9. pta在数组中查找指定元素
  10. 《EDIUS 6.5快刀手高效剪辑技法》——导读