Лупайте сю скалу...

Іван Франко


 RSS | Atom

19 June 2015

TOP »

Selected projects See all »

Banalize: Bash static code analyzerDBIx::Report::ExcelShell settings, AKA dotfilesNagios Monitoring for Couchbase serverNagira: RESTful API for NagiosStartpack for Github pages developmentWeb iPhoto

Travel Blog Витрішки »

Україномовний блог Витрішки - блог про все, крім роботи. Цикл статей "Літо на півночі Японії" з цього блогу тепер опублікований у Витрішках

Computer Blog All posts »

iPhoto and files permission Is MacOSX 10.9 == MacOS 9.x? Running Chef roles from Capistrano und Capiche Github & Jekyll: Speeding up Jekyll siteGithub & Jekyll: More experience with Jekyll and setup changesBuild new hosts with Capistrano and Chef Request Tracker: More about custom CSS for RT4Ruby 2 test drive Nagira v0.2.5 release It's UNIX my dear Watson

Slides See all »

Nagira @TLUG, 2012Rspec and Guard

About

Ярило Yarylo, Jarilo me @ github me @ CPAN Dmytro (CV)

Unobtrusive Git prompt for ZSH

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).

Regular shell prompt - not git repository

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.

Narrow screen

Clean Git repository

When I am inside git repository right part of the prompt appears and it indicates:

  1. git branch
  2. git repository name
  3. directory inside the git repository (last part of it)

Clean git repository

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

Repository with changes

Action indication

Finally last screen shot shows example of unfinished merge. In red color indicated current action: merge, rebase,etc.

In the middle of a merge

Source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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_ and vcs_info_msg_1_.
  • In each line vcs_info_msg_0_ and vcs_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 repository
    • nvcsformats - same for non git directories
    • actionformats - 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 and vcs_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.






дмитро ковальов
dmytro @ github
dmytro.sytes.net