7

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?

5
  • Reverse search is a function of the shell not the terminal. It would be worthwhile mentioning what shell you're using. bash? zsh? And to answer the second part of your question: it's not happening for me zsh 5.0.7 using the latest iTerm2 on 10.10.2. Edit starts at the end of the command as expected.
    – Ian C.
    Commented Mar 1, 2015 at 18:51
  • I'm using bash 3.2.53 and the latest iTerm2.
    – Kar
    Commented Mar 1, 2015 at 19:08
  • bash 3.2.57 is working as expected for me, here. It puts the cursor one character beyond the last character in the line every time.
    – Ian C.
    Commented Mar 1, 2015 at 19:22
  • @IanC. inspired me to switch to zsh, and I'm quite happy with it!
    – Kar
    Commented Mar 1, 2015 at 19:48
  • My dotfiles can help you get up and running fast with zsh: github.com/ianchesal/dotfiles
    – Ian C.
    Commented Mar 1, 2015 at 22:50

3 Answers 3

6

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.

3
  • My PS1 is PS1='\[\e[0;33m\]\u\[\e[0m\]:\[\e[0;32m\]\w\[\e[0m\]\$ ' I can't quite seem to pick out what hasn't been escaped.
    – Kar
    Commented Mar 2, 2015 at 6:24
  • Note: it is unnecessary and undesirable to export shell variables. Interactive sub-shells will run the startup script and define the variable, and it is generally undesirable to have programs you run from the shell inherit shell-specific variables.
    – Chris Page
    Commented Dec 11, 2015 at 1:03
  • So glad I found this - I had a similar issue where reverse-search + edits was inserting the edits in the wrong place, and this fix was exactly the solution for that!
    – Krease
    Commented Jan 30, 2019 at 0:04
0

The unfortunate answer is: it's just you.

My bash is:

bash-3.2$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)
Copyright (C) 2007 Free Software Foundation, Inc.

And iTerm2 is the latest release and it behaves as expected: Ctrl-E during a reverse history search puts me at the end of the line, on character after the last character in the selected line.

It might be something in your bash configuration that's causing the problem. You can move aside your ~/.bash_profile and ~/.bashrc` files and try Ctrl-R and Ctrl-E and see if things behave better. If they do, replace your customizations one at a time and see if you can spot the culprit.

0

Changing the export TERM="xterm-color" to export TERM="xterm" in my .bash_profile fixed the issue for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .