Skip to content

Stop characters from being wrapped to the beginning of the line #16724

Answered by DHowett
Casper-Guo asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks!

Alright, so, one of the important things in a bash prompt is to enclose the non-printable escape sequences in \[ and \]. That way, bash doesn't consider them when it measures how long the prompt is.

Try this out! It may help, or it may (depending on how well I remember bash...) break terribly.

function colorize_prompt {
    if [ $? -ne 0 ]; then
        printf "\e[31m"
    else
        printf "\e[32m"
    fi
}

function git_branch {
    git status 2>/dev/null | grep 'On branch' | sed 's/On branch / => /'
}

export PS1="\[\e[36m\][\d \t]\[\e[m\] \[\e[32m\]\u\[\e[m\e[33m\]@\[\e[m\e[31m\]\h:\[\e[m\e[36m\]\w\[\e[m\] \[\$(colorize_prompt)\]$\[\em\e[33m\]\$(git_branch)\[\e[m\] "

Here's …

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Casper-Guo
Comment options

@DHowett
Comment options

Answer selected by Casper-Guo
@Casper-Guo
Comment options

@DHowett
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants