Incorrect line wrapping on konsole, yakuake etc

My bash prompt in ~/.bashrc is:

PS1=' \e[1;32m\u@\h\e[1;34m\W\e[1;33m\$ \e[m'

Of late, when my command line is long, instead of continuing to terminal width and wrapping around with a newline on to the next line, the text jumps back to the bash prompt on the current line, overwriting it, and then continues and wraps as it normally would.

I am also unable to use previous commands and change them for re-use because the cursor moves one line above the current one.

I thought it might be because of my prompt: so I have given it above.

Kindly suggest how to investigate further.

Add \[\e[0m\] to the end of your prompt. It resets the escape codes for colorizing and positioning the prompt back to the default. :wink:

Thus, your prompt would be defined like so… :arrow_down:

PS1='\e[1;32m\u@\h\e[1;34m\W\e[1;33m\$ \e[0m\]'
1 Like

Thank you @Aragorn.

I copied and pasted your prompt and it did go to the next line but them jumped back to the start of that line.

Nevertheless, you pointed out what I was missing, which was enclosing all non-printable colour codes in \[ and \] pairs. Once I did that, things are behaving normally.

For the record, my prompt now is:

PS1='\[\e[1;32m\]\u@\h\[\e[1;34m\]\W\[\e[1;33m\]\$ \[\e[0m\]'

1 Like

I finally managed my prompt to be readable and reconfigurable. Also, colorset is configurable (local, remote, virtual) and I set it differently for various machines.

2020-11-09_20-54

SHELL_TYPE='local'
# SHELL_TYPE='remote'
# SHELL_TYPE='virtual'

ESC=$(printf "\e")
FG_BLACK="\[$ESC[30m\]"
FG_RED="\[$ESC[31m\]"
FG_GREEN="\[$ESC[32m\]"
FG_YELLOW="\[$ESC[33m\]"
FG_BLUE="\[$ESC[34m\]"
FG_PURPLE="\[$ESC[35m\]"
FG_CYAN="\[$ESC[36m\]"
FG_WHITE="\[$ESC[37m\]"
BOLD="\[$ESC[1m\]"
DIM="\[$ESC[2m\]"
RESET="\[$ESC(B$ESC[m\]"

if [ "$USER" = "root" ]; then
    if [ $SHELL_TYPE = 'remote' ]; then
        COL_USER="${FG_RED}"
        COL_HOST=${FG_BLUE}
    elif [ $SHELL_TYPE = 'virtual' ]; then
        COL_USER="${FG_RED}"
        COL_HOST="${FG_YELLOW}${BOLD}"
    else
        COL_USER="${FG_RED}"
        COL_HOST=${FG_CYAN}
    fi
else
    if [ $SHELL_TYPE = 'remote' ]; then
        COL_USER=${FG_BLUE}
        COL_HOST=${FG_BLUE}
    elif [ $SHELL_TYPE = 'virtual' ]; then
        COL_USER="${FG_YELLOW}${BOLD}"
        COL_HOST="${FG_YELLOW}${BOLD}"
    else
        COL_USER=$FG_CYAN
        COL_HOST=$FG_CYAN
    fi
fi

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

PS1="${COL_USER}\u"  # username
PS1+="${COL_HOST}@\h${RESET}"  # host
PS1+="${FG_WHITE}${DIM}:[${RESET}"  # opening bracket
PS1+="${FG_BLUE}\w"  # directory
PS1+="${FG_WHITE}${DIM}]${RESET}"  # closing bracket
PS1+="${FG_RED}\$(parse_git_branch)"  # git branch
PS1+="${FG_WHITE}:${RESET} "  # closing colon
2 Likes

Wow! :star_struck:

Thanks.

That’s great, thanks! I’d been thinking of something along similar lines as I, too, use custom prompts … looks nicer & makes it easy to identify which machine(s) I’m logged in to.