Skip to content

Instantly share code, notes, and snippets.

@mendeza
Last active March 18, 2022 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mendeza/468230f6cb98be32716e220340e8f463 to your computer and use it in GitHub Desktop.
Save mendeza/468230f6cb98be32716e220340e8f463 to your computer and use it in GitHub Desktop.
git completion and prompt under zsh

git completion and prompt under zsh

curl scripts into config folder

mkdir $HOME/.config/zsh
cd $HOME/.config/zsh
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh -o _git
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O

Add to .zshrc

# Git completions, status, and prompt
# See: https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Zsh
autoload -Uz compinit && compinit
# See: $HOME/config/zsh/_git
fpath=($HOME/.config/zsh $fpath)
# uncomment if completions don't work:
# zstyle ':completion:*:*:git:*' script ~/.config/zsh/git-completion.bash

# Git status in prompt
# See: $HOME/config/zsh/git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM="auto verbose name git"
GIT_PS1_SHOWCOLORHINTS=1
GIT_PS1_STATESEPARATOR="|"
source $HOME/.config/zsh/git-prompt.sh
setopt PROMPT_SUBST
export PROMPT='%~ $(__git_ps1 "\n(枝%s)")
# Or for a more compact prompt, remove the linebreak (this is what I actually use):
# export PROMPT='%~ $(__git_ps1 "枝%s")
%# '
@mendeza
Copy link
Author

mendeza commented Oct 18, 2021

This is a very opinionated prompt, stacking 2-3 lines, showing only:

— relative path from $HOME
— branch name and state (if within a git repo)
— prompt (regular user '%', root '#')

Why?

  • I know who I am and which machine I'm using, and I virtually never ssh into another box, so username and hostname aren't needed.
  • I always want to know my working directory.
  • I always want to know if I'm within a git repo, and if so, the branch and status.
  • I always want maximum terminal width for potentially long commands.
  • I always want commands to have the same margin—no ragged left—to ease visual scanning back up the screen.
  • is just for fun. It means branch in both Chinese and Japanese.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment