-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: '优雅地搜索命令历史记录' | ||
date: 2024-07-24T13:32:48+08:00 | ||
slug: "search-command-history-elegantly" | ||
image: "cover.jpg" | ||
tags: | ||
- linux | ||
- command line | ||
categories: | ||
- Linux | ||
description: "用fzf进行模糊搜索" | ||
--- | ||
|
||
## 前言 | ||
|
||
`history`命令可以显示所有的命令历史记录,但难以查找命令。 | ||
聪明的你当然会想到配合`grep`命令进行搜索,可行但效率太低,这样检索不够灵活,要查找记忆模糊的命令还需要正则匹配。 | ||
|
||
## 模糊搜索命令历史记录 | ||
|
||
[fzf](https://github.com/junegunn/fzf)是一个交互式命令行模糊搜索工具,可以模糊搜索内容、模糊补全文件路径等,功能强大,更多功能请参考官方文档。 | ||
|
||
- 通过自带的包管理器或`homebrew`等第三方包管理器安装即可,参考官方文档安装。 | ||
- 按下`ctrl+r`组合键,进入命令历史记录搜索模式,输入搜索内容,fzf会自动搜索匹配的命令。 | ||
- `ctrl+j`或`ctrl+n`向下移动,`ctrl+k`或`ctrl+p`向上移动,也可以使用方向键或鼠标滚轮,按下回车选中命令。 | ||
- 按下`ctrl+g`组合键或`Esc`键,退出搜索模式,返回命令行。 | ||
|
||
- 如果你忘了快捷键,可以使用`history | fzf`命令,通过管道让`fzf`模糊搜索`history`的输出内容。 | ||
|
||
## 使用fzf进行文件路径补全 | ||
|
||
既然提到`fzf`,那么顺便介绍一下如何使用它进行文件路径补全。 | ||
当然,默认按`tab`键也可以进行补全,但`fzf`更好用。 | ||
|
||
- 如果想补全指定目录下的某个文件名,在指定目录下输入两个星号,然后按下`tab`键,就会打开`fzf`的交互式界面进行匹配。 | ||
- 例:比如想补全`~/.config/`下的某个较长的文件名,`ls ~/.config/**` ,然后按下`tab`键 | ||
- 如果想输入某个用户目录下的路径,可以使用快捷键`ctrl+t`打开交互式界面,选中后回车上屏。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters