I really like zsh's history autocomplete feature. Namely, when I press up, I get the previous command. When I type emacs
and press up, I get the last emacs command I used. When I type git
and press up, I get the last git command I used. When I try to do this in bash, it just goes to the last generic command I used. Essentially, I want to be able to half-type a command and press up to get the last command I used that matches what I've typed so far. I don't know how to achieve this in bash. I can't use zsh on this system, so is it possible to replicate this functionality in bash?
The up arrow is bound to the previous-history
command. You want to rebind it to history-search-backward
(which is unbound by default) instead. You can check which keys previous-history
is currently bound to:
$ bind -p | grep previous-history
"\C-p": previous-history
"\eOA": previous-history
"\e[A": previous-history
In my case, the last two both represent up arrow (the exact escape sequence may differ from terminal to terminal, or depending on what mode the terminal is in, but these two are fairly standard). Especially since previous-history
will still be available with Control-P, it's safe to change the behavior of the up arrow.
Add this to your .inputrc
file (creating the file if necessary):
"\e[A": history-search-backward
"\eOA": history-previous-history
Or, you can add call bind
from your .bashrc
:
bind '"\e[A": history-search-backward'
bind '"\eOA": history-search-backward'
You may also want to similarly bind history-search-forward
to the down arrow key, \e[B
and \eOB
.
-
This works as close as I can get in bash, I guess, so thanks! Is the proper
\eOB
bindhistory-next-history
then? – Hal T Feb 17 '17 at 14:35 -
The only missing behavior is putting the cursor at the end of the line instead of the character I last typed, for example if I type
ema
then press up, the text that comes up will beemacs -nw .inputrc
but the cursor will be at the end ofema
instead of the end of the line. Can this be fixed? – Hal T Feb 17 '17 at 14:37 -
next-history
is the "downwards" version ofprevious-history
. As far as positioning the cursor, Control-E is bound toend-of-line
, so you can move there quickly, but I don't think there is a way to automate that. – chepner Feb 17 '17 at 15:16
You can search your history with ctrl+R.
When you first press it, the prompt will change and you will be able to enter the characters you want to search for in the history. The new prompt disregards characters that were entered in the previous prompt and will overwrite them when something from the history is matched.
It will display the last command that matches your input and you can press ctrl+R again to navigate the results from the latest to the earliest.
When you've found the entry you're interested in, you can either press Enter to execute it or the left or right arrows to return to the standard prompt to edit the command line. Pressing the up or down arrows will return to the standard prompt but navigate the history one step forward or backward, which I find more confusing than anything.
-
2This just seems to turn my comment into an answer that does not achieve what was asked? – Gert van den Berg Feb 17 '17 at 13:44
-
@GertvandenBerg It's the same as your comment, although I typed my answer before seeing your comment (you just have my word for it obviously). I don't see how it doesn't answer the question? The functionnality is replicated, although in a different way as you pointed out – Aaron Feb 17 '17 at 13:45
-
While this achieves similar behavior, it does not achieve the exact behavior. Is it possible to get the exact same behavior? – Hal T Feb 17 '17 at 13:45
-
@HalT If you mean binding the up arrow to this functionality, or being able to type part of the command and only then typing the key that activate this functionality, I don't know how to do that, sorry. It may be possible, but you'll have to wait for someone more knowledgeable to answer. – Aaron Feb 17 '17 at 13:48
-
@Aaron: Makes sense... @HalT:
bash
is significantly simpler thanzsh
, my guess would be "no" (There might be really dodgy tricks using programmable completion) – Gert van den Berg Feb 17 '17 at 13:48
Ctrl-R
might achieve something similar in a different way... (Ctrl-R and then type emacs to get lastemacs
command, Ctrl-R again to get one further back, etc...) – Gert van den Berg Feb 17 '17 at 13:42command #useful
and when you need type#useful
by CTRL+R – Ans Feb 17 '17 at 13:50