Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is my current prompt definition:

PS1=$'%F{063}%1~%f %(1v.%F{099}%1v %f.)%F{063}%%%f '
RPROMPT='$VIMODE %m'

and I'm working on integrating this. Basically I'm starting to find it very unreadable.

Is there any way that I can make it multiline in a way perhaps similar to what Perl can do with regex's (e.g., what /x mode does. Like m{ ... }x. the ... can be multiline in that)?

Something like this:

PS1=$'
   %F{063}%1 # format blue
      ~      # show current directory
   %f
   %(1v.%F{099}%1v %f.) # show git branch if git repo in purple
   %F{063}   # format blue
      %#     # % for user and # for root
   %f '
RPROMPT='$VIMODE %m'
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

With Zsh 4.3.11, you can use the Z parameter expansion flag to split a string value according to the normal shell parsing rules while discarding comments (C option to Z) and treating newlines as normal whitespace instead of replacing them with semicolons (n option to Z). You can then stitch the results back together (j::) and evaluate a level of quoting (Q) to let you quote whitespace and other problematic characters (like “bare” comment introducer characters):

PS1=${(j::Q)${(Z:Cn:):-$'
   %F{063}%1 # format blue
      ~      # show current directory
   %f" "
   %(1v.%F{099}%1v %f.) # show git branch if git repo in purple
   %F{063}   # format blue
      %#     # % for user and # for root
   %f" "
'}}

Note: This parsing mode seems to know that it should parse the whole %(v…) expression as a single word, so we do not have to protect the space embedded in the conditional value. However, we do need to protect the “top level” spaces (the ones that happen to come after %f) since those will otherwise be taken as a normal word separator. The final unquoting pass will process any quoting mechanism (i.e. \, '', "", $''), so you can pick what you use to protect special characters (e.g. “top level” spaces or comment introducers intended for the final value).


If you are not using 4.3.11, then you can use an array to let you intersperse comments with the string elements. You will probably have to use more quoting than the with the Z parameter expansion flag, but the result may still be tolerable.

ps1_arr=(
   %F{063}%1 # format blue
      \~     # show current directory
   %f' '
   '%(1v.%F{099}%1v %f.)' # show git branch if git repo in purple
   %F{063}   # format blue
      %\#    # % for user and # for root
   %f' '
)
PS1=${(j::)ps1_arr}

Some notes on the quoting:

  • You can avoid quoting the ~ if you say %1~ instead of splitting it (it is %~ with an argument of 1, after all).
  • I quoted the whole %(v…) word, but only the parentheses and the space need protection.
  • You only need to quote the # in %# if you have EXTENDED_GLOB enabled.
  • The spaces that happen to come after %f need some kind of quoting. You can use a backslash, but it might look like a line continuation if you do not have “visible whitespace” in your editor.
share|improve this answer
    
This looks interesting... I will have to peruse this more later. –  xenoterracide May 20 '11 at 11:38
add comment

You could do something like:

PS1='%F{063}%1' # format blue
PS1=$PS1'~'     # show current directory
PS1=$PS1'%f'
PS1=$PS1'%(1v.%F{099}%1v %f.)' # show git branch if git repo in purple

etc.?

share|improve this answer
1  
yeah I was hoping for something a little bit cleaner... I could just have FOO='...' BAR='...' then PS1="$FOO $BAR" or some such. –  xenoterracide Aug 23 '10 at 9:27
    
@xenoterracide I really think the way you describe here is the best way to do it. A few clearly defined variables on their own lines, then string them together when defining $PS1. I'm not sure how you could get more readable than that. –  Sandy Aug 25 '10 at 2:15
    
@Sandy can't blame a guy for hoping for some sort of switch like perls regex switch. –  xenoterracide Aug 25 '10 at 19:59
    
Slightly "cleaner" (at least for bash>=3.1) is: PS1+='%f' # comment –  Peter.O May 18 '11 at 13:40
add comment

since you're probably wanting a quick answer here's a solution

PS1=$'\
%F{063}%1\
~\
%f\
%(1v.%F{099}%1v %f.)\
%F{063}\
%#\
%f '

but please note that you can not comment inside a string

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.