6

When I ssh into a Linux server from bash Terminal, I can use CTRL+A to return to the start of line properly, and CTRL+E to go to end of line and everything works fine.

When I use bash locally on my Mac, when I use CTRL+A it logically goes back to the start of the line, but doesn't display the cursor in the correct place. It displays the cursor 7 or so characters off and doesn't update the line text correct.

Example

find . -name "hello.cpp"

CTRL+A!

find . -name "hello.cpp"
       ^~~ cursor shows here ????

Insert "this is a test" (just to show)

Displays:

find . this is a test -name "hello.cpp"
                      ^~~ cursor is still here ???
       ^~~~~~~~~~~~~^ text inserted here ???
^~~~~~^ unchanged for some reason

Command executed

this is a test find . -name "hello.cpp"

I use the same

PS1=\n \e[1;95m \u@\h \e[0;32m\w \e[1;30m $(__git_ps1 " (%s)" ) \n \e[1;95m \@ \n $ \e[0;30m

as I do on Linux.

Version Info

  • bash version: 3.2.57(1)
  • terminal version: 2.6.1 (361.1)
  • OS X El Capitan, 10.11.5
  • 3
    Actually the reason seems to be the opposite you need [ ] to enclose the colour shift codes as per the article quoted there ibm.com/developerworks/linux/library/l-tip-prompt – Mark Jun 5 '16 at 19:21
  • Adding \[ and \] around the colors solved the problem. It's actually the inverse of the other problem, which I'd recommend changing the subject line for as this is a more general issue than just for "emacs style". Do you want me to mark as duplicate even though they're inversed? I think this subject would make it easier to find. – MtRoad Jun 5 '16 at 20:43
  • Currently it is not a duplicate but I suspect a question could be written using the other answer to produce a common question – Mark Jun 6 '16 at 10:08
  • @Mark I converted your comment to an answer. Questions should have answers. If you create your own answer (you can copy the one I added if you want) then I will delete mine. – Bennett McElwee Oct 27 '16 at 21:58
5

Add \[ and \] around the colour codes to enable bash to calculate the line length correctly. Something like this:

PS1=\n \[\e[1;95m\] \u@\h \[\e[0;32m\]\w \[\e[1;30m\] $(__git_ps1 " (%s)" ) \n \[\e[1;95m\] \@ \n $ \[\e[0;30m\]

(This answer is taken from @Mark's comment on the question.)

You must log in to answer this question.

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