Per ottenere un prompt che indica Git-filiale a Zsh
Domanda
Ho eseguito i seguenti codici a parte come il mio sollecito senza successo in .zshrc. Questo mi suggerisce che a quanto pare non ho un programma chiamato __git_ps1. Non è in MacPorts.
# 1
PROMPT="$(__git_ps1 " \[\033[1;32m\] (%s)\[\033[0m\]")\$"$
# 2
PROMPT="$(__git_ps1 " (%s)")\$"$
# 3
# Get the name of the branch we are on
git_prompt_info() {
branch_prompt=$(__git_ps1)
if [ -n "$branch_prompt" ]; then
status_icon=$(git_status)
echo $branch_prompt $status_icon
fi
}
# Show character if changes are pending
git_status() {
if current_git_status=$(git status | grep 'added to commit' 2> /dev/null); then
echo "☠"
fi
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='
%~%{$fg_bold[black]%}$(git_prompt_info)
→ %{$reset_color%}'
Come si può ottenere un prompt che mostra il nome di un Git-ramo?
Soluzione
__git_ps1
è da git-completion.bash. In zsh probabilmente è necessario fornire la propria funzione per determinare il ramo directory git attuale. Ci sono alcuni post sul blog circa un git pronta per zsh.
Hai solo bisogno:
- una funzione di fornire il nome del ramo
- abilitare la richiesta di sostituzione (comando)
- aggiungere la funzione per il prompt
Ad esempio
git_prompt() {
ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
echo $ref
}
setopt prompt_subst
PS1=$(git_prompt)%#
autoload -U promptinit
promptinit
Aggiornamento: utilizzare il modulo di zsh vcs_info invece di git_prompt ()
setopt prompt_subst
autoload -Uz vcs_info
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 '
zstyle ':vcs_info:*' formats \
'%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{5}]%f '
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r'
zstyle ':vcs_info:*' enable git cvs svn
# or use pre_cmd, see man zshcontrib
vcs_info_wrapper() {
vcs_info
if [ -n "$vcs_info_msg_0_" ]; then
echo "%{$fg[grey]%}${vcs_info_msg_0_}%{$reset_color%}$del"
fi
}
RPROMPT=$'$(vcs_info_wrapper)'
Altri suggerimenti
Ecco un prompt git estesa per zsh:. zsh-git-pronta
La risposta di ko-dos è grande, ma io preferisco un po 'più git pronta a conoscenza di quello che usa. In particolare, mi piace messo in scena, non è stato classificato, e le notifiche di file non monitorate nel prompt stesso. Usando la sua risposta e la zsh vcs_info esempi, sono venuto con il seguente:
setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' stagedstr 'M'
zstyle ':vcs_info:*' unstagedstr 'M'
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' actionformats '%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats \
'%F{5}[%F{2}%b%F{5}] %F{2}%c%F{3}%u%f'
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked
zstyle ':vcs_info:*' enable git
+vi-git-untracked() {
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
[[ $(git ls-files --other --directory --exclude-standard | sed q | wc -l | tr -d ' ') == 1 ]] ; then
hook_com[unstaged]+='%F{1}??%f'
fi
}
precmd () { vcs_info }
PROMPT='%F{5}[%F{2}%n%F{5}] %F{3}%3~ ${vcs_info_msg_0_} %f%# '
Questo crea un prompt che imita l'output colorato di git status -s
(che può essere configurato nel file .gitconfig
). Un quadro è forse più utile qui:
In confronto con git status -s
:
Se non ti piace di output colorato, o preferisce qualche altro personaggio o la costruzione rapida, basta cambiare le stagedstr
, unstagedstr
e valori hook_com[unstaged]
nel codice qui sopra.
Un sacco di queste soluzioni sembrava lento per me quando schiacciare il tasto Invio, quindi ecco un'opzione che è essenziale e veloce:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
setopt PROMPT_SUBST
PROMPT='%9c%{%F{green}%}$(parse_git_branch)%{%F{none}%} $ '
Si otterrà un prompt che assomiglia a questo:
~/dev/project (feature-branch) $
Grazie per i link!
Ho fatto il seguente messaggio basata su di essi
# get the name of the branch we are on
git_prompt_info() {
git branch | awk '/^\*/ { print $2 }'
}
get_git_dirty() {
git diff --quiet || echo '*'
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='%{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)$(get_git_dirty)%{$fg[blue]%} $ %{$reset_color%}'
RPROMPT='%{$fg[green]%}%1(j.%j.)'
, qualsiasi proposta di miglioramento.
Ho appena rifatto il mio da quando abbiamo nomi filiali lunghi sul posto di lavoro. Questo troncherà con i puntini di sospensione se si tratta di più di 35 caratteri.
parse_git_branch() {
git_status="$(git status 2> /dev/null)"
pattern="On branch ([^[:space:]]*)"
if [[ ! ${git_status} =~ "(working (tree|directory) clean)" ]]; then
state="*"
fi
if [[ ${git_status} =~ ${pattern} ]]; then
branch=${match[1]}
branch_cut=${branch:0:35}
if (( ${#branch} > ${#branch_cut} )); then
echo "(${branch_cut}…${state})"
else
echo "(${branch}${state})"
fi
fi
}
setopt PROMPT_SUBST
PROMPT='%{%F{blue}%}%9c%{%F{none}%}$(parse_git_branch)$'
(mi vergogno a quanto sono orgoglioso di questo.)