5

I've been looking at spicing up my Terminal experience by among other things, adding some color. I used the tput command to build a nicely looking prompt. This is part of my .bash_profile:

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BOLD=$(tput bold)
REVERSE=$(tput rev)
RESET=$(tput sgr0)

export PS1="${WHITE}$(date "+%H:%M") ${MAGENTA}\u${WHITE}@${MAGENTA}$(scutil --get ComputerName)${WHITE}:${MAGENTA}\w\n${GREEN}\$ ${REVERSE}"

Notice the ${REVERSE} (or $(tput rev)) at the end of the prompt, which makes the commands I type stand out by adding a background color.

enter image description here

My problem however is, that when executing any command, the style is not automatically reset upon pressing enter, which in this case adds an undesired green background to the command's output as well. How can I reset the terminal right before command execution?

enter image description here

3

What you're looking for in BASH is equivalant to what is built-in precmd in ZSH. This built-in function would be executed prior to executing a command. A work around is mentioned in https://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command. I hope it gives you a hint of how to solve your problem. The reset has to be called within this function.

  • Thank you, this led me to adding trap "tput sgr0" DEBUG, which amazingly enough seems to work. – Gerry Aug 10 '12 at 7:04
1

Try the reset command (available at /usr/bin/reset) or tput reset.

Adding ${RESET} at the end of the prompt seems to work.

  • Oh, you want to reset the terminal right before command execution but after typing the command. I don't know how to do that. – lhf Aug 10 '12 at 1:24

You must log in to answer this question.

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