Summary
How to incorporate Git information in Zsh shell prompt in an unobtrusive manner
vcs_info module for ZSH gives an ability to design nice and functional prompts for use with version control systems. Google can find large number of references to manuals, tutorials, blog posts and what not regrading using the module for prompt designs. In many cases, however, as to my taste resulting prompts stand in the way more than actually help. Four or five lines shell prompts are not for me, same if prompt occupies 80% of the command line space, or it is too colorful.
It took me a while, first, to learn about ZSH prompts and vcsinfo, and second to design a prompt, that I can use without too much hating it. Below are some screen-shots and code for the prompt.
I am using git
mostly, so all my examples below are for git
.
Screen shots
Standard prompt
When not in the git repository prompt is pretty much standard UNIX shell prompt: user name, hostname and last part of the current directory. Stepping into git repository add right part to the prompt (see below).
Small screen
Same prompt if I am in the git repository but my screen space is too small to accommodate both parts of the prompt (yellow line on the right is pane border in Tmux). Also what is really nice about right part of the prompt: if I start typing very long command and come close to the right part of the prompt, it disappears.
Clean Git repository
When I am inside git repository right part of the prompt appears and it indicates:
- git branch
- git repository name
- directory inside the git repository (last part of it)
Git repository with uncommitted changes
Right part in this case adds indication for:
- U - for unstaged changes (encircled)
- S - for staged changes
- M - for stashed changes
Action indication
Finally last screen shot shows example of unfinished merge. In red color indicated current action: merge, rebase,etc.
Source code
| autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git
precmd() {
vcs_info
}
setopt prompt_subst
zstyle ':vcs_info:git:*' check-for-changes true
zstyle ':vcs_info:*' formats "%f[%%n@%%m %1~] $ " "%f%a %F{3}%m%u%c %f%b:%r/%S"
zstyle ':vcs_info:*' nvcsformats "%f[%n@%m %1~]$ " ""
zstyle ':vcs_info:*' actionformats '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
PROMPT='${vcs_info_msg_0_}'
RPROMPT='${vcs_info_msg_1_}' |
Short explanation for the code.
- Each
zstyle ':vcs_info:*'
line sets style for vcs_info messages. There are 2 messages configured:vcs_info_msg_0_
andvcs_info_msg_1_
. - In each line
vcs_info_msg_0_
andvcs_info_msg_1_
are first and second arguments in each definition. I.e., for example on line 9"%f[%%n@%%m %1~] $ "
is msg 0, and"%f%a %F{3}%m%u%c %f%b:%r/%S"
is msg 1.formats
defines style for prompt in inside git repositorynvcsformats
- same for non git directoriesactionformats
- for git actions
- Left and right parts of prompt are set on lines 13 and 14 correspondingly to display
vcs_info_msg_0_
on the left andvcs_info_msg_1_
on the right.
References
Below are links to some of the resources I've used. They can be used as reference or simply as example of different designs of the prompt.