Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There are a number of tutorials/posts online (e.g. http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/) about how to customize your prompt in bash by setting PS1. Is there a way to customize the color/bolding of the command I type?

In other words, as an example lets say that I wanted a green prompt, a bold blue command, and then output in whatever colors would be default. In the example below, I already know how to make "joe>" show as green, blue, bold, whatever by setting PS1. But is there a way I can I make git status show as bold for example?

joe> git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
...
share|improve this question
    
If you want a really good shell you should use zsh,what you want to achieve there would be very simple with preexec() { printf '\e[0m' } – bollovan Sep 18 '11 at 9:32
3  
@bollovan - 'Really good' is a bit subjective, I find it really detestable because of its bloat and POSIX noncompliance. But, whatever floats your boat. – Chris Down Sep 18 '11 at 12:38
    
@ChrisDown bloat floats your boat - never heard it, quite amusing. – n611x007 Mar 14 '13 at 13:14

You'd have to leave a colour trailing after the PS1 (start it after > in your example), and then use the bash DEBUG trap to clear the colour before your command was run (but after you press enter in your shell. Try something like this:

shopt -s extdebug
trap "tput sgr0" DEBUG
share|improve this answer
    
for someone stranger to escape codes: would this preserve the original coloring of the command (eg. would ls --color=always be colored)? – n611x007 Mar 14 '13 at 13:16
    
@naxa - Yes, as this explicitly sets colour codes (except for colours that are colored in the default terminal colour, which will only take effect after the first colour). – Chris Down Mar 15 '13 at 1:13
    
This trick seems to interfere with tab completion (prints weird things like ^[(B^[[m). It happens only on some systems, though: I tested this on two systems, both running Ubuntu 12.04 LTS, and only one of them had the interference. – musiphil Jul 15 '13 at 18:47

For completeness, here's how to set a prompt color and the command line input color in bash. Put these lines in your .bashrc.

PS1='\[\e[1;34m\]\w\$ \[\e[0;32m\]'
trap 'printf \\e[0m' DEBUG

The DEBUG trap, as suggested by Chris Down, is used to reset the text attributes. It's meant for debugging, but it's also useful to execute code before executing the command typed by the user.

In the prompt setting:

  • \[…\] is used around escape sequences to tell bash that they don't use up any screen estate (without them, the screen would become garbled when bash needs to redraw the prompt).
  • \e[…m where is a number, or more generally a sequence of numbers separated by semicolons, are escape sequences sent to the terminal to affect the color and other attributes of the following text. For example, 1;34 sets bold blue; 0;32 resets all attributes then switches the color to green. Adjust to taste.
  • \w\$ print the current directory and $ or #. You can of course change this for whatever you like, and insert text attribute changes in between if you like.
  • There's a final attribute change at the end, which sets the color of the text input.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.