zsh prompt
things i found useful in zsh prompt
prompt
Maybe one day I'll get back to being a minimalist and just use $
or #
or ;
,
but right now I'm liking my custom prompt,
loosely based on sindresorhus/pure.
122:49:16 ~ 0:00:00
2»
bit by bit
- start with a newline:
this can be annoying as it takes up a lot of space,
but saves you when the previous output doesn't end with a newline
- current time:
not as smart as using a time updated when execution starts
example bash
but this is simpler and you can work out the same info by doing maths with the next prompt
- full path:
very useful to orient yourself / copy-paste
- previous command execution time:
how long it took for the previous command to execute,
took me quite a while to collapse it into a 2 line thing.
- second line:
yes it uses more vertical space,
but I like having my commands consistently aligned within an environment
- screen:
tells me if I'm in a screen session
- virtual env:
tells me if I have a virtual env activated,
not used much since I don't do much python these days
- git branch:
current branch of git repo
- conditional coloring:
red / magneta based on if the previous command exited properly,
magneta because I like it.
- user@host:
only shown if I'm ssh-ed into somewhere,
otherwise it's redundant information
»
:
my current preferred arrow,
might cause issues if locale/termcap isn't set properly(?)
1#!/usr/bin/env zsh
2
3export PROMPT_EOL_MARK=''
4
5function _preexec() {
6 typeset -g prompt_timestamp=$EPOCHSECONDS
7}
8
9function _precmd() {
10 integer elapsed=$(( EPOCHSECONDS - ${prompt_timestamp:-$EPOCHSECONDS} ))
11 local human="$(( elapsed / 3600 )):${(l:2::0:)$(( elapsed / 60 % 60 ))}:${(l:2::0:)$(( elapsed % 60 ))}"
12 vcs_info
13 local newline=$'\n%{\r%}'
14
15 PROMPT="${newline}%F{green}%*%f %F{blue}%~%f %F{yellow}${human}%f${newline}"
16 PROMPT+="%F{242}${STY:+screen-}${VIRTUAL_ENV:+venv-}${vcs_info_msg_0_:+${vcs_info_msg_0_} }%f"
17 PROMPT+="%(?.%F{magenta}.%F{red})${SSH_CONNECTION+%n@%m}»%f "
18}
19
20add-zsh-hook precmd _precmd
21add-zsh-hook preexec _preexec