0

I've noticed recently in my terminal emulator that sometimes the <up> arrow to show previous commands is behaving very strangely. This is best illustrated with a repeatable example I've found:

ls

(This just makes sure there's some command in the history)

git --git-dir=$HOME/.configrepo/ --work-tree=$HOME status

(It doesn't matter what this command outputs)

Now if I press <up> my current command line changes to git --git-dir=$HOME/.configrepo/ --work-tree=$HOME status, but if I press it again I get git --git-dls.

Oddly, if I run this command it outputs the result of ls, and the text before ls cannot be edited. It's almost as if a fragment of the git command has become a part of the prompt. I originally suspected there were non-printing characters hiding somewhere in there but this happens even if I type out all the commands from scratch.

In the bash profile I have the prompt set as follows.

PS1='\e[0;32m\w\$ \e[0m'

In case it's relevant I'm using the macOS 11.4 Terminal application.

CheChe
  • 64
  • 7
  • 1
    To see if the prompt itself has become weird, press control-L to have bash clear the window and redraw the prompt and whatever you've typed so far. If the extra junk disappears, it's not part of the prompt; if the extra junk remains, it is part of the prompt. In any case this has nothing to do with Git: it's just bash interacting badly with your Terminal (and/or some previous command). – torek Jun 25 '21 at 08:11
  • 1
    Does the same happen if you set `PS1=""`? – choroba Jun 25 '21 at 08:14
  • 1
    Looks like your PS1 improperly handles colors and bash can't calculate cursor positions. Please show `printf "%q\n" "$PS1"`. – KamilCuk Jun 25 '21 at 08:22
  • 1
    Thanks all. PS1="" does indeed correct the problem, and ctrl-L is showing the junk is not part of the prompt. I'll add the PS1 from my bash profile to the question since it sounds like it's very likely that. – CheChe Jun 25 '21 at 08:25
  • The nonprinting sections of the prompt string (i.e. the color change escape sections) have to be surrounded by `\[...\]` so bash can tell they don't take up space on the screen. Looks to me like it should be `PS1='\[\e[0;32m\]\w\$ \[\e[0m\]'`. See [this question](https://stackoverflow.com/questions/19092488/custom-bash-prompt-is-overwriting-itself). – Gordon Davisson Jun 25 '21 at 08:34

1 Answers1

0

Thanks to the hints in the comments I found this question which has fixed my issue. The answer was to wrap the colour sections in \[ and \]. Thanks all!

CheChe
  • 64
  • 7