How can I show colored hostname on the prompt in Powerline-Plain [Bash-it] theme?

2.1k views Asked by At

I would like to show hostname at the beginning of the prompt in Powerline-Plain [Bash-it] theme.

Something like: MYHOSTNAME root /var/www

This is the default looks: root /var/www

and I'd like to assign to it a different random and fixed color for each different hostname.

3

There are 3 answers

0
KaMZaTa On BEST ANSWER

Just overwrite the POWERLINE_PROMPT variable in your .bashrc file and add hostname like this:

export POWERLINE_PROMPT=${POWERLINE_PROMPT:="hostname user_info scm python_venv ruby node cwd"}
0
Drasz On

\h is what you need to display the hostname in your bash prompt. In order for it to appear in a certain color, you need to wrap that part in the color codes you like. Like this you can define the color RED when you are root for example.

USERCOLOR='\[\e[1m\]'
NORMCOLOR='\[\e[m\]'
# If I am root, set the prompt to bright red
if [ ${UID} -eq 0 ]; then USERCOLOR='\[\e[1;31;7;47m\]'; fi
PS1="[$USERCOLOR\h$NORMCOLOR]\\n\#-\\$> "
0
nwinkler On

As outlined in the Bash-it Powerline documentation, you can use the hostname name to add the hostname to your Powerline prompt:

The contents of the prompt can be "reordered", all the "segments" (every piece of information) can take any place. The currently available segments are:

  • aws_profile - Show the current value of the AWS_PROFILE environment variable
  • battery - Battery information (you'll need to enable the battery plugin)
  • clock - Current time in HH:MM:SS format
  • cwd - Current working directory including full folder hierarchy (c.f. wd)
  • hostname - Host name of machine
  • in_vim - Show identifier if running in :terminal from vim
  • k8s_context - Show current kubernetes context
  • last_status - Exit status of last run command
  • python_venv - Python virtual environment information (virtualenv, venv and conda supported)
  • ruby - Current ruby version if using rvm
  • node - Current node version (only nvm is supported)
  • scm - Version control information, git
  • user_info - Current user
  • wd - Working directory, like cwd but doesn't show the full folder hierarchy, only the directory you're currently in.
  • shlvl - Show the current shell level (based on SHLVL environment variable), but only if you are not in root shell
  • dirstack - Show the current dirstack level (based on DIRSTACK environment variable), but only if the stack is not empty
  • history_number - Show current history number
  • command_number - Show current command number

A variable can be defined to set the order of the prompt segments:

POWERLINE_PROMPT="user_info scm python_venv ruby cwd"

Based on this, you can add the hostname segment to the POWERLINE_PROMPT variable (e.g. in your Bash profile):

export POWERLINE_PROMPT="hostname user_info scm python_venv ruby cwd"

This will add the hostname as the first entry in the prompt.

There currently is no functionality in Bash-it to assign a random colour to the prompt segments.