Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How to redirect output without escape sequences to a file

On May 8,  9:27pm, Thorsten Kampe wrote:
} I've modified my PS4 so it contains a little bit of colour[1].

I was going to suggest that you stop using the terminal escape
sequences directly and use the %F prompt escape instead:

    PS4='%F{cyan}%B+%b%f%1N[%i]%F{cyan}%B:%b%f '

Theoretically, these are only supposed to be output if the terminal
supports them, so I thought that setting TERM=dumb would then turn
off the coloring.  But no ... zsh goes for ANSI sequences if the
terminal definition doesn't specify, so something is always output.

} Is there a way to either strip the escape sequences when redirected to
} a file or to modify PS4 when stderr is redirected to a file?

So, the alternative is to do the stripping.  Instead of

    ... >& trace.log


    ... 2>>(col > trace.log) >&2

Yes, that's somewhat inconvenient.

You could also create a conditional prompt, for example:

    PS4='%(9v.+%1N[%i]:.%F{cyan}%B+%b%f%1N[%i]%F{cyan}%B:%b%f) '

which says that if $psvar[9] has a value, then print the prompt with
no coloring, otherwise use coloring.  Then when you want to turn off
color for a while, just assign psvar[9]=1, and when you're ready for
color again, delete it with psvar[9]=().  This assumes you're not
already using 9 elements of psvar in other prompts, of course.

Messages sorted by: Reverse Date, Date, Thread, Author