简体   繁体   English

如何将我的 bash 提示迁移到 zsh

[英]how to migrate my bash prompt to zsh

As of some time now I have been very tempted to switch from bash to zsh.一段时间以来,我一直很想从 bash 切换到 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.我只想知道是否有人知道我可以粘贴到我的 zshrc 中的代码,这会给我完全相同的提示。

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.我真正喜欢的两个功能是可变长度的工作目录,如果 pwd 太长和目录分隔符斜线的颜色与目录名称不同,则会调整该目录。 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我认为您对我无法进行搜索的批评是我开始所需的确切耳光和动力:) 基本上我在提示中使用了链接号 2 多色路径

to get started.开始。 I came up with the following, its not perfect (ie random colors ... ) but it is a good starting template.我想出了以下内容,它并不完美(即随机颜色......)但它是一个很好的起始模板。 The following is my current .zshrc :以下是我当前的 .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%} '
  1. Use colors . 使用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).更改终端标题:阅读此内容,以及 (2) 中的链接。

You should also read the Stackoverflow Help Page , specifically the section linked.您还应该阅读Stackoverflow 帮助页面,特别是链接的部分。 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 ). Stackoverflow 上有很多问题,还有 Superuser: ( Example ) 和 Unix.SX ( Example )。 Plenty to get you started.有很多东西可以让你开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM