History commands render incorrect on Bash Vi mode

bashcommand lineitermterminalvi

I'm having this werid and really annoying issue in bash vi-mode.

Env:
Mac 10.9.2, iTerm 2 1.0 or Terminal.app, $TERM={screen-256color|xterm-256color|xterm|vt}

Repro steps:

  1. Run some long commands (like 'some_long_commands_long_long')
  2. Press Esc and then press k.

The issue is:

  1. I do see "some_long_commands_long_long" on my command line. But the cursor is not located at the beginning, instead it's on the 12nd char, see screenshot
    enter image description here

  2. If I press A to append some other text, the cursor will go beyond the command, which means the REAL command is start from where the cursor is located at the first place, the rendering is wrong.

    enter image description here

I searched on google but ended up with nothing….. Anyone has a better idea about this? Thanks a lot…

Best Answer

After debugging for quite a while I finally figured it out. It's an issue caused by PS1. Basically I forgot to wrap my colored ">" with "\[" and "\]".