Ctrl+r is a great little tool for searching your .bash_history
for previously run commands.
However, when I use it in my OS X Terminal.app I see weird behavior, and I was wondering if anyone else sees the same thing or knows how to fix it.
- Ctrl+r
- type in something like
find
- oh cool, look...it's the command I wanted
find . -exec grep -q "hello world" '{}' \; -print
- I want to run that command but change hello world to something else.
- So I hit ← or →
- Now the command is SORTA on the command line but it always looks like some trunctated version of the command, like this:
-q "blog_posts_by" '{}' \; -print
, where the whole command is there, and I can use the cursor keys to move around on the line, but not all of it is printed. There is a disconnect between what is shown on the line and what the terminal actually thinks I am editing.
Does anyone have a clue why this might happen? It's not an easy phenomenon to search the webs for.
You probably have escape sequences for colors in your prompt that are not properly delimited. They need to be enclosed in
\[
and\]
.The length of non-printing character sequences are not included in the length of the prompt when they are thus enclosed and the position of previous commands needs to be calculated for proper display when they wrap.
This could be due to the way the escape keys are configured on your Terminal, what I normally do is use the left or right arrow on iTerm or Terminal.app since I'm not a fan of hitting return immediately, does that work for you?
Another possible cause for this problem is having the wrong
TERM
value. For instance, this happened to me when I was using colors in my PS1, but myTERM
was set toxterm
. I changed it toxterm-256color
and then CTRL-r started working correctly again.