MacOS – Terminal/iTerm2 issue: reverse-i-search trims off command

bashitermmacosterminal

I've been having this issue since 10.9.

  1. cd into a reasonably long path, e.g., cd /Volumes/Storage/backup/long/path
  2. Enter a moderately long command, e.g., curl -X GET http://localhost:8888
  3. ctrl+R, type curl
  4. ctrl+E

The command displayed is shifted to far to the left and leaving whitespace to the right like so:

enter image description here

Is this a known bug or is it just me?

Best Answer

It's your $PS1 - it's not properly escaped. You have to escape all non-printing characters with \[ and \]. For example, if you have

PS1='\e[32m\u \W\e[0m $ '

you have to change it to

PS1='\[\e[32m\]\u \W\[\e[0m\] $ '

or your cursor will appear far to the right because your shell thinks your prompt is that long, and when it retypes the command it can't find it. Annoying, I know. In zsh you need to use %{ and %} IIRC.

And while you're considering switching to another shell, I'd suggest fish. It's a bit more of a jump, but it keeps me sane.