1

When I use the Ctrl+p key binding (I use the default emacs key bindings) on my command line, it sometimes works as expected, showing the previous command that I've run. I'm using the bash shell on Ubuntu 18.04.

However, sometimes it glitches: it'll hold a part of the previous line in the command line. This does not affect the next command I run, but it's still bugging me. How can I fix this?

enter image description here(I'm aware that this explanation doesn't make much sense, so here's a screenshot. I cannot backspace anymore on the last command (the touch file line, for some reason.) When I say "this does not affect the next command I run", here's what I mean: enter image description here

Edit: my PS1 reads: \e[1;32mKR$ \e[m

Froggos
  • 123
  • 1
  • 9
  • Type Ctrl-l to get bash to clear the screen and redraw what it thinks is the current state of the input. – icarus Dec 23 '19 at 18:11
  • @icarus but I'd like to look at my previous commands to have an idea of what I've run – Froggos Dec 23 '19 at 18:19
  • 2
    Sometimes this is due to \\[ and \\] being in the wrong place, or omitted, in your prompt string. Can you edit your question to include the output of `echo $PS1` ? – Mark Plotnick Dec 23 '19 at 18:20
  • @MarkPlotnick done! quick question, is there supposed to be a \] in the PS1? – Froggos Dec 23 '19 at 18:23
  • 1
    [Wrap non-printable characters in `\[` `\]`](https://superuser.com/a/1221598/432690). – Kamil Maciorowski Dec 23 '19 at 18:37
  • @KamilMaciorowski thanks! I tried that, does this look right for a PS1? `\[\e[1;32m\]KR$ \[\e[m\]` – Froggos Dec 23 '19 at 18:48
  • 1
    `\e[1;32m` is equivalent to key sequence `ESC1;32m` ... it sets text and background colors ... there is no closing bracket ... same with the `\e[m` ....... https://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/ – jsotola Dec 23 '19 at 19:09
  • 1
    This is https://unix.stackexchange.com/questions/317734 , https://unix.stackexchange.com/questions/446118 , et al. again – JdeBP Dec 23 '19 at 19:19

1 Answers1

1

Solution is to change the command prompt, the value of $PS1 by enclosing things that begin with \e in \[ and \]. This is from another answer.

I started out with the $PS1 being set to \e[1;32mKR$ \e[m.

The first solution I tried was to enclose the whole expression with those sequences, \[\e[1;32mKR$ \e[m\] but that didn't work, so I had to split this up into two different sections, one for each non-printable chunk. Hence, I was left with \[\e[1;32m\] and \[\e[m\] once I had added in those sequences .

The final solution was \[\e[1;32m\]KR$ \[\e[m\].

Froggos
  • 123
  • 1
  • 9