Files
hugo_blog.awin.one/content/posts/2026/2026-01-16_Windows Git Bash 改用 Zsh/index.md

122 lines
4.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
slug: Windows Git Bash 改用 Zsh
title: Windows Git Bash 改用 Zsh
description:
toc: true
authors:
- awin
tags:
- windows
- terminal
categories:
- System
series:
- Windows
date: 2026-01-16T00:00:00
lastmod: 2026-01-16T00:00:00
featuredVideo:
featuredImage:
draft: false
enableComment: true
---
[zsh](https://zh.wikipedia.org/zh-tw/Z_shell) 是一個很好用方便的 shell在 Linux 安裝非常方便,在 Windows 上如果有安裝 Git也可以透過 Git Bash 來使用,操作環境可以大幅貼近 Linux。
<!--more-->
## 0. 準備
- 要先裝 [Windows Terminal](https://apps.microsoft.com/detail/9n0dx20hk701?hl=zh-TW&gl=TW)
- 要安裝 [Git for Windows](https://git-scm.com/install/windows)
## 1. 安裝ZSH
### Windows
到 [Msys2 Package: zsh](https://packages.msys2.org/packages/zsh?repo=msys&variant=x86_64)下載
![msys2_zsh.png](images/msys2_zsh.png)
將壓縮檔內容複製到 Git 安裝目錄(一般而言是 `C:\Program Files\Git`
## 2. [安裝 oh-my-zsh](https://ohmyz.sh/)
```shell
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
```
## 3. 修改 `~/.bashrc`
Windows 的 user home 路徑是 `C:\Users\<user_name>`,所以要修改此目錄下的 `.bashrc`,如果沒有這個檔案就自己新增一個。
然後在 `C:\Users\<user_name>\.bashrc` 的最後加入:
```
if [ -t 1 ]; then
exec zsh
fi
```
這樣下次 bash 啟動的時候就會自動使用 zsh。
zsh 被啟動之後,我們的設定檔就變成了 `~/.zshrc` 而不是 `~/.bashrc` 了。
## 4. 安裝字型
等一下要使用的佈景主題會用很多字型裡的符號在UI上所以一般正常的字型沒辦法顯示只會看到方框裡面有一個問號。
所以我們要下載「修改過」的字型來顯示這些特殊符號。
到 [Nerd Font](https://www.nerdfonts.com/)下載你喜歡的字型。然後安裝到 Windows 裡面。這樣就可以了。
這裡假設你下載了 FiraCode Nerd Font。
![nerdfont_firacode.png](images/nerdfont_firacode.png)
## 5. 設定 Windows Terminal 的字型
打開 Windows Terminal`Ctrl+,` 進入設定畫面,左邊選 Git Bash ,然後在右邊選 "Appearance"
![windows_terminal_bash_appearance.png](images/windows_terminal_bash_appearance.png)
在 Font Face 那邊選擇 FiraCode Nerd Font。
![windows_terminal_bash_fontface.png](images/windows_terminal_bash_fontface.png)
## 6. 安裝 [powerlevel10k](https://github.com/romkatv/powerlevel10k)
[powerlevel10k](https://github.com/romkatv/powerlevel10k) 這個佈景主題讓 zsh 變得很好看
```shell
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
```
然後打開 `~/.zshrc` ,把佈景主題改成 powerlevel10k
找到 ` ZSH_THEME` 這一行,改成 `ZSH_THEME="powerlevel10k/powerlevel10k"`
![zsh_theme_setting.png](images/zsh_theme_setting.png)
然後重新打開 Windows Terminal打開 Git Bash ,就會出現 powerlevel10k 的 config 流程,你可以在這裡設定自己喜歡的風格樣式。
如果字型設定正確的話,會看到正確的符號,否則就是方框裡面有一個問號。
![powerlevel10k_config.png](images/powerlevel10k_config.png)
## 7. 安裝 plugin
這步不一定要做,看需求。
### 安裝 [zsh-syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting)
這個 plugin 可以在 terminal 顯示 highlight syntax
安裝
```shell
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
```
然後要修改 `~/.zshrc`,在 `plugins` 加入 `zsh-syntax-highlighting`
![zsh_plugins_setting.png](images/zsh_plugins_setting.png)
### 安裝 [zsh-autosuggestions](https://github.com/zsh-users/zsh-autosuggestions)
這個 plugin 可以在輸入的時候提供已經輸入過的歷史命令或是相似的命令,讓你節省一點時間。
安裝
```
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
```
然後修改 `~/.zshrc`,在 `plugins` 加入 `zsh-autosuggestions`