29

Right now my iTerm2 looks like this...

enter image description here

Everything is the same color and is hard to read. Can I make any of the Prompt, Command, and Output different colors like this?

enter image description here

5 Answers 5

21

There's a pretty exhaustive description of editing the bash prompt here there might be some linux specific stuff, but most of it is generic bash stuff.

But just for getting started, I recommend this bash profile generator

It is pretty simple, so I've since learned more and my prompt looks like: dwightk's bash prompt

using:

[[ -s "/Users/dwightk/.rvm/scripts/rvm" ]] && source "/Users/dwightk/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="🍔  \[\033[01;35m\]\u@\h:\[\033[01;34m\]\$(parse_git_branch) \[\033[01;32m\]\w \[\033[01;34m\]\n>\[\e[0m\]"

which is pretty useful when dealing with git. Everything before the export PS1= is what enables the git branch to appear.

The 🍔 is from my first foray into bash prompt editing after I read this blog post.

7
  • Hopefully someone with a better grasp of the actual editing of the prompt can post a more cogent answer.
    – dwightk
    Commented Mar 26, 2014 at 16:33
  • 2
    🍔 <- Is that char intented? Commented Mar 26, 2014 at 16:52
  • 1
    yeah, it's a leftover from my first expedition into bash prompt editing after reading: notes.torrez.org/2013/04/put-a-burger-in-your-shell.html I liked it so it stayed around.
    – dwightk
    Commented Mar 26, 2014 at 16:54
  • 1
    That git function is really nice. Will save be some git branch -a cmd's. Commented Mar 28, 2014 at 2:19
  • 1
    colored git output is nice too. thomashunter.name/blog/… Commented Mar 28, 2014 at 17:38
14

AFAIK there is no any UNIXy way to color anything else than the prompt.

But as you asked for an iTerm2 solution you are in luck! Use the Triggers feature to color text matching regexp.

For example if your prompts looks like this:

..then add in iTerm -> Preferences -> Profiles -> Advanced -> Triggers , [Edit] a Trigger like this:

..that will match the command part of each line, which will start to look like this:

Voilà - you have different colors for prompt, command and output! :)

5
  • This is simpler for most folks I bet so upvoted. But since the other one will work in any term, even remotely and it colors the git prompt as well, I'm going to keep it as the accepted answer. Commented Sep 8, 2016 at 20:06
  • What font do you use?
    – towry
    Commented Sep 25, 2016 at 1:33
  • @towry : Consolas Commented Sep 25, 2016 at 10:01
  • You could very easily put something like \e[95m at the end of your PS1 var and that would do it
    – Samie Bee
    Commented Mar 26, 2019 at 20:08
  • Thanks @SamyBencherif, but this will also color the output with the same color as command, which was not what OP asked for. Commented Mar 27, 2019 at 12:22
1

The iTerm solution works but there seems to be a slight delay in the coloring. If one stops typing for a second, the whole line is colored as expected.

1
  • That's true. I haven't found a better solution though. PS This should rather be a comment, not an answer. :) Commented Jun 28, 2020 at 9:21
0

Here's what worked for me. Try it and let me know.

http://dobsondev.com/2014/02/21/customizing-your-terminal/

The approach involves adding some instructions to your bash profile.

1
0

If you are using zsh you could do like this:

Add this code into your ~/.zshrc file

autoload -Uz vcs_info
precmd() { vcs_info }
precmd_functions+=( precmd_ves_info)
zstyle ':vcs_info:git:*' formats ':%b'
PROMPT='🍔 %F{040}%n%f at %F{166}%m%f in %F{031}${PWD/#$HOME/~}%f%F{magenta}${vcs_info_msg_0_}%f %# '

Then reload your profile

source ~/.zshrc

It should look like this: enter image description here

I like this example because it shows a bit of how you can use colour codes or colour names

You must log in to answer this question.

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