Skip to content

Instantly share code, notes, and snippets.

@msabramo
Created April 11, 2012 00:07
Show Gist options
  • Save msabramo/2355834 to your computer and use it in GitHub Desktop.
Save msabramo/2355834 to your computer and use it in GitHub Desktop.
The slowness of my zsh prompt when in a git-svn managed directory was killing me. I improved it by removing the git status stuff that slows it down...
function git_prompt_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$ZSH_THEME_GIT_PROMPT_SUFFIX"
}
@retgoat
Copy link

retgoat commented Apr 23, 2015

Works great! Thanks!

@kwketh
Copy link

kwketh commented May 22, 2015

Thanks, that looks great.
If anybody is still running into issues, you may trace all commands with set -o xtrace, you will see full context and likely which command may be taking long time. It helped me trace my problem.

@pmalek
Copy link

pmalek commented May 29, 2015

@kwketh How to undo set -o xtrace?

@pmalek
Copy link

pmalek commented May 29, 2015

After applying this fix I am missing the * (when I have midified working tree).

@minhoryang
Copy link

HAPPY TO MEET YOU!

@SSchnitzler
Copy link

Thank you for this gist, it made working with zsh possible again for me as it was just unusably slow and i had to disable it again.

@songcy6202
Copy link

That's great ,thanks a lot.

@smeggingsmegger
Copy link

Still relevant and wonderful. Thanks!

@triadsk
Copy link

triadsk commented Jan 2, 2016

That worked, thanks a lot!

@HandIeNeeded
Copy link

Awesome!

@xiaoyang4011
Copy link

nice!

@usmanayubsh
Copy link

Works like a charm! Thank you so much.

@dannykopping
Copy link

👍

@harukaeru
Copy link

Great!

@motocodist
Copy link

@pmalek to undo set -o xtrace, run set +o xtrace - plus instead of minus

@jep-dev
Copy link

jep-dev commented Oct 29, 2017

In my case, this didn't noticeably improve the delay. It has all the same symptoms (no delay outside of Git repo; delay for anything that causes a new prompt to draw, like hitting enter on an empty line, <ctrl+l>, etc.; no delay pressing other keys or while a command is still running.) This does not appear to be related to my network speed. I could post my versions, config files, etc. but I'd like to look find more general alternative approaches first.

For what it's worth, I'm using bhilburn/powerlevel9k with submodule checking disabled, effectively --ignore-submodules=dirty (not that the project I'm testing on has submodules anyway - more complicated projects I've cloned take much longer.)

Would it be possible to set a flag or add a sentinel file somewhere to indicate if/when the local status has actually changed? Maybe caching the command's output and using the cached version if the (parent) directory's modified date is within some threshold of the cache, or if the cache is similarly recent compared to the current time? The worst effects if it malfunctions should be saving the output every time (negligible contribution to original delay) or showing an outdated status, and both should be fixed by re-tuning the threshold, and of course excluding the cache file via .gitignore.

A few benchmarks for reference...
I can't seem to use the time command on git_prompt_info, but 500 iterations of the command (piped to /dev/null; book-ended by date) averaged to 26ms each.
Timing git add -An and git status with the same method each average to 12ms.
Pinging github.com (as a lower bound) shows an average delay of 20-30ms with a few minutes between each run.

@wang-nima
Copy link

wang-nima commented Dec 14, 2018

where should I insert your code to make it works? thanks!

@RasmusFonseca
Copy link

where should I insert your code to make it works? thanks!

In your ~/.zshrc file.

Everyone seems pretty stoked to be rid of git status from their prompt because the git_current_branch function can be slow for large repos, but if anyone would still like to see info on their current branch you can replace with the following which is still fairly fast:

export PROMPT='${ret_status} %{$fg[cyan]%}%~%{$reset_color%} %{$fg[blue]%}$(git_current_branch)%{$reset_color%} $ '

@thomasaarholt
Copy link

Could someone clarify: Should one paste the above function git_prompt... into ~/.zshrc? I don't seem to notice a difference, but then I am testing on a small repo.

@liladas
Copy link

liladas commented Jan 25, 2019

Git config modification to "hide dirty" check

git config --add oh-my-zsh.hide-dirty 1

https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/git.zsh#L16

if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then

@zad
Copy link

zad commented May 15, 2019

thank you!

@ncdmr
Copy link

ncdmr commented Dec 4, 2019

thanks!

@namzo
Copy link

namzo commented Jan 22, 2020

Git config modification to "hide dirty" check

git config --add oh-my-zsh.hide-dirty 1

https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/git.zsh#L16

if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then

Thanks!

@anselm94
Copy link

anselm94 commented Aug 4, 2020

If someone uses gruvbox theme on ZSH, you may have to comment out prompt_bzr from build_prompt function in ~/.oh-my-zsh/custom/themes/gruvbox.zsh-theme file.

Or you may check why ZSH lags by executing this

zsh -vx

@stevenvo
Copy link

this saved my day!

@avatar-lavventura
Copy link

Thanks ; would it be enough if I just paste it into .zshrc file?

@trunc8
Copy link

trunc8 commented Nov 19, 2020

Awesome and thanks @liladas!
@avatar-lavventura, add this snippet just above the source $ZSH/oh-my-zsh.sh line in your ~/.zshrc

function git_prompt_info() {
  local ref
  if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
    if [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
      ref=$(__git_prompt_git symbolic-ref HEAD 2> /dev/null) || \
      ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return 0
      echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
    fi
  fi
}

And inside your desired (probably very large) git repo, run this command git config --add oh-my-zsh.hide-dirty 1

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