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

Re: PS1='%?'



On Sun, Sep 30, 2007 at 03:14:27AM +1200, Atom Smasher wrote:
> if a command returns non-zero, i see the return status in my prompt. 
> sometimes that can be a distraction, and i'd like to "clear" it by hitting 
> <return>.

Here's (a very watered down version of) how I do that:

# Set up RPS1 to display $psvar[3], rather than $?
RPS1='%3v'

# When executing a command, set a flag saying that we want $? shown
function preexec {
  shownexterr=1
}

# Before drawing the prompt, set $psvar[3] to be "[$?]" or "",
# depending on whether the flag to show it is set and the return was
# non-zero.  Then, reset the flag, so the next time precmd runs the
# exit status won't be show.
function precmd {
  local exitstatus=$?  # Need a local, since other cmds change $?

  if [[ "$exitstatus" -gt 0 && "$shownexterr" -gt 0 ]]; then
    psvar[3]="[${exitstatus}]"
  else
    psvar[3]=""
  fi
  shownexterr=0;
}



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