2

When using the terminal, I want both my username and my hostname to have separate color schemes. Is that possible with Ubuntu's default terminal emulator(if not, is it possible in terminator?)?

muru
  • 197,895
  • 55
  • 485
  • 740
Mario Kamenjak
  • 1,043
  • 3
  • 16
  • 25
  • 3
    http://askubuntu.com/questions/13892/is-it-possible-to-color-the-prompt-in-bash and the link it connects to: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/ – Rinzwind Mar 06 '16 at 10:51

1 Answers1

3

You could wrap \h in, e.g., \[\033[01;31m\] and \[\033[00m\] in ~/.bashrc's $PS1 definition to make the host red; \[\033[00m\], which resets all the attributes, is already there in the default prompt, so you won't really need it, and I suggest commenting out $PS1's definition and adding a line right after rather than editing the current $PS1's definition for an easy rollback:

if [ "$color_prompt" = yes ]; then
    #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

This should work out-of-the-box on e.g. gnome-terminal; on xterm (and I'd guess in general on xterm-based terminals), you'll also have to uncomment this line:

#force_color_prompt=yes

You can check out other available colors e.g. here.

kos
  • 35,891
  • Does that work for the terminal application? – Mario Kamenjak Mar 06 '16 at 20:59
  • @MarioKamenjak It works for all terminals that support coloring through ANSI color escape sequences (for example the console, gnome-terminal and xterm, but most if not all xterm-based terminals will likely support that, though the palette could be more or less extensive). I suppose you're referring to Terminal (which is gnome-terminal), so if that's the case yeah, it will work for Terminal. – kos Mar 06 '16 at 21:26
  • I had to uncomment force_color_prompt first. Anyway, it works now. Here is my color scheme: PS1='${debian_chroot:+($debian_chroot)}[\033[01;33m]\u[\033[01;36m]@[\033[01;36m]\h[\033[00m]:[\033[00m]\w[\033[00m]$' – Mario Kamenjak Mar 07 '16 at 15:32
  • This helped as well: http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/ I will accept your answer now. – Mario Kamenjak Mar 07 '16 at 15:32
  • @MarioKamenjak Which terminal? It shouldn't be necessary in gnome-terminal / xterm (in fact my prompt is colored, but force_color_prompt=yes is still commented). – kos Mar 07 '16 at 15:42
  • @MarioKamenjak Ah I see, apparently it's actually required in xterm (and I'd guess also in xterm-based terminals?). Thanks, I'll add that to the answer. – kos Mar 07 '16 at 15:46