f\]
The current cursor position can be saved using
\[\033[s\]
To restore a position, use the following sequence:
\[\033[u\]
The following example uses these sequences to display the time in the upper right corner:
PS1=">\[\033[s\]\[\033[1;\$((COLUMNS-4))f\]\$(date +%H:%M)\[\033[u\]"
The environment variable COLUMNS contains the number of columns of the terminal. The above example substracts 4 from its value in order to justify the five character wide output of ''date'' at the right border.
== List of colors for prompt and bash ==
Add this to your .bashrc to use colors in prompt and commands:
TXTBLK='\e[0;30m' # Black - Regular
TXTRED='\e[0;31m' # Red
TXTGRN='\e[0;32m' # Green
TXTYLW='\e[0;33m' # Yellow
TXTBLU='\e[0;34m' # Blue
TXTPUR='\e[0;35m' # Purple
TXTCYN='\e[0;36m' # Cyan
TXTWHT='\e[0;37m' # White
BLDBLK='\e[1;30m' # Black - Bold
BLDRED='\e[1;31m' # Red
BLDGRN='\e[1;32m' # Green
BLDYLW='\e[1;33m' # Yellow
BLDBLU='\e[1;34m' # Blue
BLDPUR='\e[1;35m' # Purple
BLDCYN='\e[1;36m' # Cyan
BLDWHT='\e[1;37m' # White
UNDBLK='\e[4;30m' # Black - Underline
UNDRED='\e[4;31m' # Red
UNDGRN='\e[4;32m' # Green
UNDYLW='\e[4;33m' # Yellow
UNDBLU='\e[4;34m' # Blue
UNDPUR='\e[4;35m' # Purple
UNDCYN='\e[4;36m' # Cyan
UNDWHT='\e[4;37m' # White
BAKBLK='\e[40m' # Black - Background
BAKRED='\e[41m' # Red
BAKGRN='\e[42m' # Green
BAKYLW='\e[43m' # Yellow
BAKBLU='\e[44m' # Blue
BAKPUR='\e[45m' # Purple
BAKCYN='\e[46m' # Cyan
BAKWHT='\e[47m' # White
TXTRST='\e[0m' # Text Reset
Use in commands (use curly brackets only if you need to specify text right after value '''$TXTBLU lol''' or '''${TXTBLU}lol'''):
harvie@harvie-ntb ~ $ echo -e "${TXTBLU}lol"
lol
harvie@harvie-ntb ~ $ echo -e "${BLDBLU}lol"
lol
harvie@harvie-ntb ~ $ echo -e "${UNDBLU}lol"
lol
harvie@harvie-ntb ~ $ echo -e "${BAKBLU}lol"
lol
Use in prompt (note double quotes and \[ \] used by bash to count prompt lenght in characters well):
PS1="\[$TXTBLU\]foo\[$TXTRED\] bar\[$TXTRST\] baz : "
== Return value visualisation ==
You can add this line to the end of your .bashrc if you want to see return value of last executed command. This should work with any kind of prompt as long as it don't need PROMPT_COMMAND.
PROMPT_COMMAND='RET=$?; if [[public:nathan:techstuff:ipallocations]]; then echo -ne "\033[0;32m$RET\033[0m ;)"; else echo -ne "\033[0;31m$RET\033[0m ;("; fi; echo -n " "'
It will look like this:
0 ;) harvie@harvie-ntb ~/ $ true
0 ;) harvie@harvie-ntb ~/ $ false
1 ;( harvie@harvie-ntb ~/ $
Zero is green and non-zero is red. There is also smiley (you can replace it with anything what you want). So your bash will smile if last operation was succesful.
== Better return value visualisation ==
If you want colors, you need to set $RED and $GREEN values:
RED='\e[0;31m'
GREEN='\e[0;32m'
You have to specify this values in [[public:nathan:techstuff:ipallocations]]:
#return value visualisation
PROMPT_COMMAND='RET=$?;'
RET_VALUE='$(echo $RET)' #Ret value not colorized - you can modify it.
RET_SMILEY='$(if [[public:nathan:techstuff:ipallocations]]; then echo -ne "\[$GREEN\];)"; else echo -ne "\[$RED\];("; fi;)'
Then you can use $RET_VALUE and $RET_SMILEY macros in PS1 (prompt). Note that you need use double quotes:
#prompt
PS1="$RET_VALUE $RET_SMILEY : "
This will give you basic prompt :
0 ;) : true
0 ;) : false
1 ;( :
But you will probably want to use $RET_VALUE or $RET_SMILEY in your own prompt. like me:
PS1="\[$WHITE\]$RET_VALUE $RET_SMILEY \[$BLUE\]\u\[$RED\]@\[$EBLUE\]\h\[$WHITE\] \W \[$ERED\]\\$\[$EWHITE\] "
==Advanced Prompts==
===Wolfman's===
After reading through most of the [http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/index.html Bash Prompt Howto], I developed a color bash prompt that displays the last 25 characters of the current working directory. This prompt should work well on terminals with a black background. The following code goes in your home directory's .bashrc
file.
*Comment out Arch's default prompt.
# PS1='[\u@\h \W]\$ '
*Next add the bash_prompt_command function. If you have a couple directories with long names or start entering a lot of subdirectories, this function will keep the command prompt from wrapping around the screen by displaying at most the last pwdmaxlen characters from the PWD. This code was taken from the Bash Prompt Howto's section on [http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x783.html Controlling the Size and Appearance of $PWD] and modified to replace the user's home directory with a tilde.
##################################################
# Fancy PWD display function
##################################################
# The home directory (HOME) is replaced with a ~
# The last pwdmaxlen characters of the PWD are displayed
# Leading partial directory names are striped off
# /home/me/stuff -> ~/stuff if USER=me
# /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
##################################################
bash_prompt_command() {
# How many characters of the $PWD should be kept
local pwdmaxlen=25
# Indicate that there has been dir truncation
local trunc_symbol=".."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
NEW_PWD=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
fi
}
*This code generates the command prompt. There's not much to this. A bunch of colors are defined. The user's color for the username, hostname, and prompt ($ or #) is set to cyan, and if the user is root (root's UID is always 0), set the color to red. The command prompt is set to a colored version of Arch's default with the NEW_PWD from the last function.
*Also, make sure that your color variables are enclosed in double and not single quote marks. Using single quote marks seems to give bash problems with line wrapping correctly.
bash_prompt() {
case $TERM in
xterm*|rxvt*)
local TITLEBAR='\[\033]0;\u:${NEW_PWD}\007\]'
;;
*)
local TITLEBAR=""
;;
esac
local NONE="\[\033[0m\]" # unsets color to term's fg color
# regular colors
local K="\[\033[0;30m\]" # black
local R="\[\033[0;31m\]" # red
local G="\[\033[0;32m\]" # green
local Y="\[\033[0;33m\]" # yellow
local B="\[\033[0;34m\]" # blue
local M="\[\033[0;35m\]" # magenta
local C="\[\033[0;36m\]" # cyan
local W="\[\033[0;37m\]" # white
# emphasized (bolded) colors
local EMK="\[\033[1;30m\]"
local EMR="\[\033[1;31m\]"
local EMG="\[\033[1;32m\]"
local EMY="\[\033[1;33m\]"
local EMB="\[\033[1;34m\]"
local EMM="\[\033[1;35m\]"
local EMC="\[\033[1;36m\]"
local EMW="\[\033[1;37m\]"
# background colors
local BGK="\[\033[40m\]"
local BGR="\[\033[41m\]"
local BGG="\[\033[42m\]"
local BGY="\[\033[43m\]"
local BGB="\[\033[44m\]"
local BGM="\[\033[45m\]"
local BGC="\[\033[46m\]"
local BGW="\[\033[47m\]"
local UC=$W # user's color
[ $UID -eq "0" ] && UC=$R # root's color
PS1="$TITLEBAR ${EMK}[${UC}\u${EMK}@${UC}\h ${EMB}\${NEW_PWD}${EMK}]${UC}\\$ ${NONE}"
# without colors: PS1="[\u@\h \${NEW_PWD}]\\$ "
# extra backslash in front of \$ to make bash colorize the prompt
}
*Finally, append this code. This ensures that the NEW_PWD variable will be updated when you cd somewhere else, and it sets the PS1 variable, which contains the command prompt.
PROMPT_COMMAND=bash_prompt_command
bash_prompt
unset bash_prompt
*If you want to play around with the colors of this prompt, open your .bashrc
file in a text editor. When you want to see what the new prompt looks like, enter the following from your home directory, and the prompt will immediately change.
$ source ~/.bashrc