# This prompt has the hostname and up to two levels of directory context. Just one level often doesn’t disambiguate, and more than two is often annoying. So a session might go:
# [mymachine ~]$ cd foo
# [mymachine ~/foo]$ cd bar
# [mymachine foo/bar]$ cd baz
# [mymachine bar/baz]$
# The actual code:
echo ‘($H)=($ENV{HOST}=~/([^\.]+ /);s|$ENV{HOME}|~|g;s|.+/([^/]+/[^/]+)$| ${1}|g;s|^|$H |;’ > $HOME/._sp
# Running it:
function sp () { PS1=”[`pwd | perl -p $HOME/._sp`]\$ “; }
# Install it into the prompt:
# precmd is a special tcsh alias executed before every command prompt.
# PROMPT_COMMAND is a special bash environment variable for the same purpose.
export PROMPT_COMMAND=sp
# Initialize it the first time:
sp
# alongside this, I’ve also implemented forward and back commands for directories as we are used to them in web browsers, so after the session at the beginning, I could say:
# [mymachine bar/baz]$ b
# [mymachine foo/bar]$ b
# [mymachine ~/foo]$ f
# [mymachine foo/bar]$ cd /usr/local/lib
# [mymachine local/lib]$ b
# [mymachine foo/bar]$
# If anyone is interested, I’m happy to post the code for that.
function cdd () { \cd $*; export PWDx=`perl $HOME/._cdd_add “$PWD”`; }
function b () { export PWDx=`perl $HOME/._cdd_back`; \cd “`perl $HOME/._cdd_curd`”; }
function f () { export PWDx=`perl $HOME/._cdd_forw`; \cd “`perl $HOME/._cdd_curd`”; }
alias cd=cdd
fi
#>
# I should mention that the idea and initial implementation for the two-level command prompt were from Jason A. Haynes for tcsh:
# “; if (“$hd:h” == “”) set prompt=”%m $pwd%# “; unset hd’
# alias sp ‘set pwd=`dirs`; set hd=$pwd:h; set prompt=”%m ..$hd:t/$pwd:t%
#Oh, I see, it wasn’t so much “cut off” as hidden by angle brackets.
# historical directory browsing
# Historical directory browsing adds `b’ and `f’ commands to your shell
# to let you go back and forward in your directory history like you
# go back and forward in your web page browsing history.
# Idea and original implementation from Deniz Yuret, rewritten by Gremio
if [ ! -n "$PWDx" ]; then
export PWDx=”1:`pwd`”
Perhaps it’s because when I’m at an actual prompt, I’m at home, not working, I find all the stuff about current directory, user, hostname, et cetera, just extraneous. So generally my prompt is just
PS1=’\$ ‘
If I need to know stuff, there’s always pwd, hostname, and whoami.
13 Comments
Mine is ever so slightly more complicated than yours:
PROMPT=$’%(!.%F{red}.%F{blue})%D{%a %R} %B[%m:%35<..<%~%<<] %n%#%b%f '
RPROMPT=$'%(!.%F{red}.%F{blue})<%(1j. %BJ%j%b.)%f'
if (( $SHLVL != ${ZSHLVL:=$SHLVL} ))
then
RPROMPT=$RPROMPT$' %F{magenta}Z'$(( $SHLVL – $ZSHLVL ))$'%f'
fi
export ZSHLVL
# Set colorful PS1 only on colorful terminals
NORMAL=”\[33[0;0m\]”
BOLD=”\[33[1m\]”
BLUE=”\[33[0;34m\]”
CYAN=”\[33[0;36m\]”
BRIGHT_CYAN=”\[33[1;36m\]”
DIM_CYAN=”\[33[2;36m\]”
WHITE=”\[33[1;37m\]”
PS1=”$BLUE[$CYAN\u@\h:\w$BLUE]\n$BLUE[$CYAN\t$BLUE]$BOLD\$$NORMAL “
PS1=”[\[33[32m\]\w]\[33[0m\]\n\[33[1;36m\]\u\[33[1;33m\] -> \[33[0m\]“
My PS1 is different depending on the environment.
In normal terminal session it is: \h:\w \u\$
But within Emacs (where most of my shell work is) it is simply: [\t] \$
# This prompt has the hostname and up to two levels of directory context. Just one level often doesn’t disambiguate, and more than two is often annoying. So a session might go:
# [mymachine ~]$ cd foo
# [mymachine ~/foo]$ cd bar
# [mymachine foo/bar]$ cd baz
# [mymachine bar/baz]$
# The actual code:
echo ‘($H)=($ENV{HOST}=~/([^\.]+ /);s|$ENV{HOME}|~|g;s|.+/([^/]+/[^/]+)$| ${1}|g;s|^|$H |;’ > $HOME/._sp
# Running it:
function sp () { PS1=”[`pwd | perl -p $HOME/._sp`]\$ “; }
# Install it into the prompt:
# precmd is a special tcsh alias executed before every command prompt.
# PROMPT_COMMAND is a special bash environment variable for the same purpose.
export PROMPT_COMMAND=sp
# Initialize it the first time:
sp
# alongside this, I’ve also implemented forward and back commands for directories as we are used to them in web browsers, so after the session at the beginning, I could say:
# [mymachine bar/baz]$ b
# [mymachine foo/bar]$ b
# [mymachine ~/foo]$ f
# [mymachine foo/bar]$ cd /usr/local/lib
# [mymachine local/lib]$ b
# [mymachine foo/bar]$
# If anyone is interested, I’m happy to post the code for that.
Gregory:
That would be nice. Thanks.
# $HOME/._cdd_add
echo ‘$_=$ENV{PWDx};split/:/;if($_[0]>1){$_[0]–};print(join(q{:},@_));’ > $HOME/._cdd_back
echo ‘$_=$ENV{PWDx};split/:/;print($_[$_[0]]);’ > $HOME/._cdd_curd
echo ‘$_=$ENV{PWDx};split/:/;if($_[0] $HOME/._cdd_forw
function cdd () { \cd $*; export PWDx=`perl $HOME/._cdd_add “$PWD”`; }
function b () { export PWDx=`perl $HOME/._cdd_back`; \cd “`perl $HOME/._cdd_curd`”; }
function f () { export PWDx=`perl $HOME/._cdd_forw`; \cd “`perl $HOME/._cdd_curd`”; }
alias cd=cdd
fi
#>
# I should mention that the idea and initial implementation for the two-level command prompt were from Jason A. Haynes for tcsh:
# “; if (“$hd:h” == “”) set prompt=”%m $pwd%# “; unset hd’
# alias sp ‘set pwd=`dirs`; set hd=$pwd:h; set prompt=”%m ..$hd:t/$pwd:t%
# Grem
# Ack! Part of the Historical Directory Browsing was cut off. Let me try again:
# $HOME/._cdd_add
echo ‘$_=$ENV{PWDx};split/:/;if($_[0]>1){$_[0]–};print(join(q{:},@_));’ > $HOME/._cdd_back
echo ‘$_=$ENV{PWDx};split/:/;print($_[$_[0]]);’ > $HOME/._cdd_curd
echo ‘$_=$ENV{PWDx};split/:/;if($_[0] $HOME/._cdd_forw
function cdd () { \cd $*; export PWDx=`perl $HOME/._cdd_add “$PWD”`; }
function b () { export PWDx=`perl $HOME/._cdd_back`; \cd “`perl $HOME/._cdd_curd`”; }
function f () { export PWDx=`perl $HOME/._cdd_forw`; \cd “`perl $HOME/._cdd_curd`”; }
alias cd=cdd
fi
#Oh, I see, it wasn’t so much “cut off” as hidden by angle brackets.
# historical directory browsing
# Historical directory browsing adds `b’ and `f’ commands to your shell
# to let you go back and forward in your directory history like you
# go back and forward in your web page browsing history.
# Idea and original implementation from Deniz Yuret, rewritten by Gremio
if [ ! -n "$PWDx" ]; then
export PWDx=”1:`pwd`”
echo ‘$_=$ENV{PWDx};split/:/;$_[++$_[0]]=shift;splice(@_,$_[0]+1);print(join(q{:},@_));’ > $HOME/._cdd_add
echo ‘$_=$ENV{PWDx};split/:/;if($_[0]>1){$_[0]–};print(join(q{:},@_));’ > $HOME/._cdd_back
echo ‘$_=$ENV{PWDx};split/:/;print($_[$_[0]]);’ > $HOME/._cdd_curd
echo ‘$_=$ENV{PWDx};split/:/;if($_[0] $HOME/._cdd_forw
function cdd () { \cd $*; export PWDx=`perl $HOME/._cdd_add “$PWD”`; }
function b () { export PWDx=`perl $HOME/._cdd_back`; \cd “`perl $HOME/._cdd_curd`”; }
function f () { export PWDx=`perl $HOME/._cdd_forw`; \cd “`perl $HOME/._cdd_curd`”; }
alias cd=cdd
fi
Found that bash on Solaris doesn’t like that PS1…. here is the updated version:
PS1=’\u@\h:\W>’
Perhaps it’s because when I’m at an actual prompt, I’m at home, not working, I find all the stuff about current directory, user, hostname, et cetera, just extraneous. So generally my prompt is just
PS1=’\$ ‘
If I need to know stuff, there’s always pwd, hostname, and whoami.
I do not claim any proper code in the following lines that I’ve had for ages…
%cat load.zsh
#!/bin/zsh
declare -i Up=”`uptime | awk -F: ‘{print $4}’ | cut -f1 -d’,'`”
if [ $Up -ge 2 ]
then
echo ‘red’
exit
else
if [ $Up -lt 1 ]
then
echo ‘green’
exit
else
echo ‘yellow’
exit
fi
fi
%cat prompt.zsh
autoload -U colors
colors
user_color=”cyan”
user_in_color=”white”
path_color=”green”
percent_color=”yellow”
date_color=”`zsh ~/.zsh/load.zsh`”
date_format=”%H:%M”
#start=”%U”
date=”%{$fg[$date_color]%}%D{$date_format}”
user=”%{$fg[$user_color]%}[%{$fg[$user_in_color]%}%n%{$fg[$user_color]%}]”
cpath=”%B%{$fg[$path_color]%}%~%b”
percent=”%{$fg[$percent_color]%}%%”
end=”%{$reset_color%}”
PS1=”$user$cpath $percent$end ”
RPS1=”$date$end”
PS1=”[\u@\h \W]\\$ “
Post a Comment