You are not logged in.

#1 2009-11-09 03:31:35

Zer0
Member
From: Windsor, ON, Canada
Registered: 2006-08-25
Posts: 299

zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

Story:
Quite awhile back when I discovered Phil!s ZSH Prompt via one of the Screenshots threads. I wanted a prompt like that but I don't use ZSH, I'm a bash user.  So I looked for something with similar options and looks for bash without any luck. I tried Bashish but I felt that there was too much extra going on for what I wanted.  You know what they say!  Want something done right, do it yourself!

So I created zer0prompt for myself, used it for a couple years and now I feel like sharing it big_smile . It has some features from Phil!'s prompt, similar looks to Bashish's bluesteel theme,
I just finished going over the code for public release. I cleaned up the code, added some features, a few user options and this is the result...

Screenshots:
contntrunc.th.png titlebarngfx.th.png rootj.th.png colourthemes.th.png

Features:
- a TERMWIDE prompt, fills the width of the terminal no matter how wide.
- 2 line prompt
- following info is always shown = user, host, current tty, working directory, time, user identifier.
- truncates the shown working directory when it's wider then the terminal.
- shows /home/user as ~
- continuation prompt.
- when logged in as root user, display info in red (visual reminder).
- displays the exit code when a command fails.
- sets xterm/rxvt terminal titles (shows username, WIDTHxLENGTH, and directory)
- user selectable = colour themes, info colours, time format, and line graphics.
- user can manually set "no colour mode" and/or "safe line graphics" mode
- very easy to install without making a mess of your .bashrc
- tries not to pollute your bash environment wink

Usage:
1) save the code in the next post as file .zer0prompt inside your home directory
2) edit your .bashrc , comment out the PS1 line, and add the following

source ~/.zer0prompt
zer0prompt
unset zer0prompt

3) repeat for root if you want to display the info in red when you su to root.

Other:
Questions, Comments, Suggestions?    Please reply!

*EDIT*

Current project can be found at the following sites. Many new features and fixes!
- My personal website
- GitHub

Last edited by Zer0 (2015-06-14 16:24:18)

Offline

#2 2009-11-09 03:32:30

Zer0
Member
From: Windsor, ON, Canada
Registered: 2006-08-25
Posts: 299

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

.zer0prompt code:

#!/bin/bash
#
#  zer0prompt
#    By: Wes Brewer [zer0]
#    Last updated: Nov 8, 2009
#
#  Credit for ideas/info: Phil!'s ZSH Prompt, Bashish, TERMWIDE prompt
#                         Bash Prompt Howto
#
#  Usage: Add the follwing lines to your ~/.bashrc file
#    source ~/.zer0prompt
#    zer0prompt
#    unset zer0prompt

#### user config ####

## set colour theme
# options -- cyan, blue, green, red, purple, yellow, black, white, none
zpcl="cyan"

## set info colours
# colour ref -- http://www.gilesorr.com/bashprompt/howto/c333.html#AEN335
zi1="\[\033[1;32m\]"  # user@host:tty 
zi2="\[\033[1;35m\]"  # current path
zi3="\[\033[1;33m\]"  # time
zi4="\[\033[1;31m\]"  # exit status
zi5="\[\033[1;32m\]"  # user identifier ($ or #) 

## set time format
# options -- 12, 24, 12s, 24s
zptm="12"

## set line graphics to use
zg1="─"; zg2="┌"; zg3="└"; zg4="┤"; zg5="├"; zg6=">"; zg7="|"
#zg1="-"; zg2="+"; zg3="+"; zg4="|"; zg5="|"; zg6=">"; zg7="|"


#### code ####
# Use bash builtin checkwinsize option for terminals which fail to properly
# set the $COLUMNS variable. (bug workaround)
shopt -s checkwinsize

# if root user then colour user@host info and user identifier red.
[ "${UID}" = 0 ] && zi1="\[\033[1;31m\]" && zi5="\[\033[1;31m\]"

# This function is run at every prompt update, keeping our variables updated.
# Bash's PROMPT_COMMAND option handles this (see end of this function).
function pre_prompt {
  # show exit code of last failed command
  ZEXIT="${?}"
  [ "$ZEXIT" = "0" ] && ZEXIT=""

  ZPWD=${PWD/#$HOME/\~}  # sorten home dir to ~

  # set length of our important info
  local infolength="$(whoami)@$(hostname):$(basename $(tty))$ZPWD"
  # set length of our graphics
  local gfxlength=23

  # construct ZFILL size to fill terminal width (minus info/gfx lengths).
  local fillsize
  let fillsize=${COLUMNS}-${gfxlength}-${#infolength}
  ZFILL=""
  while [ "$fillsize" -gt "0" ]; do
    ZFILL="$ZFILL$zg1"
    let fillsize=${fillsize}-1
  done

  # determine how much to truncate ZPWD, if ZFILL can't shrink anymore.
  if [ "$fillsize" -lt "0" ]; then
    local cut=3-${fillsize}  # some tricky math, 3-(-number)=+number
    ZPWD="...${ZPWD:${cut}}"
  fi
}
PROMPT_COMMAND=pre_prompt

# This function tells bash how to draw our prompt
function zer0prompt {
  local zc0="\[\033[0m\]"     # clear all colors
  local zc1="\[\033[1;37m\]"  
  local zc2="\[\033[0;37m\]"

  # set colour theme
  if [ "$zpcl" = "cyan" ]; then
    local zc3="\[\033[1;36m\]"; local zc4="\[\033[0;36m\]"
  elif [ "$zpcl" = "blue" ]; then
    local zc3="\[\033[1;34m\]"; local zc4="\[\033[0;34m\]"
  elif [ "$zpcl" = "green" ]; then
    local zc3="\[\033[1;32m\]"; local zc4="\[\033[0;32m\]"
  elif [ "$zpcl" = "red" ]; then
    local zc3="\[\033[1;31m\]"; local zc4="\[\033[0;31m\]"
  elif [ "$zpcl" = "purple" ]; then
    local zc3="\[\033[1;35m\]"; local zc4="\[\033[0;35m\]"
  elif [ "$zpcl" = "yellow" ]; then
    local zc3="\[\033[1;33m\]"; local zc4="\[\033[0;33m\]"
  elif [ "$zpcl" = "black" ]; then
    local zc3="\[\033[1;30m\]"; local zc4="\[\033[0;30m\]"
   elif [ "$zpcl" = "white" ]; then
    local zc3="\[\033[1;37m\]"; local zc4="\[\033[0;37m\]"
  else  # no colour
    local zc3=""; local zc4=""; local zc1=""; local zc2=""
    zi1=""; zi2=""; zi3=""; zi4=""; zi5=""
  fi

  # set time format
  if [ "$zptm" = "24" ]; then
    local ZTIME="\A"
  elif [ "$zptm" = "12s" ]; then
    local ZTIME="\T"
  elif [ "$zptm" = "24s" ]; then
    local ZTIME="\t"
  else    
    local ZTIME="\@"
  fi

  # set titlebar info if xterm/rxvt
  case $TERM in
    xterm*|rxvt*)
      local TITLEBAR='\[\033]0;\u (\w) [${COLUMNS}x${LINES}]\007\]';;
    *)
      local TITLEBAR="";;
  esac

# prompt
PS1="${TITLEBAR}\
$zc1$zg2$zg1$zc3$zg1$zc4$zg1$zg4$zi1\u@\h:\l$zc4$zg5$zg1$zc2$zg1$zg1$zc4\
\$ZFILL$zc3$zg1$zg1$zg1$zg1$zc1$zg1$zg1$zg1$zc3$zg1$zg1$zc4$zg1$zg4$zi2\
\$ZPWD$zc4$zg5$zg1$zc2$zg1
$zc3$zg3$zc4$zg1$zg4$zi3$ZTIME$zc4$zg7$zi5\\\$$zc4$zg5$zi4\
\$ZEXIT$zc2$zg1$zc3$zg6$zc0 "

# continuation prompt
PS2="$zc3$zg3$zc4$zg1$zg4$zi5\\\$$zc4$zg5$zc2$zg1$zc3$zg6$zc0 "
}

Last edited by Zer0 (2009-11-09 03:33:31)

Offline

#3 2009-11-09 18:36:32

markg85
Member
Registered: 2009-06-27
Posts: 130

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

Thanx a lot for sharing smile

Offline

#4 2009-11-11 07:26:00

PingPing
Member
From: /dev/null
Registered: 2009-07-24
Posts: 39
Website

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

Saw this on November screenies.  Great work and thanks for sharing it. smile

Offline

#5 2010-01-14 03:09:23

tidalwav1
Member
From: USA
Registered: 2007-05-20
Posts: 12
Website

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

This appears to work great...

...as long as it's not running inside tmux (similar to GNU Screen)! :'(

Screenshot outside tmux:

zer0notmux.png

Screenshot inside tmux:

zer0tmux.png

You can see in the second screenshot where the commands/cursor appear in relationship to the prompt.

If I detatch from and reattach to tmux, the cursor jumps around/switches positions sometimes, but never displays in the correct place.

This is obviously a very sweet Bash prompt and I appreciate that you've made it available, but...any suggestions/thoughts for getting it working inside tmux?

Thanks!

Offline

#6 2010-06-13 19:09:48

splippity
Member
Registered: 2010-05-25
Posts: 144

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

call me wierd but I dont understand how to get it to work for root........ followed instructions and all is fine except root doesnt show properly thanks........
pretty sweet.

sorry.......
more specifically if I do it then yes it turns red but I lose the color for things like folders and what not if say I do su and then password then "ls" everythings white
instead of blue for folders and what not.

thanks

Last edited by splippity (2010-06-13 19:14:12)

Offline

#7 2010-06-30 20:45:26

dUc0N
Member
Registered: 2010-06-30
Posts: 1

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

splippity wrote:

call me wierd but I dont understand how to get it to work for root........ followed instructions and all is fine except root doesnt show properly thanks........
pretty sweet.

sorry.......
more specifically if I do it then yes it turns red but I lose the color for things like folders and what not if say I do su and then password then "ls" everythings white
instead of blue for folders and what not.

thanks

Add this to your ~/.bashrc:
alias ls='ls --color=auto'

Offline

#8 2010-07-01 18:49:48

splippity
Member
Registered: 2010-05-25
Posts: 144

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

your the bomb.........

Offline

#9 2013-07-09 23:22:00

splippity
Member
Registered: 2010-05-25
Posts: 144

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

I know this is a really old post but Im experiencing an issue with a new install using this script.

?????jricide@jricide:4???????????????????????????????????????????????????????????????~???

as you can see question marks instead of -------

so how might I fix that?


thanks

Offline

#10 2013-07-09 23:35:04

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,412

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

It works for me in bash... did you follow the instructions?

Offline

#11 2013-07-10 03:13:20

splippity
Member
Registered: 2010-05-25
Posts: 144

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

yes I copy and pasted the entire script into a file and then linked it


shows up the same no matter what I use it in: konsole, xterm or whatever

always has the ?????????? instead of -------------

Last edited by splippity (2013-07-10 03:15:43)

Offline

#12 2013-07-10 03:27:22

splippity
Member
Registered: 2010-05-25
Posts: 144

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

ok I was looking at the script and it looks like they used the following symbols and I have no idea what I would need to install to get bash to recognize them:
## set line graphics to use
zg1="─"; zg2="┌"; zg3="└"; zg4="┤"; zg5="├"; zg6=">"; zg7="|"
#zg1="-"; zg2="+"; zg3="+"; zg4="|"; zg5="|"; zg6=">"; zg7="|"

any help would be appreciated thanks

Offline

#13 2013-07-10 03:32:35

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,765
Website

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

They are unicode symbols: yout $TERM needs to support them and you also have to have your locale set correctly.

This isn't a support thtread, though so you should start a new thread with information about both of the above on your machine.


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#14 2015-06-14 16:22:09

Zer0
Member
From: Windsor, ON, Canada
Registered: 2006-08-25
Posts: 299

Re: zer0prompt - a Phil!'s ZSH Prompt alternative for BASH users

I hate to "bump" such an old forum topic but the zer0prompt project has seen many updates since my initial post. Given that there are some who still may use it or others who may be interested, zer0prompt can be found at the following sites.

- My personal website
- GitHub

Current Features:

  • 18 different colour themes

  • a TERMWIDE prompt, fills the width of the terminal no matter how wide.

  • 2 line prompt

  • the following info is always shown = user, host, current tty, working directory, time, user identifier

  • truncates the shown working directory when it’s wider then the terminal.

  • shows /home/user as a tilde ~

  • continuation prompt

  • when logged in as root user, display info in red as a visual reminder.

  • displays the exit code when a command fails

  • sets xterm/rxvt terminal titles (shows username, working directory, and terminal size)

  • user selectable colour themes, info colours, time format, and line graphics

  • auto detection of supported line graphics mode (standard or fallback)

  • user can manually set “no colour mode” and/or “graphics fallback mode”

  • very easy to install without making a mess of your .bashrc

  • tries not to pollute your bash environment!

Offline

Board footer

Powered by FluxBB