loading...

Better bash history search with McFly

hamatti profile image Juha-Matti Santala ・2 min read

If there's one functionality of bash that most beginners don't know but get very excited about when they discover it, it's history. First you learn to go through the history by pressing up and down, then you find out that CTRL+R is the spell that gives you access to write commands and find them from history.

I'm a huge fan of that. Probably 80-90% of my bash command history is repeating commands over and over again. Whether it's npm install, npm run start, git add . or something similar, I can find it from my history.

But the built-in search isn't always the optimal one. You can't really see the history: you have to know what you're looking for. Luckily, developer community is amazing and Andrew Cantino has built a tool to augment your bash history.

Meet McFly. Great Scott!

Screenrecording of bash shell using mcfly to read through history, re-run commands and edit commands

Instead of being faced with blank black prompt, you get nice visual view of your history. You can search by writing a part of the command, you can navigate with arrow keys and either run (ENTER) or edit (TAB) your commands.

It also features a smart system that takes into account certain things to be more relevant: in which directory you are, what's the context of the command, if the command has failed or not, etc.

In the beginning, mcfly felt a little bit slower because it takes a moment to start up. But after using it a while, I don't want to go back to guessing game.

Posted on by:

hamatti profile

Juha-Matti Santala

@hamatti

Find my past and future blog posts and programming guides from https://hamatti.org

Discussion

markdown guide
 

I've been using fzf with some keybindings. The added benefit is that fzf is a general purpose fuzzy autocomplete engine, it possible to pick a directory or a file in a similar fashion. It's pretty snappy too.

 

Thanks for sharing Dmitry! I'll have to check it out and give it a go.

 

McFly looks great.
Makes it easy to visually find the command you're after by showing a list.
Thanks for sharing!

I've been using this in my ~/.bash_profile for an easier CTRL+R experience using the up arrow.

bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forward

It completes from your bash history using the up keyboard key.

# Using the up keyboard key you can cycle through your last used commands starting with "cows"
cows[UP]

If you've cycled through a few commands pressing the down arrow brings the previously cycled commands back.

 

That's such a smart improvement, thanks for sharing!