How to redirect my bash request to zsh

For some time I really wanted to switch from bash to zsh. There is only one problem, I am very picky about my prompt. I use the terminal all day, and it took me a long time to get a hint look and behave the way I want it. By the way, I put it together from various sources and some of my own things, and this may contain errors or incorrectly written code. All I want to know is if someone knows a code that I can paste into my zshrc that would give me exactly the same invitation.

Two functions that I really like are a variable-length working directory that adjusts if pwd is too long, and the directory separator is a different color and then the names. This prompt also sets the name of the dynamic terminal.

I could not post a photo because of some kind of 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 slashes of a different color, because I would also like to change the color of directory names as well

EDIT AND ANSWER:

Thanks Simont for your reply. I think your criticism is that I am not able to do the search - this is what I need to start with: basically I used the link number 2 the multi-color path in the tooltip

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%} ' 
+4
source share
1 answer

Notes:

You must first search the web to configure zsh . This is pretty easy, and your question betrays the inability to do a quick search before launching here ...

Secondly, your question ... is illogical. You said:

... I am very picky about my prompt ... it took me a long time to make my invitation look and behave the way I want it.

You set up a bash prompt that took a lot of time - why do you expect someone to have the identical zsh prompt?

Answer:

  • Use colors .
  • To truncate the path, view this . A fairly detailed discussion of how it works (I used something similar in my tooltip).
  • The names of the color directories are different: read this answer that talks about something similar (I think: I'm not quite sure what you are asking there).
  • Change the name of the terminal: read this and the link from (2).

You should also read the Stackoverflow help page , in particular the related section. If you have problems with something, edit your question to contain what you tried and what your problem is .

There are many questions about Stackoverflow as well as Superuser: ( Example ) and Unix.SX ( Example ). A lot to get you started.

0
source

Source: https://habr.com/ru/post/1492424/


All Articles