You are not logged in.
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 . 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...
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
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
.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
Thanx a lot for sharing
Offline
Saw this on November screenies. Great work and thanks for sharing it.
Offline
This appears to work great...
...as long as it's not running inside tmux (similar to GNU Screen)! :'(
Screenshot outside tmux:
Screenshot inside tmux:
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
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
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
your the bomb.........
Offline
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
It works for me in bash... did you follow the instructions?
Offline
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
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
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.
Offline
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.
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