DEV Community

Ctrl+R Autocomplete with Bash is a Life Saver

Ben Halpern on January 17, 2017

I only recently started integrating Ctrl+R into my command-line workflow, and I am happy I did! The command, called reverse-i-search, starts an aut...
Collapse
 
amrutprabhu profile image
Amrut Prabhu

This is surely a life saver for me. Just tried it. And it works!!!! :)

Collapse
 
tbodt profile image
tbodt

For me, what's even better than Ctrl-R is the Fish shell. In Fish, you type something that you want to search for in your history and then press up, and it goes back through your history and only shows commands that match what you typed. Your shell configuration looks pretty bare-bones from the screenshot, so you might also appreciate the syntax highlighting and very high-quality autocomplete.

The website is at fishshell.com. If you have Homebrew, you can install it with brew install fish.

Collapse
 
michaelhazani profile image
Michael Hazani

yup. I've been using Fish since I started programming and I fail to see why it's not omnipresent. One you go fish you never go back.

Collapse
 
bezirganyan profile image
Grigor Bezirganyan • Edited

It's same for zsh with ohmyzsh, just write the start and up up :)

Collapse
 
tbodt profile image
tbodt

The thing I don't like about zsh is you need a whole pile of custom code such as ohmyzsh and its plugins, which really slow it down. All the cool things about fish are built in, and written in C++.

Thread Thread
 
bezirganyan profile image
Grigor Bezirganyan

I've never used fish, so I think I will give it a try!

Thread Thread
 
gcorrel profile image
gcorrel

And who says zsh's plugins slows its functionality down? It even works better and faster with its plugins + OhMyZsh.

Collapse
 
ben profile image
Ben Halpern

Thanks for the tip. I'll look into it. If you're interested in breaking this out into your own post with an intro + tips on fish, and why you use it, I think that would be useful for folks.

Collapse
 
shavs profile image
Stephen Shave • Edited

This is also in Bash, but requires a little bit of config. Put this into your ~/.inputrc file:

"\e[A":history-search-backward
"\e[B":history-search-forward
Enter fullscreen mode Exit fullscreen mode

This emulates the functionality of Oh My Zsh, where if you type a letter or word, then press up or down, it will search through the .bash_history file.

The only thing I miss from Oh My Zsh is a "visual selector" for files, by tabbing through a virtual list, and ignoring cases when tabbing to a folder.

Collapse
 
oktayacikalin profile image
Oktay Acikalin

I'm also a heavy fish user. Perhaps we should write something for fish and zsh?

Thread Thread
 
ben profile image
Ben Halpern

Do it!

Collapse
 
jerbiahmed profile image
JerbiAhmed

Tried almost every shell out there and I can say Fish is just the best

Collapse
 
rlipscombe profile image
Roger Lipscombe

See also Hashtags for commands, wherein you add a #somethingmemorable after a complicated command, making it easier to Ctrl+R for it in future.

Collapse
 
elado profile image
Elad Ossadon

fzf is even more amazing than the plain ctrl+r - github.com/junegunn/fzf

Collapse
 
morriswchris profile image
Chris Morris

Hands down one of the first things I install on a new computer

Collapse
 
jonasbn profile image
Jonas Brømsø

I have come to enjoy: McFly

REF: github.com/cantino/mcfly

Collapse
 
markuman profile image
Markus Bergholz

If I want to use the last scp command, I simply use !sc for example.

And I use my lazy bash alias for grepping my history (h grep)

function search_in_bash_history () {
        grep "$1" ~/.bash_history
}
alias h="search_in_bash_history"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ben profile image
Ben Halpern

Nice 👌

Collapse
 
docent_net profile image
Maciek Lasyk

Take a look at:

hstr (history suggest box): github.com/dvorka/hstr - it's way better than standard reverse-i-search

Even much better is ZSH's history-substring-search plugin: asciinema.org/a/89727

Collapse
 
andydangerous profile image
Andy

I'm a big fan of Ctrl+r, but sometimes eschew it for the limited duplicate ups afforded by export HISTCONTROL=erasedups

Collapse
 
jonasbn profile image
Jonas Brømsø

HISTIGNORE is also a good tool

export HISTIGNORE="pwd:ls:ls -l:cd:clear"
Enter fullscreen mode Exit fullscreen mode

REF: jonasbn.github.io/til/bash/slimmin...

Collapse
 
erikch profile image
Erik Hanchett

Great article! Ctrl -R is great!

I set up my command line to use vim like keybindings. You have to run this command first, or put it in your .zshrc or .bashrc.

set -o vi

To search I press the escape key then /. I can also go to the end of the command line by pressing ^, or the front by pressing $. I can delete words just like I'm in vim.

Collapse
 
jakallergis profile image
John A. Kallergis

Another awesome thing to do is set some key mappings to your terminal (I use the magnificent iTerm) to better navigate through the words in your lines.
Like alt-left to go back a word, or cmd-Del to delete the whole line, or alt-Del to delete just a word, etc.
My most used key mappings (OSX):

Cmd + Left Arrow : Send Hex Codes 0x01 (Start Of Line)
Cmd + Right Arrow : Send Hex Codes 0x05 (End Of Line)
Option + Left Arrow : Send Escape Sequence b (Go Back a Word)
Option + Right Arrow : Send Escape Sequence f (Forward a Word)
Cmd + Del : Send Hex Codes 0x15 (Delete Line)

Collapse
 
redgreenrepeat profile image
Andrew

aren't many of these mapping are built into bash by default?

ctrl-a : beginning of line
ctrl-e : end of line
ctrl-w : delete last word

ctrl-k : cut to end of line
ctrl-y : paste cut item

meta/alt/command-f: move forward a word
meta/alt/command-d: cut forward a word
...?

and on OSX/macOS, some of these mappings are system wide:

redgreenrepeat.com/2016/10/14/extr...

Collapse
 
flinux profile image
Francesco Marasco

try ohmyz.sh/ !!

Collapse
 
weswedding profile image
Weston Wedding

This is totally tangential, but what did you use to capture the animated gif?

Collapse
 
drumor profile image
Ludovic Taffin

Waouw didn't know about that! Thx Ben!

Collapse
 
rafaelcg profile image
Rafael Corrêa Gomes

I loved it _

Collapse
 
philthomasme profile image
Phil Thomas

It never ceases to amaze me that I find something like this after years of using bash. Thanks for the tip!

Collapse
 
jonasbn profile image
Jonas Brømsø

Do Try McFly

REF: github.com/cantino/mcfly

Collapse
 
dosamp profile image
DosAmp

Another upside is that this works with all applications that use libreadline. I just used this to bring up a complex SQL query from my ~/.mysql_history, and it also does the job for Python 3.x.

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

In zsh, I bound ctrl-R to history-incremental-pattern-search-backward, which allow wild cards in the search string. It's even more useful.

Collapse
 
geoff profile image
Geoff Davis

This is what I use, really enjoy it. Still "autocompletes", but you don't have to move from the arrow keys

Collapse
 
benhemphill profile image
Ben Hemphill

Thats awesome! Going to use that. Ohmyzsh does some interesting things too, but definitely my pattern is still a few too many up arrow keys :)