Is it possible to set color for the user input in bashrc?

2

3

Note that I only want user's input to have specific color (words that user has typed with keyboard), not the actual command's output.

I tried:

export PS1="$ \033[38;5;154m"

I know that it should be closed with \033[m but I have no clue how to insert it after the input was sent to stdin.

export PS1="$ \033[38;5;154m$PS1\033[m"

This doesn't work either.

The reason why I want this is to be able to easily distinguish the input command from it's output. Thanks in advance.

Aleksandr Makov

Posted 2013-02-20T16:21:48.397

Reputation: 263

Answers

2

Bash won't be able to color all user input. UNIX is full-duplex and all data displayed comes from the unix machine, not from the local terminal emulator/keyboard so the terminal emulator (xterm?) can't really tell the difference, and bash only controls what it does, not what every program does.

I use a different colored prompt.

e=$'\e'
COLORblue="$e[0;34m"
COLORred="$e[0;31m"
COLORreset="$e[0m"
PC="$COLORblue"
if [[ "$EUID" -eq 0 ]]; then PC="$COLORred"; fi
PS1="\[$PC\]#===> \@ %\j / \u @ \h : \w/\n#===> \\\$ \[$COLORreset\]"

# Print when exit status is not 0 -- I like this too... shown in red
export PROMPT_COMMAND="x0=\$?;if ((\$x0 > 0)); then echo '${COLORred}# exit status = '\$x0' -- From PROMPT_COMMAND$COLORreset'; fi"

Maybe check
https://stackoverflow.com/questions/6841143/how-to-set-font-color-for-stdout-and-stderr

9mjb

Posted 2013-02-20T16:21:48.397

Reputation: 354

3

I've been working on this and a few other bash/terminal things all day, and figured I'd find any relevant questions and answer them if I could even be of any remote help -- so the below .bashrc will color the terminmal input command green and the output will be standard white. The background is still black.

# ~/.bashrc: executed by bash(1) for non-login shells.

# Created by EDH automated script to set up terminal colors
# Ref: http://linux.die.net/man/1/echo
# Ref: https://www.kirsle.net/wizards/ps1.html#help
# Ref: https://wiki.archlinux.org/index.php/Color_Bash_Prompt
# Ref: http://linux.die.net/man/1/bash

# Note: reload settings w/o relogin; source ~/.bashrc

# for terminal line coloring
export PS1="\[$(tput sgr0)\]\[$(tput setaf 1)\]\u \[$(tput setaf 6)\]\w \[$(tput setaf 1)\]\\$ \[$(tput setaf 2)\]"
none="$(tput sgr0)"
trap 'echo -ne "${none}"' DEBUG

# ls dir coloring
export LS_OPTIONS='--color=auto'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'

I tried to include the sources where I found them! Hope this helps, even if this is an older thread.

ehiller

Posted 2013-02-20T16:21:48.397

Reputation: 304

1

I don't think you can get a different color for input commands and their output. In any case, you certainly cannot do this by setting the PS1 variable. The PS1 is the prompt, the text displayed to the left of whatever you type in. For example:

enter image description here

You can change the default colors of the terminal by modifying your .Xdefaults and/or .Xresources file. Have a look here, here and here for some more information.

terdon

Posted 2013-02-20T16:21:48.397

Reputation: 45 216