10.6 bash history-search-backwards

This is a repost from http://discussions.apple.com/message.jspa?messageID=10954384

10.6 broke my terminal history search

In 10.4 I added

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

to my .bash_profile allowing me to use the up/down arrows to search through the history in terminal, but as of 10.6 this no longer works.

I would like the up and down arrows to perform a search of the history for the string I have typed in the terminal. That is if I had issued the following sequence of commands in the terminal,

$cd ~/
$nano .bash_profile
$vi .bash_profile

so now these commands are sequentially listed in my bash history. Now if I press the up arrow, the first command to come up will be the last one that I entered, that is, $vi .bash_profile. Now if I had instead pressed up three times, the command, $cd ~/ would come up. Now what I want bash to do, and had working in OS X 10.4, is if I type a string into the terminal (and DO NOT press ENTER) such as, $cd , then I press up, bash will search through the terminal history for the instances of the command cd that I have previously entered. This is the same behavior as the MATLAB command prompt.

I know this is possible because it used to work. I would like to have this behavior again because it is very useful. I do know that I can use crl-R to search the history, but this is kind of a pain to use and it doesn't work very well.

I added the above code to ~/.inputrc and now if I type the letter 'b' in terminal I get the output
{quote}
"": history-search-forward
{quote}

It only does this for the the letter 'b'. I have tried all the other keys and there is no weird output. What is up with bash?

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)

Also see threads:
http://forums.macosxhints.com/showthread.php?p=569961#post569961 Mac OS X Hints 10.6 bash history-search-backwards
http://www.macosxhints.com/article.php?story=20031026174236860 Mac OS X Hints 10.3: Assign arrow keys to history search in bash

Mac Book Pro, Mac OS X (10.6.2)

Posted on Jan 25, 2010 12:02 PM

Reply
4 replies

Jan 25, 2010 12:57 PM in response to jetak

I have the following in my .bashrc file:

#
# Setup Command line editing using vi mode (default is Emacs).
# Map Ctrl-P and Up-arrow as command recall (previous),
# with Ctrl-N and Down-arrow as next (foreword) command.
#
# Viewing key binding:
# bind -p | more # key bindings
# bind -s | more # macros (like below)
# bind -m vi-command -p | more # key bindings
# bind -m vi-command -s | more # macros (like below)
#
shopt -s -o vi # set vi command line editing.
bind '"C-p": "ekA"' # Ctrl-P: when in insert mode
bind -m vi-command '"C-p": "kA"' # Ctrl-P: when in insert mode
bind '"C-n": "ejA"' # Ctrl-N: when in command mode
bind -m vi-command '"C-n": "jA"' # Ctrl-N: when in command mode
#
# I have had problems with a Linux version of Bash 2.05.8(1) that did not
# like it when I remapped the up and down arrow keys, so if I find myself
# with that version of bash, I do not try to remap them.
#
# "cat -v" can be very useful for determing what the escape sequence being
# issued by an keypad or function key. Keep in minde that the keypad has
# multiple modes the terminal emulator can be placed in, which alters the
# escape sequence, hence the multiple sets of mapping for up and down arrow
# in both vi-insert and vi-command mode.
#
if [[ "$BASH_VERSION" != 2.05.8(1) || $OSTYPE = darwin ]]; then #{
bind '"M-OA": "ekA"' # Up-arrow: when in insert mode
bind -m vi-command '"M-OA": "kA"' # Up-arrow: when in insert mode
bind '"M-OB": "ejA"' # Down-arrow: when in command mode
bind -m vi-command '"M-OB": "jA"' # Down-arrow: when in command mode
bind '"M-[A": "ekA"' # Up-arrow: when in insert mode
bind -m vi-command '"M-[A": "kA"' # Up-arrow: when in insert mode
bind '"M-[B": "ejA"' # Down-arrow: when in command mode
bind -m vi-command '"M-[B": "jA"' # Down-arrow: when in command mode
fi #}

The above does work for me on ALL my platforms (Mac OS X, Tiger, Leopard, Snow Leopard), Linux, Solaris, AIX, and Windows-Cygwin.

I use vi command line editing, so if you prefer the emacs command line editing, then the above will not work for you, in some cases I map the up/down arrows and Control-P/Control-N to vi commands.

Jan 25, 2010 1:20 PM in response to BobHarris

My comments may be totally messed up. I think the bind commands that use \e are for when I'm in insert mode, where the \e is escape to get back to command mode. And the bind commands that do not have \e are for when I'm actually in command mode.

The truth is, I created these bind commands about 4+ years ago, and I debugged them and they do work for me, but today, I would have to dig back into the bash man page as well as the readline documentation to figure out what they really do today 🙂

Message was edited by: BobHarris

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

10.6 bash history-search-backwards

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.