Skip to content

Instantly share code, notes, and snippets.

@fworks
Last active February 27, 2024 09:30
Star You must be signed in to star a gist
Save fworks/af4c896c9de47d827d4caa6fd7154b6b to your computer and use it in GitHub Desktop.
Zsh / Oh-my-zsh on Windows Git Bash

Steps:

  1. Download the latest zsh package: https://packages.msys2.org/package/zsh?repo=msys&variant=x86_64

Example:

zsh-5.7.1-1-x86_64.pkg.tar.xz

Update:

The package now is compacted using zstd, so now we need some "special" extractor.
So, in my case, I've downloaded this file
https://repo.msys2.org/msys/x86_64/zsh-5.8-5-x86_64.pkg.tar.zst And extracted it using the Peazip.
https://peazip.github.io/zst-compressed-file-format.html

  1. Extract the content to your git bash installation dir:

Usually C:\Program Files\Git

  1. Test it and config zsh:

Open git bash and type:

zsh

So, this step is important, it seems zsh will ask a few configurations, like the tab completion, history, etc.
Please read the options and set that according to your use.

  1. Installing oh-my-zsh, execute the following cmd on git bash
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  1. Configuring zsh as default shell

Edit the ~/.bashrc file. (create it if it doesn't exist)

Add the following lines at the end of the file

# Launch Zsh
if [ -t 1 ]; then
exec zsh
fi

Close and open again the git bash.

Optional steps

  1. Installing the Honukay theme
curl -fsSL https://raw.githubusercontent.com/oskarkrawczyk/honukai-iterm/master/honukai.zsh-theme -o ~/.oh-my-zsh/custom/themes/honukai.zsh-theme
  1. Set it
sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="honukai"/g' ~/.zshrc
@IdyllicHappiness
Copy link

@driwand @IdyllicHappiness I was able to fix the Home/End buttons (and possibily other unknown problems with keycodes) putting the following line inside ~/.zshenv:

export MSYS=enable_pcon

I noticed that git-bash was exporting that variable in a file located in C:\Program Files\Git\etc\git-bash.config, so i googled that and found this: https://github.com/mintty/mintty/wiki/Tips#inputoutput-interaction-with-alien-programs

Hope it works also for you!

@Rick-1990 Worked a charm, thanks! 🙂

@Rick-1990
Copy link

@Rick-1990 it did work 👏 appreciate it! fixed the history navigation as well

I realized that resizing the terminal within vs code is causing it to crash, do you have an idea about this one?

@driwand yeah noticed that, in my case it happens only when resizing the height of the window, also in Windows Terminal. Curiously though, it works fine in mintty. I do not have a very deep knowledge of this kind of stuff, but I'll try to investigate in my free time.

@nidefawl
Copy link

@driwand @IdyllicHappiness I was able to fix the Home/End buttons (and possibily other unknown problems with keycodes) putting the following line inside ~/.zshenv:

export MSYS=enable_pcon

I noticed that git-bash was exporting that variable in a file located in C:\Program Files\Git\etc\git-bash.config, so i googled that and found this: https://github.com/mintty/mintty/wiki/Tips#inputoutput-interaction-with-alien-programs

Hope it works also for you!

Interesting.
I worked around the home/end keys by adding this to my ~/.zshrc

bindkey  "^[[1~"   beginning-of-line
bindkey  "^[[4~"   end-of-line

One other problem I had was with git pull creating directories names = every time I run a pull inside zsh.
This only happens when you use key based authentication along with the git-credential-manager for passphrase storage
That is fixed by no longer exporting the (empty) lowercase tmp and temp environment variabels in /etc/profile
More info git-ecosystem/git-credential-manager#505 (comment)

diff --git a/C:/Program Files/Git/etc/profile.orig b/C:/Program Files/Git/etc/profile
index 7556f28..72a3d7e 100644
--- a/C:/Program Files/Git/etc/profile.orig
+++ b/C:/Program Files/Git/etc/profile
@@ -151,7 +151,7 @@ then
   export ACLOCAL_PATH
 fi

-export PATH MANPATH INFOPATH PKG_CONFIG_PATH USER TMP TEMP HOSTNAME PS1 SHELL tmp temp ORIGINAL_TMP ORIGINAL_TEMP ORIGINAL_PATH CONFIG_SITE
+export PATH MANPATH INFOPATH PKG_CONFIG_PATH USER TMP TEMP HOSTNAME PS1 SHELL ORIGINAL_TMP ORIGINAL_TEMP ORIGINAL_PATH CONFIG_SITE
 unset PATH_SEPARATOR

 if [ "$MAYBE_FIRST_START" = "true" ]; then

@Rick-1990
Copy link

Thank you @nidefawl, I don't currently use key based auth in my setup, but I'll remember your fix 😃

@driwand The "resize problem" seems to be from oh-my-zsh, more specifically it's a bug of the git_prompt_info function (I think this happens only on Windows, in my Mac I wasn't ever able to reproduce it): ohmyzsh/ohmyzsh#9953

@Rick-1990
Copy link

Rick-1990 commented Apr 25, 2022

Ok, I have absolutely NO IDEA about this one, but it works. I was checking out this article and saw that in the bottom notes he suggests to place unset LESS; at the end of ~/.zshrc because "on some commands oh-my-zsh runs less command with -R (repaint)". That had me curious, so I tried and I have no more crashes.

Just to be clear, that -R flag actually instructs less to print raw control characters. This MAY be a reason for the crashes, but I don't know anything more than that.

I tried to look for less usages inside oh-my-zsh to no luck (found it inside some plugins, but I haven't enabled any of them). I really have no clue 👀

@salloom-domani
Copy link

I noticed an issue with root directory autocompletion when I type for example "/c/use" and hit tab it won't find it, except in git bash I could get the autocompletion correctly and with capitalized folders like "/c/Users"

@hatakawas
Copy link

I personally still have issues with trying to fix the ERROR: this script is obsolete, please see git-completion.zsh that pops up each time I open ZSH.

I've tried all the methods mentioned above by @wl2776 and @michelpfirter

Where exactly in git/etc/zsh should I place those two lines?

local drives=($(mount | command grep --perl-regexp '^\w: on /\w ' | cut --delimiter=' ' --fields=3))
zstyle ':completion:*' fake-files "/:${(j. .)drives//\//}"

UPDATE I was looking at solving slow performance in zsh, and stumbled upon this: adding those 2 lines at the .gitconfig files in the home directory

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

Not only did it speed up my zsh significantly, it also removed the error somehow, not sure exactly why.

Source: https://stackoverflow.com/questions/12765344/oh-my-zsh-slow-but-only-for-certain-git-repo

Thanks, it works for me!

The 2 lines of "git config", seems it just hide the error but the error still there. I don't like this.

I added the "local drivers" and "zstyles" lines to /etc/zshprofile located in C:\Program Files\Git\etc\zsh
Then the ERROR messge gone.

@thienbao12a2
Copy link

I am having an issue in vscode terminal where typing text, then removing it will leave small pixel on the screen like in the picture. Could you please help me fix this issue? Thank you
image

@tanishqmanuja
Copy link

tanishqmanuja commented Jul 25, 2022

Same pixel issue for me also

@HaiLin545
Copy link

Is any one help with this problem ? zsh on git-bash cant't user "conda activate" and "source activate",it horrible!
image

@fworks
Copy link
Author

fworks commented Aug 8, 2022

Is any one help with this problem ? zsh on git-bash cant't user "conda activate" and "source activate",it horrible! image

Hi, I think it is not a zsh thing, it is more a git-bash/windows thing.
I remember using conda before on git bash, I think you have to use the conda activate comand instead of source activate.

@Zamiell
Copy link

Zamiell commented Aug 31, 2022

https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh
needs to be updated to:
https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh
(as per the ohmyzsh homepage)

@herrmartins
Copy link

Can you give us a light on how to make spaceship theme to work? I coudln't so far....

@LionH
Copy link

LionH commented Sep 26, 2022

I noticed an issue with root directory autocompletion when I type for example "/c/use" and hit tab it won't find it, except in git bash I could get the autocompletion correctly and with capitalized folders like "/c/Users"

I have the same issue. Also it doesn't handle well Windows Path. e.g. when executing gradle wrapper, it complains about invalid java.io.tmpdir
If you try java -XshowSettings:all it shows windows path:
java.io.tmpdir = C:\Users\lione\AppData\Local\Temp\
and I suspect that a missing mintty configuration is needed here...

@pas-de-2
Copy link

pas-de-2 commented Nov 4, 2022

zsh (or oh-my-zsh, not sure which) freezees if you try resizing the console in Windows Terminal

@Armand-Lluka
Copy link

zsh (or oh-my-zsh, not sure which) freezees if you try resizing the console in Windows Terminal

Where you able to find a fix for this?

@pas-de-2
Copy link

pas-de-2 commented Nov 18, 2022

zsh (or oh-my-zsh, not sure which) freezees if you try resizing the console in Windows Terminal

Where you able to find a fix for this?

Unfortunately, no, but I also haven't put much time into tracking down the problem.

EDIT seems to have something to do with git_prompt_info
ohmyzsh/ohmyzsh#9953

@LaurenceZanotti
Copy link

mine also freezes unfortunately

zsh (or oh-my-zsh, not sure which) freezees if you try resizing the console in Windows Terminal

@n-WN
Copy link

n-WN commented Dec 21, 2022

  1. git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH/plugins/zsh-syntax-highlighting

install zsh-autosuggestions plugin

  1. git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH/plugins/zsh-autosuggestions
  2. source $ZSH/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
  3. edit ~/.zshrc file
plugins=( 
    ...
    zsh-autosuggestions
)

install zsh-syntax-highlighting plugin

  1. git clone git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH/plugins/zsh-syntax-highlighting
  2. source $ZSH/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  3. edit ~/.zshrc file
plugins=( 
    ...
    zsh-syntax-highlighting
)

Cool. I will test that later. Thanks for that.

chenge->

  1. git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH/plugins/zsh-syntax-highlighting

@alshdavid
Copy link

alshdavid commented Jan 26, 2023

I am getting really long start up times of around 4 seconds

➜  ~ time  zsh -i -c exit
zsh -i -c exit  0.78s user 3.21s system 100% cpu 3.957 total

image

Any idea what could cause this?

Commenting out source $ZSH/oh-my-zsh.sh in my .zshrc works - but that disables oh my zsh

This is my zprof

https://gist.github.com/alshdavid/4e49614df12678c7bc7ba1fdfc3ab4c4

@lazuee
Copy link

lazuee commented Jan 26, 2023

This error is originated in "C:\Program Files\Git\etc\profile.d\git-prompt.sh" That file contains following lines

		if test -f "$COMPLETION_PATH/git-prompt.sh"
		then
			. "$COMPLETION_PATH/git-completion.bash"
			. "$COMPLETION_PATH/git-prompt.sh"
			PS1="$PS1"'\[\033[36m\]'  # change color to cyan
			PS1="$PS1"'`__git_ps1`'   # bash function
		fi

File "$COMPLETION_PATH/git-completion.bash" checks that it is run by zsh, emits error message and exits. I've changed them to the following:

		if test -f "$COMPLETION_PATH/git-prompt.sh"
		then
      		        if [ "x${ZSH_VERSION}" = "x" ]; then
  				. "$COMPLETION_PATH/git-completion.bash"
  			fi
			. "$COMPLETION_PATH/git-prompt.sh"
			PS1="$PS1"'\[\033[36m\]'  # change color to cyan
			PS1="$PS1"'`__git_ps1`'   # bash function
		fi

Bro, this replacing code makes my git to keep on popping (C:\Program Files\Git\usr\bin\zsh.exe).
I can't even exit the terminal because more git bash keeps on opening.

@alienalien13
Copy link

Thanks! work for Windows 10 Enterprise LTSC

@shierro
Copy link

shierro commented Apr 19, 2023

thanks! Looks good on Windows 11, good stuff

@kissingfire123
Copy link

kissingfire123 commented Jun 6, 2023

I noticed an issue with root directory autocompletion when I type for example "/c/use" and hit tab it won't find it, except in git bash I could get the autocompletion correctly and with capitalized folders like "/c/Users"

I ran into this problem also. inspired by @LionH , I founded a way to work around 🥇

#  after cd to c: , you will find you can complete by tab 
cd c:
cd Users/yourxxxPath  

the problem is zsh can't receive the sequences of windows-way. I tried stackoverflow: zsh-tab-completion-not-working and rehash-zsh guide, they didn't helped either.

@adamerose
Copy link

Putting this in .bashrc breaks anything trying to directly run bash commands.

if [ -t 1 ]; then
    exec zsh
fi

Didn't anyone else run into problems with VS Code because of this? I came looking for a fix but was surprised to see nobody mentioned it. For example VSCode runs npm scripts like "C:\Program Files\Git\bin\bash.exe '--login', '-i', '-c', 'npm run watch'", which doesn't work anymore.

Try to open cmd and run "C:\Program Files\Git\bin\bash.exe" --login -i -c "echo hello" and see it opens zsh but never echos anything.

Doing this instead seems to fix it:

# Check for BASH_EXECUTION_STRING to see if bash is being called with -c to execute a command.
if [[ -z "$BASH_EXECUTION_STRING" && -t 1 ]]; then
    exec zsh
fi

@rombat
Copy link

rombat commented Oct 12, 2023

image
any idea what could cause this issue when trying to install oh-my-zsh ?

@mamboer
Copy link

mamboer commented Oct 31, 2023

install zsh-autosuggestions plugin

  1. git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH/plugins/zsh-autosuggestions
  2. source $ZSH/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
  3. edit ~/.zshrc file
plugins=( 
    ...
    zsh-autosuggestions
)

install zsh-syntax-highlighting plugin

  1. git clone git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH/plugins/zsh-syntax-highlighting
  2. source $ZSH/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  3. edit ~/.zshrc file
plugins=( 
    ...
    zsh-syntax-highlighting
)

Cool. I will test that later. Thanks for that.

Change $ZSH to $ZSH_CUSTOM will be better.

@glenkusuma
Copy link

Install Git Bash with ZSH, OH MY ZSH & POWERLEVEL10K | Windows Guide

Thanks to all of you guys i've create a fork of fworks works and your guys' suggestion in the comment.

@MrOxMasTer
Copy link

image

guys, does anyone know how to remove this? What they write doesn't help. I tried using -f to run the function and it still appears

@erickshilz
Copy link

Hi,

My name is Erick Shil, I'm begginer brazilian dev and I have only one problem, that I didn't find solution anywhere.

Screenshot 2024-02-17 172921
Always when Hyper start, this message it's printed, and I don't know how to solve this question. In StackOverflow, nobody alredy have the same problem. Can you help me?

System:

  • Lenovo Ideapad 3
  • Ryzen 7 5700
  • 128GB SSD
  • 8GB RAM

(ps. Sorry for the bad English. As a developer (and understanding the importance of English, since we only speak Portuguese here), I am trying to train English without a translator...)

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