Did you notice the command error exit in the shell?
28 points by monzool
28 points by monzool
My prompt is normally green and turns red if the previous command didn't exit with code 0. (The command typed is in unstyled text, it's just the prompt that's coloured.) This is much less screaming than the version in OP, but also works well. It also has the exit code in there.
By the way, this is default in fish, which I use. :)
I do something similar. I append the error code number. The code for my (minimalist) prompt is here https://github.com/jmtd/punctual
The nice thing about the fish prompt is that if you pipe a command into another command, it'll show you which command failed too
That's exactly what I do in Zsh too.
Preview: BjEgC6Q.png
Code for .zshrc:
precmd() {
code=$?
PS1="%F{0}"
PS1="$PS1%K{green}"
PS1="$PS1%~"
if [[ $code -eq 0 ]]; then
PS1="$PS1%F{green}"
else
PS1="$PS1 %K{red} $code"
PS1="$PS1%F{red}"
fi
PS1="$PS1 %k%f "
}
Same, I thought it's quite standard nowadays.
If one needs something so screaming for git, I would advise checking out jj, where mistakes are not such a big deal. :D
This is built-in zsh with setopt printexitvalue.
It's documented as "PRINT_EXIT_VALUE":
Print the exit value of programs with non-zero exit status. This is only available at the command line in interactive shells.
I have a bash function that sets the terminal character "$" to red if the command fails.
Edit here's where I stolecopied it from
What I like about this solution is, that it allow me to bring extra attention for specific programs.
I use starship which changes prompt (đ ) to red on error exit. It is perhaps a bit subtle, but is generally perfectly fine for most cases. I think its just my workflow for git that trips me once in a while. I always use the command line for git commands. I will typically switch to the multi-tab never-closed Wezterm terminal; fire the commands and then toggle back to vscode. So the git error message is there in the terminal. The current prompt arrow is red. Heck, even the old branch name listed by starship is still there (although that is not always easy to distinguish). Its lack of attention from my part in that specific workflow
λ(user@host ~) $
-1-(user@host ~) $
the error part is red and not white like the (fluff) λ . Good enough even at a glance, been running this or a variation of it for probably 15 years.
I do something similar: green "â" if the exit code was 0, red "2 â" otherwise. Also, I am using a transient prompt and only the symbol is kept for past prompt (not the error code).
I do something similar but do it like this, so that the highlighted error code is part of the PS1: https://github.com/bkhl/dotfiles/blob/main/.bashrc#L155
Actually modifying the prompt string rather than printing directly to the terminal avoids some input issues that can otherwise happen.
I just print in red: "Error: ${code}" after every failing top level command. I don't do this for pipefails.
Tainting the prompt with previous command status never felt right to me.
I've had it like this since I've built LFS in 2004 or so. LFS teaches you hard to watch out for failing commands I guess.
EDIT: here's the code:
function err() { echo -e "\e[1;31mError: \e[33m$?\e[0m" >&2; }
trap err ERR
i love the amiga throwback. well done@
grml zsh config which I use on every system/server/OS shows me the return code in red, if it wasn't 0.
My shell prompt looks like
fanf@basalt.dotat.at:~/Code/ops/wwwdotat (trunk)
: 0 ;
The username / hostname / working directory is in scp syntax. (I used to deal with a lot of computers and often switched user so this is vital for situational awareness.) The git branch is a relatively recent addition (this century); it breaks triple-click to select the whole path, but in practice I donât need to do that very often. The line break allows the path to grow without pushing the command line into the right margin. The :; is a no-op command that allows me to triple-click copy and paste the command line. The 0 is the exit status. I donât use colour in my prompt, itâs boldface which is just enough to stand out from command output.
I've got the following snippet in my .zshrc and it does the job without more than 1 LOC and doesn't bork the layout of the regular prompt (which makes it a tad easier to miss, but I'm fine with that)
RPROMPT='%F{red}%(?..%?)%f'
This shows exit code only if it was non-zero (as much as I don't like info-pages, zsh info is great)
I prefer to integrate the exit code itself into the prompt:
~$ true
~$ false
~ [1]$ foo
zsh: command not found: foo
~ [127]$
Coloured red, itâs plenty visible enough for me.
Adapted from the end of my zsh $PROMPT, after the working directory part
%(?,, %F{red}[%?]%f)$
Read through âEXPANSION OF PROMPT SEQUENCESâ of man zshall for explanation of all % bits. %(?,,âŠ) is a ternary on exit status, %? is the exit status itself. I donât actually use %F myself, but instead write the escape sequence out manually, from habit, including literal â (Vim: Ctrl-V Escape) rather than \e, though this way then requires %{âŠ%} wrapping to get cursor positioning right:
%(?,, %{\e[31m%}[%?]%{\e[m%})$
mine turns the $ into a red 1 (or whatever the non-zero exit code was)