Replacing history with McFly

Command Line – McFly

© Photo by Shiro hatori on Unsplash

© Photo by Shiro hatori on Unsplash

Article from Issue 265/2022
Author(s):

McFly improves on the venerable history command with a customizable interface and contextualized results.

The history command brings previously used commands forward to use again. So what could be a more appropriate name for a history replacement than the last name of Michael J. Fox's character in Back to the Future? McFly [1] replaces history with an improved interface and more contextualized results that are generated with an AI-based algorithm and an SQLite database. Although no quicker with results than history, McFly offers numerous advantages:

  • Results are displayed full-screen, with basic commands summarized in the titlebar (Figure 1).
Figure 1: Unlike history, McFly uses a result screen, with a summary of navigation keys in the titlebar.
  • Information is given about when a result was run and in which directory, if it was not in your home directory, as well as if it failed. Failed results are usually not displayed.
  • Priority is given to the present working directory.
  • Results take into account other commands that in the past were run after the command.

Unlike the history command (Figure 2), McFly does not number results, so you can cannot jump to a specific one. However, numbering is rarely useful, and McFly's contextualized results, as well as the ability to launch a command from the results page, more than compensate for the lack of numbering. Similarly, the ability to edit a result in McFly means that history's complicated editing tools are unnecessary. In addition, McFly's structure is easier to use than history's, which many users ignore when searching history in favor of the up and down arrow keys.

Figure 2: Search results with the old history command.

Installing and Configuring McFly

McFly runs on the Bash and Zsh command shells, with support for the fish shell in development. McFly can be installed using Homebrew [2] or using its installation script. To use the installation script as shown in Figure 3, run the following with root privileges:

curl -LSfs https://raw.githubusercontent.com/cantino/mcfly/master/ci/install.sh | sh -s -- --git cantino/mcfly
Figure 3: McFly's installation script starting up.

Then add the following line to the ~/.bashrc file

eval "$(mcfly init bash)"

or to ~.zshrc

eval "$(mcfly init zsh)"

Finally, to link McFly to the shell, run

source ~/.bashrc

or

source ~/.zshrc

In both cases, finish by importing the shell history (Figure 4). On older or much-used machines, the process may take several minutes.

Figure 4: The first time it runs, McFly imports history's list of commands.

At this point, McFly is ready to run, but you may want to configure it in ~/.bashrc or ~/.zshrc with the options shown in Table 1.

Table 1

McFly Options

export MCFLY_LIGHT=TRUE

Sets McFly to run in Light mode rather than the default Dark mode.

export MCFLY_KEY_SCHEME=vim

Sets McFly to use Vim key commands rather than the default Emacs.

export MCFLY_FUZZY=true

Results display related terms, not just exact matches. This field can increase the chance of finding results, but at the cost of more results to scroll through.

export MCFLY_INTERFACE_VIEW=BOTTOM

Places titlebar menu at the bottom of the screen.

export MCFLY_DISABLE_MENU=TRUE

The titlebar menu is not displayed.

export MCFLY_RESULTS=50

Limits the number of results displayed. By default, only the top dozen results display.

export MCFLY_HISTORY_LIMIT=5000

Limits the number of listings in McFly's history.

export MCFLY_RESULTS_SORT=LAST_RUN or MCFLY_RESULTS_SORT=RANK

Displays results according to the time run or by the rank assigned by McFly.

Running McFly

McFly's basic syntax for searching is simple:

McFly search STRING

The basic navigation appears in the titlebar of the results page (see Table 2).

Table 2

Navigating McFly Results

Enter

Run the command highlighted by the arrow key.

Tab

Edit a command before running it.

F1

Sort by time, rather than rank. This is the default, so it has no effect unless MCFLY_RESULTS_SORT=RANK is set (see Table 1).

F2

Delete the selected command from McFly's history. Useful for improving results.

Esc

Closes result page and returns to the command prompt.

You can also display McFly's complete history and scroll through it with the up and down arrow keys. Using the arrow keys is usually the least effective way to use McFly, but it is probably the most common way to access history when you are using the same command several times in a row. Providing this functionality makes McFly a complete replacement for history.

Results may change depending on the directory from which McFly is run, which can return more accurate results, but can also confuse users or complicate results. Similarly, if results are set to display in priority, a previously selected command is given priority, while a command that results in an error is not displayed or given lower priority. As you continue to use McFly, the results should improve as McFly adjusts to your use of commands. The search sub-command can take the option --fuzzy (-f) NUMBER for a fuzzy search and --results (-r) NUMBER to specify the maximum number of results. In addition, --output-selection (-o) PATH saves the results to a file.

Many users may only use the search sub-command. However, McFly also supports other sub-commands, although they are not documented by any man page, only by brief entries accessed from the --help SUB-COMMAND option. The add sub-command can add to the history, using --dir (-d) to specify where the command was run, --exit (-e) EXIT-CODE to specify an exit code, and --when (-w) to set when the command was run. Similarly, move can specify an old directory (OLD_DIR_PATH) and a new one (NEW_DIR_PATH) in that order. The sub-commands init and train are also available for developers, but appear to be undocumented. However, the other sub-commands can be used by users to assist McFly in improving its search results.

Is McFly Worth Using?

At least initially, algorithms that can be trained must be taken on faith. All I can say with confidence is that several weeks of semi-regular use did not noticeably improve McFly's results. However, such limited, inconsistent trials are inconclusive. It seems likely that heavier use or a longer trial period would produce clearer results. Documentation on how to use the train sub-command would probably be useful as well.

However, even without a learning algorithm, McFly has decided benefits over history. Not only is McFly customizable, both through its environmental variables and the add and move sub-commands, but its simple interface makes it much easier to use than history. Even with the verdict still out about its learning capabilities, McFly's design makes it one of the new breed of modern Linux commands that are replacing so many of the venerable ones.

The Author

Bruce Byfield is a computer journalist and a freelance writer and editor specializing in free and open source software. In addition to his writing projects, he also teaches live and e-learning courses. In his spare time, Bruce writes about Northwest Coast art (http://brucebyfield.wordpress.com). He is also co-founder of Prentice Pieces, a blog about writing and fantasy at https://prenticepieces.com/.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • McFly

    When it comes to working at the command line, using Bash history effectively can save you time. McFly extends the Bash history's features and helps you find past commands more quickly.

  • Don't Know Much About History

    The versatile Bash history command can save you time and effort at the command line.

  • FOSSPicks

    Graham looks at TreeSheets, rare, McFly, b2, DrumGizmo, A/B Street, Xmonk, and much more!

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News