12

Zsh / Oh-my-zsh on Windows Git Bash

 1 year ago
source link: https://gist.github.com/fworks/af4c896c9de47d827d4caa6fd7154b6b
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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

Thank you soooo much!!!!!!!!!!!

Does anyone have any tips to make ZSH startup faster on windows? It takes a few seconds to startup on my PC and it kinda started to annoy me Q.Q

@manciuszz I use SSD and my average startup time is 1-2 seconds and I use oh-my-zsh theme. In oh-my-zsh, if I add plugin for nodejs it will increase startup time.

@manciuszz I use SSD and my average startup time is 1-2 seconds and I use oh-my-zsh theme. In oh-my-zsh, if I add plugin for nodejs it will increase startup time.

DiskMark64_xZZhEI0UUa

I doubt my SSD is the problem. I'm also running 8 core 16 thread CPU that runs on 4.2GHz across all cores and 5GHz 1 core. Furthermore, I'm using PowerLevel10K theme with INSTANT-PROMPT enabled, while running 3 plugins -

plugins=(
	adb
	zsh-autosuggestions 
	zsh-syntax-highlighting
)

The zsh starts instantly, but the time until I can interact with it takes around 1-2 (sometimes even a couple seconds more) and that is what bothers me.

I wonder if its the plugins or just MINGW64 being super slow.

egyjs commented on Aug 29, 2021

edited

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

Author

fworks commented on Sep 2, 2021

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.

Thanks for this guide!

Regarding step 6 Configuring zsh as default shell

Instead of creating/editing ~/.bashrc and manually calling exec zsh inside the if [ -t 1 ]; block to start zsh from bash, you could also create a shortcut to directly open zsh:

C:\Program Files\Git\bin\bash.exe -c zsh

As an example, duplicate the existing "Git Bash" shortcut (as created by the installer), and edit it to look like this:

image

except for the part of i needed to extract it with 7zip everything wents fine =)

6) Configuring zsh as default shell

Edit the ~/.bashrc file.
Add the following lines at the end of the file

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

Hey all work, but where I find bashrc file?

If it doesn't exist, refer to https://gist.github.com/fworks/af4c896c9de47d827d4caa6fd7154b6b#gistcomment-3718563

Hi everyone.
I've tried everything but zsh didn't start when GitBash launched.

So i 've add (append) the same code in C:\Program Files\Git\etc\bash.bashrc (requires elevated priviligies) and now it starts.
I know that is not a good idea. but works. :)

The if [ -t 1 ]; then exec zsh; fi statement in my ~/.bashrc file didn't work well for me. It would work fine when running the terminal interactively, but running build tasks in VS Code wouldn't work when using "Git Bash" as my default shell.

Here's how I got it working:

  1. Edit /etc/zsh/zshenv and add this line at the beginning of the file:
PATH=/mingw64/bin/usr/bin:/usr/bin:/bin:$PATH
  1. Add this section to the VS Code settings.json file:
"terminal.integrated.profiles.windows": {
  "Git Zsh": {
    "path": "C:\\Program Files\\Git\\usr\\bin\\zsh.exe",
    "args": []
  }
}
  1. And finally, change the default shell in the same file:
"terminal.integrated.defaultProfile.windows": "Git Zsh"

sylvanic commented on Oct 23, 2021

edited

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

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

Awesome!! Thanks!

image

Before this I was using cygwin with zsh package but it has some limitations like weird directories (cygdrive), this works like a charm. Thank you so much.

masrurimz commented on Nov 8, 2021

edited

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.

image

I got wierd character when enabling auto-suggestion option on vscode and windows terminal.
Which terminal did u use ? And how to configure it with auto suggestion tobe worked ?

EDIT : SOLVED
Solved by checking out to v0.6.4

cd ~/.oh-my-zsh/plugins/zsh-autosuggestions
git checkout tags/v0.6.4 -b v0.6.4-branch

nidefawl commented on Jan 2

edited

Don't do this

C:\Program Files\Git\bin\bash.exe -c zsh

It will initialize a bash environment when it runs the rc and profile.d scripts. You can inspect this by echo $SHELL.
It better be set to zsh.

Instead

Directly invoke zsh using mintty
"C:\Program Files\Git\usr\bin\mintty.exe" --dir "%USERPROFILE%" /usr/bin/zsh -l -i

This way all the rc and profile scripts will actually detect zsh and not run .bash scripts.

I also created a ~/.zshenv to set the missing environment variables that git-bash.exe hands to /bin/bash.
I removed a lot of my windows path entries so autocomplete is more responsive.

# ~/.zshenv
# This is the first file that zsh is loading

# my preferred locale/lang settings
export LC_ALL=C.utf8
export LANG=en_US.utf8
export LC_CTYPE=en_US.utf8

if [ "x${EXEPATH}" = "x" ]; then
    # Change this to reflect your path to git
    export EXEPATH=$(/usr/bin/cygpath -u -p "C:/Program Files/Git")
fi
if [ "x${MSYSTEM}" = "x" ]; then
    # important msys env var
    export MSYSTEM=MINGW64
fi
if [ "x${PLINK_PROTOCOL}" = "x" ]; then
    # some putty env var from git-bash 
    export PLINK_PROTOCOL=ssh
fi
if [ "x${HOME}" = "x" ]; then
    # Change this to reflect your ~
    export HOME=$(/usr/bin/cygpath -u -p "C:/Users/Michael")
fi

# Change this to paths you really need in zsh, only forward slashes!
ENV_PATH_USER="D:/dev/WIN32PATH;C:/ProgramData/chocolatey/bin;C:/Python310/Scripts/;C:/Python310/;C:/Program Files/PuTTY/;C:/Users/Michael/.cargo/bin"
ENV_PATH_USER=$(/usr/bin/cygpath -u -p "${ENV_PATH_USER}")

ENV_PATH_WIN="/c/Windows/System32:/c/Windows:/c/Windows/System32/wbem:/c/Windows/System32/WindowsPowerShell/v1.0"

export PATH="${ENV_PATH_USER}:${ENV_PATH_WIN}"

#if you have problems with your path, just print it somewhere
#echo "PATH set to" >> ~/.shelllog
#echo $PATH | /usr/bin/sed 's/^\|:/\n    /g'>> ~/.shelllog

export HISTSIZE=999999999
export SAVEHIST=$HISTSIZE

If you get this message:

ERROR: this script is obsolete, please see git-completion.zsh

It is because etc\profile.d\git-prompt.sh does not check the shell before including git-completion.bash
To fix it you can create an empty file
echo "" > ~/.config/git/git-prompt.sh

Also, make sure you have configured mintty for UTF8 and 256 colors:
cat ~/.minttyrc

BoldAsFont=no
Font=DejaVuSansMono Nerd Font Mono
FontHeight=18
Columns=160
Rows=40
ScrollbackLines=2000
BackgroundColour=13,13,13
MiddleClickAction=void
RightClickAction=paste
Language=
BellType=0
BellFlash=yes
Printer=Microsoft Print to PDF
Transparency=off
CursorBlinks=yes
ThemeFile=nord
ForegroundColour=178,178,178
CursorColour=225,225,225
FontSmoothing=full
Locale=en_US
Charset=UTF-8
Term=xterm-256color
BoldAsColour=no
CursorType=block

noxizcm commented on Jan 3

thank you !!!
the installation works well. finally i prefer it to wsl2 ubuntu.

siposm commented on Feb 23

Hi! If you don't want to install Peazip just for this, you can get the extracted files from my repo which I created just for this: https://github.com/siposm/zsh-shell-extracted

Cheers,
Miki wave

rhkdgns95 commented on Feb 24

@chiragraman

Sorry for the late reply.

I solved it a while ago by updating git to the latest version

driwand commented on Mar 10

edited

anyone having an issue with zsh history when using vs code, and any ideas on how to solve it?
edit: arrow_upper_leftand "end" doesn't work as well

I've got a similar issue to @driwand.

On the whole it works, but if I press the End key, it prints a tilde instead of going to the end of the line. And If I press Home, it just does nothing.

This is only when I enter into zsh, if I use bash these keys work fine.

driwand commented on Apr 6

@IdyllicHappiness over a month and didn't find a solution yet, inform if you do please

Rick-1990 commented on Apr 11

edited

@nidefawl Thank you, your config works pretty well compared to the ~/.bashrc method!

Your solution is perfect to create a link that opens a new window with the mintty terminal, but it does not work inside Windows Terminal and VS Code. I was able to fix that simply by replacing

"C:\Program Files\Git\usr\bin\mintty.exe" --dir "%USERPROFILE%" /usr/bin/zsh -l -i

"C:\Program Files\Git\usr\bin\zsh.exe" -l -i

in both configurations. Thank you again!

EDIT: @IdyllicHappiness unfortunately I have the same issue, if you (or others!) have some suggestions please inform us

Rick-1990 commented on Apr 13

edited

@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!

driwand commented on Apr 13

@Rick-1990 it did work clap 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 @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! slightly_smiling_face

Rick-1990 commented on Apr 14

@Rick-1990 it did work clap 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 commented on Apr 16

@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 GitCredentialManager/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 commented on Apr 18

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

@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 commented on Apr 25

edited

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 eyes

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK