mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
[Translated] 20181210 McFly - A Replacement To \342\200\230Ctrl-R- Bash History Search Feature.md
Signed-off-by: FSSlc <liuchang011235@163.com>
This commit is contained in:
parent
73e112578f
commit
c029acada7
@ -1,160 +0,0 @@
|
|||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: (FSSlc)
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
[#]: subject: (McFly – A Replacement To ‘Ctrl+R’ Bash History Search Feature)
|
|
||||||
[#]: via: (https://www.ostechnix.com/mcfly-a-replacement-to-ctrlr-bash-history-search-feature/)
|
|
||||||
[#]: author: (SK https://www.ostechnix.com/author/sk/)
|
|
||||||
|
|
||||||
McFly – A Replacement To ‘Ctrl+R’ Bash History Search Feature
|
|
||||||
======
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/12/mcfly-720x340.png)
|
|
||||||
If you spend a lot of time on CLI mode, you should definitely used or heard about **reverse search** function in BASH. The keyboard shortcut to do reverse search in Bash is **Ctrl+r**. Using bash reverse search, we can bring up all commands which we used previously executed without having to re-type them every time. You can, of course, use UP/DOWN arrows to search your bash history. However, Ctrl+r will make this process much easier and faster. Today, I Stumbled upon a replacement to ‘Ctrl+r’ Bash history search feature. Meet **“McFly”** , a simple tool written in **Rust** programming language that replaces the default Ctrl+r Bash history search with an intelligent search engine. All command suggestions made by McFly are prioritized in real time with a small **neural network**.
|
|
||||||
|
|
||||||
McFly rebinds Ctrl+r functionality to bring up all recently executed commands from your Bash history. It augments your shell history by tracking the following:
|
|
||||||
|
|
||||||
* Command exit status,
|
|
||||||
* timestamp (When you run the command),
|
|
||||||
* and execution directory (Where you run the command).
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
It saves all tracking details in a SQLite database. Since it tracks the command’s historical exit status, you can simply ignore the old failed commands. Cool, yeah?
|
|
||||||
|
|
||||||
When suggesting a command, McFly considers the following facts:
|
|
||||||
|
|
||||||
* On which directory you ran the command. You’re likely to repeat that command in the same directory in future.
|
|
||||||
* What commands you typed before the command.
|
|
||||||
* How often you run the command.
|
|
||||||
* When you last ran the command.
|
|
||||||
* If you’ve selected the command in McFly before.
|
|
||||||
* The command’s historical exit status. Because, you probably don’t want to run old failed commands, right?
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
McFly maintains your default Bash history file, so you can stop using McFly at any time. McFly is not just for BASH, it is also extendable for other shells as well.
|
|
||||||
|
|
||||||
### Installing McFly
|
|
||||||
|
|
||||||
McFly can be installed using Linuxbrew on Linux. If you haven’t installed Linuxbrew yet, refer the following link.
|
|
||||||
|
|
||||||
[Linuxbrew – A Common Package Manager For Linux And Mac OS X][1]
|
|
||||||
|
|
||||||
Once Linuxbrew installed, run the following commands to install McFly:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ brew tap cantino/mcfly https://github.com/cantino/mcfly
|
|
||||||
|
|
||||||
$ brew install mcfly
|
|
||||||
```
|
|
||||||
|
|
||||||
After the installation is completed, you will see the following output.
|
|
||||||
|
|
||||||
```
|
|
||||||
==> Installing mcfly from cantino/mcfly
|
|
||||||
==> Downloading https://github.com/cantino/mcfly/releases/download/v0.2.5/mcfly-v0
|
|
||||||
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.c
|
|
||||||
######################################################################## 100.0%
|
|
||||||
==> ONE MORE STEP! Edit ~/.bashrc and add the following:
|
|
||||||
|
|
||||||
if [ -f $(brew --prefix)/opt/mcfly/mcfly.bash ]; then
|
|
||||||
. $(brew --prefix)/opt/mcfly/mcfly.bash
|
|
||||||
fi
|
|
||||||
🍺 /home/linuxbrew/.linuxbrew/Cellar/mcfly/v0.2.5: 4 files, 3.5MB, built in 33 seconds
|
|
||||||
```
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/12/install-mcfly.png)
|
|
||||||
As you can see, we need to do one more step before start using McFly.
|
|
||||||
|
|
||||||
Add the following lines to your **~/.bashrc** file:
|
|
||||||
|
|
||||||
```
|
|
||||||
if [ -f $(brew --prefix)/opt/mcfly/mcfly.bash ]; then
|
|
||||||
. $(brew --prefix)/opt/mcfly/mcfly.bash
|
|
||||||
fi
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally, run the following command to take effects changes:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ source ~/.bashrc
|
|
||||||
```
|
|
||||||
|
|
||||||
Your BASH history will be imported to McFly database when you run this command for the first time. It will take a few moments depending upon size of your bash history file. Once the import is done, you will see the following message.
|
|
||||||
|
|
||||||
```
|
|
||||||
McFly: Importing Bash history for the first time. This may take a minute or two...done.
|
|
||||||
```
|
|
||||||
|
|
||||||
You can now start using McFly.
|
|
||||||
|
|
||||||
|
|
||||||
### Usage
|
|
||||||
|
|
||||||
To search through your command history, just type ‘mcfly search’ followed by the part of the command name and hit ENTER key. Mcfly will display the command suggestions based on the search query you just type.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ mcfly search <part-of-the-command>
|
|
||||||
```
|
|
||||||
|
|
||||||
For instance, I type the following command:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ mcfly search mk
|
|
||||||
```
|
|
||||||
|
|
||||||
Here is the sample output from my Ubuntu machine:
|
|
||||||
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/12/mcfly-command-1.png)
|
|
||||||
|
|
||||||
As you can see, I have used ‘mkdir’ command two times. If you want to run a command from the list of suggestions, just use **UP/DOWN** arrows to select it and hit ENTER to run it immediately. If you want to edit a command, choose it and hit **TAB** key to bring it back to your Terminal and then edit before running it. To delete the selected command from the history, just press **F2**.
|
|
||||||
|
|
||||||
Alternatively, type the following command to open the history search and then type any command or part of the command to view the suggestions from your history.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ mcfly search
|
|
||||||
```
|
|
||||||
|
|
||||||
McFly will display the command suggestions as you type.
|
|
||||||
|
|
||||||
Here is a short video demonstration of McFly:
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/12/mcfly-demo.gif)
|
|
||||||
View help:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ mcfly --help
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### Remove McFly
|
|
||||||
|
|
||||||
Don’t like McFly, no problem! Remove it using the following commands:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ brew uninstall mcfly
|
|
||||||
|
|
||||||
$ brew untap cantino/mcfly
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally, remove the lines which we added earlier from **~/.bashrc** file.
|
|
||||||
|
|
||||||
And, that’s all for now. More good stuffs to come. Stay tuned!
|
|
||||||
|
|
||||||
Cheers!
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.ostechnix.com/mcfly-a-replacement-to-ctrlr-bash-history-search-feature/
|
|
||||||
|
|
||||||
作者:[SK][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://www.ostechnix.com/author/sk/
|
|
||||||
[b]: https://github.com/lujun9972
|
|
||||||
[1]: https://www.ostechnix.com/linuxbrew-common-package-manager-linux-mac-os-x/
|
|
@ -0,0 +1,155 @@
|
|||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (FSSlc)
|
||||||
|
[#]: reviewer: ( )
|
||||||
|
[#]: publisher: ( )
|
||||||
|
[#]: url: ( )
|
||||||
|
[#]: subject: (McFly – A Replacement To ‘Ctrl+R’ Bash History Search Feature)
|
||||||
|
[#]: via: (https://www.ostechnix.com/mcfly-a-replacement-to-ctrlr-bash-history-search-feature/)
|
||||||
|
[#]: author: (SK https://www.ostechnix.com/author/sk/)
|
||||||
|
|
||||||
|
McFly – Bash 历史命令搜索特性的一个替代品
|
||||||
|
======
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/12/mcfly-720x340.png)
|
||||||
|
|
||||||
|
假如你在命令行模式下花费过很长时间,那么你必定使用过或者听说过 BASH 的 **反向搜索** 功能,在 Bash 中执行反向搜索功能的快捷键是 **Ctrl+r**。通过使用这个特性,我们可以找到我们执行过的命令而无需再次输入它们。当然,你可以使用上下键来搜索你的 bash 命令记录,但使用 Ctrl+r 快捷键可以让这个搜索过程更简单快速。今天我找寻到了 Bash 历史命令搜索特性 ‘Ctrl+r’ 的一个替代品,它就是 **“McFly”**。McFly 是一个使用 **Rust** 编程语言写就的简洁工具,自带一个智能的搜索引擎,用来替换默认的 Ctrl+r 这个 Bash 历史命令搜索特性。 McFly 提供的命令建议都是通过一个小巧的 **神经网络** 来实时排序给出的。
|
||||||
|
|
||||||
|
McFly 重新绑定了 Ctrl+r 快捷键,可以从你的 Bash 历史命令中找到所有最近执行过的命令。它通过追溯下面的信息来增强你的 shell 历史命令搜索特性:
|
||||||
|
|
||||||
|
* 命令结束状态
|
||||||
|
* 当你运行命令时的时间戳
|
||||||
|
* 以及你运行命令的执行目录
|
||||||
|
|
||||||
|
它将所有追溯的信息保存在一个 SQLite 数据库中。由于它追溯了命令的历史结束状态,所以你可以很轻易地忽略掉失败的命令。听起来很酷,对吧?
|
||||||
|
|
||||||
|
在给出一个命令建议时,它将考虑如下因素:
|
||||||
|
|
||||||
|
* 你在哪个目录执行的这个命令,将来你很有可能在相同的目录重复这个命令
|
||||||
|
* 在你执行这个命令之前,执行过什么命令
|
||||||
|
* 你执行这个命令有多频繁
|
||||||
|
* 你最后执行该命令的时间
|
||||||
|
* 你是否在 McFly 中选择过这个命令
|
||||||
|
* 以及这个命令的历史结束状态。因为你很有可能不会去执行失败过的命令,对吧?
|
||||||
|
|
||||||
|
McFly 维护着你的默认 Bash 历史文件,所以你可以随时停止使用它。McFly 也并不只服务于 BASH, 它也可以扩展到其他 shell 程序。
|
||||||
|
|
||||||
|
### 安装 McFly
|
||||||
|
|
||||||
|
在 Linux 中,McFly 可以使用 Linuxbrew 来安装。如若你还没有安装过 Linuxbrew,那么你可以参考下面的这个链接。(译者注:从其github 主页了解到也可以下载其二进制来使用。)
|
||||||
|
|
||||||
|
[Linuxbrew —— 一个使用 Linux 和 Mac OS X 的通用包管理][1]
|
||||||
|
|
||||||
|
一旦安装好了 Linuxbrew,运行下面的命令来安装 McFly:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ brew tap cantino/mcfly https://github.com/cantino/mcfly
|
||||||
|
|
||||||
|
$ brew install mcfly
|
||||||
|
```
|
||||||
|
|
||||||
|
在安装完成后,你将看到下面的输出:
|
||||||
|
|
||||||
|
```
|
||||||
|
==> Installing mcfly from cantino/mcfly
|
||||||
|
==> Downloading https://github.com/cantino/mcfly/releases/download/v0.2.5/mcfly-v0
|
||||||
|
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.c
|
||||||
|
######################################################################## 100.0%
|
||||||
|
==> ONE MORE STEP! Edit ~/.bashrc and add the following:
|
||||||
|
|
||||||
|
if [ -f $(brew --prefix)/opt/mcfly/mcfly.bash ]; then
|
||||||
|
. $(brew --prefix)/opt/mcfly/mcfly.bash
|
||||||
|
fi
|
||||||
|
🍺 /home/linuxbrew/.linuxbrew/Cellar/mcfly/v0.2.5: 4 files, 3.5MB, built in 33 seconds
|
||||||
|
```
|
||||||
|
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/12/install-mcfly.png)
|
||||||
|
正如你上面看到的那样,在使用 McFly 之前我们需要再做一些配置。
|
||||||
|
|
||||||
|
将下面几行添加到你的 **~/.bashrc** 文件中:
|
||||||
|
|
||||||
|
```
|
||||||
|
if [ -f $(brew --prefix)/opt/mcfly/mcfly.bash ]; then
|
||||||
|
. $(brew --prefix)/opt/mcfly/mcfly.bash
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
最后,再运行下面的命令来让更改生效。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ source ~/.bashrc
|
||||||
|
```
|
||||||
|
|
||||||
|
当你第一次执行上面的这个命令时,你的 BASH 历史将会被导入 McFly 的数据库。依据你的 bash 历史文件的大小,这个过程将花费一些时间。一旦导入完成,你讲看到下面的提示信息。
|
||||||
|
|
||||||
|
```
|
||||||
|
McFly: Importing Bash history for the first time. This may take a minute or two...done.
|
||||||
|
```
|
||||||
|
|
||||||
|
现在你就可以使用 McFly 了。
|
||||||
|
|
||||||
|
### 使用方法
|
||||||
|
|
||||||
|
要在你的命令中执行搜索,只需要键入 ‘mcfly search’ 再加上命令名的一部分,最后敲击 ENTER 键即可。Mcfly 将会基于你刚才键入的搜索查询语句给出命令建议。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ mcfly search <part-of-the-command>
|
||||||
|
```
|
||||||
|
|
||||||
|
例如我键入了下面的命令:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ mcfly search mk
|
||||||
|
```
|
||||||
|
|
||||||
|
下面展示的是我 Ubuntu 机子上的示例输出:
|
||||||
|
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/12/mcfly-command-1.png)
|
||||||
|
|
||||||
|
如你所见,我已经使用过 'mkdir' 这个命令两次。假如你想从这些命令建议中执行其中之一,只需使用上下键来选择它,然后敲击 ENTER 键来执行它就可以了。假如你想编辑其中一个命令,则需要先选择它,然后敲 **TAB** 键将这个命令放置到终端中,最后在运行它之前更改它就行了。要从历史中删除已经选择的命令,按 **F2** 即可。
|
||||||
|
|
||||||
|
或者,输入下面的命令来打开历史搜索,然后输入任意一个命令或者命令的一部分来从你的历史命令中查看它提供的建议。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ mcfly search
|
||||||
|
```
|
||||||
|
|
||||||
|
在你输入的同时, McFly 将会展示命令的提示。
|
||||||
|
|
||||||
|
下面是一个介绍 McFly 的简短演示视频:
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/12/mcfly-demo.gif)
|
||||||
|
|
||||||
|
你还可以使用下面的命令来查看帮助:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ mcfly --help
|
||||||
|
```
|
||||||
|
|
||||||
|
### 移除 McFly
|
||||||
|
|
||||||
|
不喜欢 McFly,没问题!可以使用下面的命令来移除它:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ brew uninstall mcfly
|
||||||
|
|
||||||
|
$ brew untap cantino/mcfly
|
||||||
|
```
|
||||||
|
|
||||||
|
最后,移除先前添加到 **~/.bashrc** 文件中的几行命令。
|
||||||
|
|
||||||
|
好了,这些就是所有了,更多精彩内容敬请期待,请保存关注!
|
||||||
|
|
||||||
|
Cheers!
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.ostechnix.com/mcfly-a-replacement-to-ctrlr-bash-history-search-feature/
|
||||||
|
|
||||||
|
作者:[SK][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[FSSlc](https://github.com/FSSlc)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://www.ostechnix.com/author/sk/
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://www.ostechnix.com/linuxbrew-common-package-manager-linux-mac-os-x/
|
Loading…
Reference in New Issue
Block a user