LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   What does your shell prompt look like? (https://www.linuxquestions.org/questions/linux-general-1/what-does-your-shell-prompt-look-like-115354/)

David the H. 03-27-2011 06:07 AM

A couple of years ago I posted a script that I created that will truncate the $PWD and format it for use in the prompt.

Since then I've modified it some more and converted it into a function, and I use it like this:
Code:


#Set some color escape sequences.
#================================#
export RESET='\e[0m' #disable any colors

# Regular colors.
export BLACK='\e[0;30m' RED='\e[0;31m' GREEN='\e[0;32m' YELLOW='\e[0;33m'
export BLUE='\e[0;34m' MAGENTA='\e[0;35m' CYAN='\e[0;36m' WHITE='\e[0;37m'

# Bolded colors.
export BBLACK='\e[1;30m' BRED='\e[1;31m' BGREEN='\e[1;32m' BYELLOW='\e[1;33m'
export BBLUE='\e[1;34m' BMAGENTA='\e[1;35m' BCYAN='\e[1;36m' BWHITE='\e[1;37m'

#=========================#
# Command prompt settings #
#=========================#

# Function for setting a nice variable-length shell prompt.
#==========================================================#
function variprompt {

# This function takes the $PWD or other input string and truncates it to a
# set percentage of the terminal width.  It's designed for use in the bash
# $PS1 prompt to keep it from taking up too much space.  There may be minor
# variations in the actual length due to rounding in the arithmetic.

# Set the desired width of the string below as the percentage of the total
# terminal width.  Remember that any other parts you add to the prompt (such as
# username) will take up additional space.  Also be aware that this script is
# designed for reasonable numbers, and may act in unpredictable ways if the
# target and/or terminal width are very small.  Target values under 25 are not
# recommended.  Will use $1 if it exists, or defaults to the set value.

TARGETPCT=${1:-40}

# Get the current directory or input string.  This will $2 if it exists, or else
# It will default to $PWD.  In addition, if the string includes the $HOME
# directory, it will truncate it to "~".  Simply comment out the second line if
# this behavior is unwanted and you always want the full path.

CURRENTDIR="${2:-$PWD}"
CURRENTDIR="${CURRENTDIR/#$HOME/~}"

# Be sure that the shopt option "checkwinsize" is enabled, so that the curent
# shell width (COLUMNS) is available to this function.  If the $COLUMNS variable
# is unavailable, the following line will set it to 100 instead.  Alternatively,
# you can manually set this variable to a fixed value of 100, and the output
# will always be TARGETPCT columns wide.

COLUMNS=${COLUMNS:-100}

# Determine the number of characters in the input string and calculate
# the total target length and the sizes of the starting and ending segments.

CURRENTLENGTH="${#CURRENTDIR}"
TARGETLENGTH="$(( COLUMNS * TARGETPCT / 100 ))"
FIRSTPART="$(( TARGETLENGTH / 3 - 3 ))"
LASTPART="$(( CURRENTLENGTH - ( TARGETLENGTH * 2/3 ) ))"

# Finally we compare the current length of the prompt to the target length
# and print the appropriate output string.

if (( CURRENTLENGTH <= TARGETLENGTH )); then

    # No modifications are necessary. Print the unmodified string.
    echo "$CURRENTDIR"

else

    # Very small target or column values can make "FIRSTPART" zero or less.
    # So test it to avoid throwing out an error, and set it to display only the
    # first two characters of the string in such situations. Otherwise, print
    # the desired truncated string.

    if (( FIRSTPART > 1 )); then
          echo "${CURRENTDIR:0:$FIRSTPART}...${CURRENTDIR:$LASTPART}"
    else
          echo "${CURRENTDIR:0:2}...${CURRENTDIR:$LASTPART}"
    fi

fi

}

# End function.
#==============#

# Set the prompt.
#================#

# Set checkwinsize option, it's needed to ensure COLUMNS variable is read properly.

shopt -s checkwinsize

# Pattern to set the window title portion of the prompt, if in an xterm.
# The formatting pattern goes between '\e]0;' and '\a' (\a = \007).

if [[ "$TERM" =~ xterm ]]; then
    XTITLE='\e]0;\u:$(variprompt 55 2>/dev/null)\a'
else
    unset XTITLE
fi

# Define PS1 for root and regular users.  Must unquote all the variables.
# Escape sequences must be enclosed in \[ and \] brackets.

if (( UID == 0 )); then
    export PS1='\['${XTITLE}$BRED'\]\u:[\['$MAGENTA'\]$(variprompt 2>/dev/null)\['$BRED'\]]##\['$RESET'\] '
else
    export PS1='\['${XTITLE}$BGREEN'\]\u:[\['$BBLUE'\]$(variprompt 2>/dev/null)\['$BGREEN'\]]$\['$RESET'\] '
fi

Yeah, it seems quite long, but it's mostly comments. :)
The result looks something like this:
Code:


david:[~]$ cd /mnt/musicdir/western_artists/Beatles--Albums/08_Sgt_Peppers_Lonely_Hearts_Club_Band/
david:[/mnt/musicdi...ppers_Lonely_Hearts_Club_Band]$

...but in color. Note also how the window title string can be set to a different length from the prompt itself.

Scruff 03-27-2011 01:25 PM

Whoa, haven't seen this thread in awhile. Mine now looks like:

[sean.bitwise: /home/sean]$

It also turns to red when a command fails, back to green under normal conditions.

PHP Code:

export PROMPT_COMMAND='PS1="\`if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\`[\u.\h: \`if [[ `pwd|wc -c|tr -d " "` > 18 ]]; then echo "\\W"; else echo "\\w"; fi\`]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"' 


bret381 03-30-2011 06:06 PM

mine looks like this:

┌─[18:04:17]──[bret]──[Ziggy]:~$
└──(28 files, 3.5Gb)>>

Code:

PS1="\[\e[01;37m\]┌─[\t]──[\[\e[01;37m\u\e[01;37m\]]──[\[\e[00;37m\]${HOSTNAME%%.*}\[\e[01;37m\]]:\w$\[\e[01;37m\]\n\[\e[01;37m\]└──\[\e[01;37m\](\[\e[32;1m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[01;37m\])>>\[\e[0m\]"

Aquarius_Girl 03-30-2011 08:15 PM

Quote:

Originally Posted by bret381 (Post 4308998)
mine looks like this:

┌─[18:04:17]──[bret]──[Ziggy]:~$
└──(28 files, 3.5Gb)>>

What is the way to type that special character there? The starting character I mean.

bret381 03-31-2011 08:30 AM

Quote:

What is the way to type that special character there? The starting character I mean.

I don't have a clue. haha, I used the archbang default prompt (which has that character) and added some stuff.

Aquarius_Girl 04-01-2011 12:15 AM

But there is a way to enter the special characters in the prompt, I know that, but how to type them?


All times are GMT -5. The time now is 05:33 PM.