2

Unlike other terminal emulators with kitty terminals I have no colors on bash prompt. I am using ubuntu, my PS1 is \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

What have I to setup for this?

Thanks

4
  • You start by checking if your terminal can manage ANSI escape sequences. Test with this command line: echo -e "default text \0033[31m red text \0033[0m default text"; If not, is there a way to modify the settings of the terminal?
    – sudodus
    Commented Jun 14, 2022 at 18:32
  • It supports it, with your test I read red text in red, Another example: if I execute ls -la I get colored output in file lists. It's just the prompt that is not colored as in other terminal emulator (gnome terminal, terminator, etc)
    – res1
    Commented Jun 14, 2022 at 18:41
  • Try with the following PS1 \[\033[01;31m\]$(statstring)\[\033[00m\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\] \$; statstring is defined as a function in my .bashrc, I don't know if you have it.
    – sudodus
    Commented Jun 14, 2022 at 18:43
  • it works, the prompt is colored now, can you explain it? maybe in an answer (I will accept it). I got the statstring warning/error but I read your edit, it is not a problem, I removed it from PS1
    – res1
    Commented Jun 14, 2022 at 18:48

2 Answers 2

3

Kitty may not be recognised as a color terminal... What is the $TERM environment variable set to?

In gnome-terminal it says:

$ echo $TERM
xterm-256color

In kitty it says:

$ echo $TERM
xterm-kitty

Check your .bashrc for this line:

xterm-color|*-256color) color_prompt=yes;;

Add xterm-kitty to it, like this:

xterm-color|*-256color|xterm-kitty) color_prompt=yes;;

Now restart kitty.

1

I think it will work with the following code in your ~/.bashrc

I don't know if you need to tamper with the variable TERM, but it is there in my ~/.bashrc. I borrowed it years ago, and don't know/remember what is actually necessary.

if [ "${TERM:0:5}" == "xterm" ]
then
 typeset TERM=xterm-color  # force colour prompt
fi

function statstring {
RC=$?
  if [ "0" != $RC ]; then
    printf "[$RC] "
  fi
}
case "$TERM" in
xterm-color)
#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

    if [ "$USER" = root ]; then
        PS1='\[\033[01;31m\]$(statstring)\[\033[00m\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\] \$ '
    else
        PS1='\[\033[01;31m\]$(statstring)\[\033[00m\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\] \$ '
    fi
    ;;
*)
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    ;;
esac

if [ "$TERM" == "xterm-color" ]
then
 typeset TERM=xterm  # force basic prompt
fi

Explanation: ANSI escape sequences in PC computers start with ESC [ and in echo statements we can use \0033 (ASCII: 3*8+3 = 27 for ESC). See this link for a detailed description of ANSI sequences.

There is also an 'error code message' when a command return with an error code. This is controlled by the function statstring.

enter image description here

5
  • It is still not clear why it needs this "tricks" to have color bash prompt unlike other terminal emulators.
    – res1
    Commented Jun 14, 2022 at 19:21
  • 1
    This is a workaround with some extra features. I don't know your Kitty terminal and why it works differently from your other terminal emulators. Maybe it activates another alternative from your .bashrc file depending on the TERM setting or something like that.
    – sudodus
    Commented Jun 14, 2022 at 19:30
  • 4
    I found the issue: my default .bashrc has this line: case "$TERM" in xterm-color|*-256color) color_prompt=yes;; esac (no newlines because is a comment). Kitty terminal is identified as xterm-kitty so I have to change the condition with xterm-color|*-256color|xterm-kitty) and it works.
    – res1
    Commented Jun 14, 2022 at 19:33
  • I'm glad you found the cause of your problem. You did it yourself after a short discussion with me :-)
    – sudodus
    Commented Jun 14, 2022 at 19:36
  • Yes, your last comment helped me to where to look for it, thanks :)
    – res1
    Commented Jun 14, 2022 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .