未加星标

McFly A Replacement To ‘Ctrl+R’ Bash History Search Feature

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

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

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:
<strong>if [ -f $(brew --prefix)/opt/mcfly/mcfly.bash ]; then
. $(brew --prefix)/opt/mcfly/mcfly.bash
fi</strong>
:beer: /home/linuxbrew/.linuxbrew/Cellar/mcfly/v0.2.5: 4 files, 3.5MB, built in 33 seconds
McFly   A Replacement To ‘Ctrl+R’ Bash History Search Feature

install mcfly

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:


McFly   A Replacement To ‘Ctrl+R’ Bash History Search Feature

Search through bash history using McFly

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:


McFly   A Replacement To ‘Ctrl+R’ Bash History Search Feature

McFly in action

View help:

$ mcfly --help Suggested read: HSTR Easily View, Navigate, Search And Manage Your Commandline History Pick A Commandline Fuzzy Search Tool For Linux How To Clear A Specific Command From Bash History In Linux 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!

Resource: McFly GitHub Repository

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

代码区博客精选文章
分页:12
转载请注明
本文标题:McFly A Replacement To ‘Ctrl+R’ Bash History Search Feature
本站链接:https://www.codesec.net/view/621276.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(51)