Reverse history search in bash followed by arrow keys causes cursor to display at the wrong location


I've noticed that many times when I'm reverse-history-searching from the command line via CTRL+r, if I want to edit the command, sometimes the cursor jumps to the wrong position when I press an arrow key or enter CTRL+a or CTRL+e. The specific position it jumps to seems to be an offset equal to the size of my command line prompt as set in PS1.

This causes difficulty because the actual location of the cursor is different from what is rendered on the screen; future edits to the command are rendered as if the cursor is where it jumped to, but the actual command retains the correct location without any jump.

Does anyone know what's causing this or how to fix it?

EDIT: If I add a newline at the end of PS1 the problem seems to go away. However, I would prefer to enter the command on the same line as the prompt.

Best Answer

This is often caused by commands in your history that are longer than 1 line. When you select a shorter item out of the history it doesn't seem to take into account the width of the prompt in re-rendering the command line, so everything after that long command is screwy.

The easy solution I've found is to hit the home key, insert "echo " and hit enter. Then recall the last command and edit from there. (First removing the echo. :)