Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to migrate my bash prompt to zsh

Tags:

bash

prompt

zsh

As of some time now I have been very tempted to switch from bash to zsh. There is just one problem, I am very picky about my prompt. I use the terminal all day and it took me a long time to get my prompt to look and behave the way I want it to. By the way, I put it together from different sources and a bit of my own stuff and it may contain errors or not properly written code. All I want to know is if someone knows the code that I can paste into my zshrc that would give me the exact same prompt.

The two features that I really enjoy is the variable length working directory that gets adjusted if pwd is too long and the directory separator slashes having different color then the dir names. This prompt also sets a dynamic terminal title.

I could not post a picture because of some reputation or something but here is the code.

my_prompt()
{       
    local NONE="\[\033[0m\]"
    local COLOR1="\[\033[0;30m\]"
    local COLOR2="\[\033[0;31m\]"
    local COLOR3="\[\033[0;32m\]"
    local COLOR4="\[\033[0;33m\]"
    local COLOR5="\[\033[0;34m\]"
    local COLOR6="\[\033[0;35m\]"
    local COLOR7="\[\033[0;36m\]"
    local COLOR8="\[\033[0;37m\]"
    local COLOR9="\[\033[1;30m\]"
    local COLOR10="\[\033[1;31m\]"
    local COLOR11="\[\033[1;32m\]"
    local COLOR12="\[\033[1;33m\]"
    local COLOR13="\[\033[1;34m\]"
    local COLOR14="\[\033[1;35m\]"
    local COLOR15="\[\033[1;36m\]"
    local COLOR16="\[\033[1;37m\]"

    # How many characters of the $PWD should be kept
    local PWDLEN=55
    ## Indicate that there has been dir truncation
    local TRUNC=".."
    local DIR=${PWD##*/}
    PWDLEN=$(( ( PWDLEN < ${#DIR} ) ? ${#DIR} : PWDLEN ))
    TITLE_PWD=${PWD/#$HOME/\~/}
    local pwdoffset=$(( ${#TITLE_PWD} - PWDLEN ))
    if [ ${pwdoffset} -gt "0" ]
    then
        TITLE_PWD=${TITLE_PWD:$pwdoffset:$PWDLEN}
        TITLE_PWD=${TRUNC}/${TITLE_PWD#*/}
    fi  

    local DIR_SEP_COLOR=$COLOR10
    local DIR_COLOR=$COLOR5
    local HOSTNAME_COLOR=$COLOR5
    local AT_COLOR=$COLOR10
    local USER_COLOR=$COLOR5

    IN=$TITLE_PWD
    arr=$(echo $IN | tr "/" "\n")

    unset NEWDIR
    for x in $arr
    do  
        if [ "$x" == "~" ]
        then
                NEWDIR="$NEWDIR$DIR_COLOR$x"
        else
                NEWDIR="$NEWDIR$DIR_SEP_COLOR/$DIR_COLOR$x"
        fi                                                        
    done


    TITLEBAR='\[\033]0;\u@\h:${TITLE_PWD}\007\]'
  MYPS1="${USER_COLOR}\u${AT_COLOR}@${HOSTNAME_COLOR}${HOSTNAME}$DIR_SEP_COLOR:${DIR_COLOR}${NEWDIR}${NONE}"

    PS1="${TITLEBAR}${MYPS1}${COLOR12}»${NONE} "
}
PROMPT_COMMAND=my_prompt

another thing is that I do not like to do something like

echo \`pwd` | grep "/"

to get the slashes different color because I would also like to be able to change the color of the directory names as well

EDIT and ANSWER:

Thank you Simont for your answer. I think your criticism of me not being able to do a search was the exact slap and motivation I needed to get started :) basically I used link number 2 multi color path in prompt

to get started. I came up with the following, its not perfect (i.e. random colors ... ) but it is a good starting template. The following is my current .zshrc :

prompt_working_dir() {

   # How many characters of the $PWD should be kept
   local PWDLEN=55
   ## Indicate that there has been dir truncation
   local TRUNC=".."
   local DIR=${PWD##*/}
   local PWDLEN=$(( ( PWDLEN < ${#DIR} ) ? ${#DIR} : PWDLEN ))
   local TITLE_PWD=${PWD/#$HOME/\~/}
   local pwdoffset=$(( ${#TITLE_PWD} - PWDLEN ))
   if [ ${pwdoffset} -gt "0" ]
   then
      TITLE_PWD=${TITLE_PWD:$pwdoffset:$PWDLEN}
      TITLE_PWD=${TRUNC}/${TITLE_PWD#*/}
   fi  

   IN=$TITLE_PWD
   arr=(${(s:/:)IN})
   unset NEWDIR

   if [ "$arr[1]" "==" "~" ]
   then
      NEWDIR="%{$fg[blue]%}$arr[1]"
      #delete 1st element
      arr[1]=()
      for x in $arr
      do
         NEWDIR="${NEWDIR}%{$fg_bold[cyan]%}/%{$reset_color%}%{$fg[blue]%}$x"
      done
   elif [ "$arr[1]" "==" ".." ]
   then
      NEWDIR="%{$fg[blue]%}$x%{$fg_bold[cyan]%}/"
      #delete 1st element
      arr[1]=()
      for x in $arr
      do
         NEWDIR="${NEWDIR}%{$reset_color%}%{$fg[blue]%}$x%{$fg_bold[cyan]%}/"
      done
   else
      for x in $arr
      do
          NEWDIR="${NEWDIR}%{$fg_bold[cyan]%}/%{$reset_color%}%{$fg[blue]%}$x"
      done
   fi  

   echo "${NEWDIR}"

   unset PWDLEN
   unset TRUNC
   unset DIR
   unset PWDLEN
   unset TITLE_PWD                                                            
   unset pwdoffset 
   unset IN
   unset arr
}      


    setopt PROMPT_SUBST
    autoload -U colors && colors

    # set window title to user@host %directory-----------
    precmd () {print -Pn "\e]0;%n@%M: %~\a"}

    SEP=":"
    PROMPT='%{$fg[blue]%}%m%{$reset_color%}'\
    '%{$fg_bold[cyan]%}@%{$reset_color%}'\
    '%{$fg[blue]%}%n%{$reset_color%}'\
    '%{$fg_bold[cyan]%}$SEP%{$reset_color%}'\
    '$(prompt_working_dir)%{$reset_color%}'\
    '%{$fg_bold[cyan]%}»%{$reset_color%} '
like image 888
user2601466 Avatar asked Jul 20 '13 04:07

user2601466


People also ask

How do I change the shell prompt in zsh?

To customize the ZSH prompt, we need to modify the prompt= variable inside the . zshrc file. We can populate the prompt variable with various placeholders, which will alter how the ZSH prompt appears.

How do I change from bash to zsh in Linux?

The actual process for changing your default shell from Bash to ZSH is extremely easy. Just run chsh -s /bin/zsh . Note that you'll need to supply the correct path your ZSH binary which you can get with the which zsh command we used earlier. Click here for more information on the chsh command.

Do bash and zsh have the same commands?

Bash lacks syntax highlighting and auto-correction features. Zsh has syntax highlighting and auto-correction features. In bash keybinding is done using '. inputrc' and 'bind builtin'.

Should I replace bash with zsh?

Zsh is more interactive and customizable than Bash. Zsh has floating-point support that Bash does not possess. Hash data structures are supported in Zsh that are not present in Bash. The invocation features in Bash is better when comparing with Zsh.


1 Answers

  1. Use colors.
  2. For path truncation, look at this. Fairly detailed discussion on how his works (I've used something similar in my prompt).
  3. Color directory names differently: read this answer, which addresses something similar (I think: I'm not really sure what you're asking there).
  4. Change the terminal title: read this, and the link from (2).

You should also read the Stackoverflow Help Page, specifically the section linked. If you're having trouble with something, edit your question to contain what you've tried and what your issue is.

There are lots of questions on Stackoverflow, as well as Superuser: (Example), and Unix.SX (Example). Plenty to get you started.

like image 58
simont Avatar answered Sep 28 '22 06:09

simont