Nxt3
Nxt3

Reputation: 2091

Backspace deletes prompt in terminal

In the following scenario, my bash prompt looks like this:

Username@Hostname ~ $

If I type any number of characters, and then press the delete button one-too-many times, it will clear the entire line. For example:

Username@Hostname ~ $ ls

then I delete s

Username@Hostname ~ $ l

then I delete l

|

I'm then left with my cursor without any sort of prompt. I can type up commands normally and everything seems to function fine--but why does the prompt disappear? Is there a way to stop this behavior?

Upvotes: 1

Views: 1150

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54623

If you are using escape sequences to color (or otherwise add content) to your prompt without marking the escapes as "non-printing", bash will lose track of the column, and result in behavior like this.

bash's manual suggests that you put nonprinting characters in your prompt within \[ and \] markers.

For the given example

PS1='\[\033[1;32m\u@\h\[\033[00m \[\033[1;34m\w $\[\033[00m '

a possible improved version would be

PS1='\[\033[1;32m\]\u@\h\[\033[00m\] \[\033[1;34m\]\w $\[\033[00m\] '

Upvotes: 5

Related Questions