Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Unfortunately history-search-(forward|backward) is not mapped to a combination in most common configurations.

I prefer to map it to esc-p which coincides with the setting in tcsh. Like that:

"\M-p": history-search-backward

So you basically type something and then press esc p to have the shell complete it.



sort by: page size:

I use ctrl-P/N for history-beginning-search-{backward,forward} in Zsh. Type the start of a previous command and cycle through the matches.

Or in .bashrc:

bind "C-p":history-search-backward

bind "C-n":history-search-forward


To enable that in bash, put this into your .inputrc file

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


If you're using zsh, you should try adding this to your .zshrc

bindkey '\e[A' history-search-backward bindkey '\e[B' history-search-forward

It tells zsh to search backward and forward in the history for any line starting with the same string as what is currently entered when you press the up and down arrow... Helps saves a lot of time compared to always just using Ctrl-r.

I think there's an equivalent for bash, but I don't know it...


Personally I've bound history searches to the up/down arrow to make bash behave more like the C-shell in this regard:

    "\e[A": history-search-backward
    "\e[B": history-search-forward
(This was really the only thing I disliked about bash compared to csh, so imagine my delight when I found out this option existed in readline.)

add this to your .zshrc:

bindkey '^r' history-incremental-search-backward

I suggest to look for a .zshrc file, the article has a good one linked.


This is indeed a lovely feature I couldn't live without.

In bash, you can get it by adding the following to your .inputrc (at least on mac terminal)

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

I know that people like to use ctrl-R to search backwards through history, but I am very zealous about using the up-arrow to search through your history instead (in your .bashrc):

bindkey "^[[a" history-beginning-search-backward

bindkey "^[[B" history-beginning-search-forward


You can also put this in your bashrc:

  bind '"\e[A":history-search-backward'
  bind '"\e[B":history-search-forward'
It's one of the things I always tell people to use since it makes using bash so much easier.

I just use this for quick history search of typically my last few commands in .zshrc:

    # tsch-style history search esc-n/esc-p
    bindkey '\ep' history-beginning-search-backward
    bindkey '\en' history-beginning-search-forward
If I need substrings or searching for something further back in time I just use `history -99999 | rg`

Tried ctrl-r bash style for about a month back when I was switching off of tcsh and hated it.


Not just that.

Add this to you inputrc:

"\e[A": history-search-backward

"\e[B": history-search-forward

Now up and down arrows will search your history with whatever you already have typed at the prompt. Re-running a command often? Probably just have to type its first letter and hit up arrow once or twice.


Example:

Tip: Bash reverse search, because manually scrolling through your history is a nuisance.

How (OS X): CTRL+R, start typing a word from your command history. Press CTRL+R to cycle through matches, ENTER to execute, TAB to edit. If you want to add forward search to bash, you can re-map your key bindings in your bash profile with the following (maps to CTRL+T): bind "\C-t":forward-search-history


Exactly, but it makes more sense to bind history-search-backward and history-search-forward to <M-p> and <M-n> in my opinion.

Or in ~/.zshrc:

    bindkey '\e[A'  history-beginning-search-backward

As an emacs user I map "C-p" "C-n" for history search in bash

In .inputrc

  "\C-p": history-search-backward
  "\C-n": history-search-forward
Also I found the following settings to be very useful:

  # List the possible completions when Tab is pressed
  set show-all-if-ambiguous on
  #tab complete without needing to get the case right
  set completion-ignore-case on

So does libreadline (and therefore bash). The following in ~/.inputrc does the trick for vi mode users:

k: history-search-backward j: history-search-forward

There is an equivalent for emacs mode.


One of my most life-changing bash configurations is having history-search-backward / history-search-forward bound to the up and down arrows.

Put the following in your ~/.inputrc

  "\e[A": history-search-backward
  "\e[B": history-search-forward
  "\e[C": forward-char
  "\e[D": backward-char
  set show-all-if-ambiguous on
  set completion-ignore-case on 
Now if you type, say:

  cat <UP ARROW>
You will cycle through commands in your history that start with the prefix "cat ".

Instead of searching through history, you can add this to your .inputrc

  "\e[B": history-search-forward
  "\e[A": history-search-backward
And this to your .bash_proflie

  export HISTCONTROL=erasedups
  export HISTSIZE=100000
  shopt -s histappend
Now the up/down arrow keys auto search and complete backwards/forwards based on what's written. If the line is empty, it behaves as normal.

This is also pretty neat, in your .bash_proflie:

  bind '"\t":menu-complete'
Enables cyclic tab completion

These are the first things I do when I'm on another terminal.


I usually just do `history | grep ...` instead; it's faster and more flexible than trying to flip through Ctrl+R matches.

That said, I did just install the history-search-backward and history-search-forward hooks in inputrc, and I can already tell I'll love these.

next

Legal | privacy