ZSH + Oh My ZSH! 在 Windows 上无需 WSL GenAI LIVE! | 2025 年 6 月 4 日

2025-06-08

无需 WSL 即可在 Windows 上使用 ZSH + Oh My ZSH!

GenAI LIVE! | 2025年6月4日

好吧,WSL 是在 Windows 上拥有Unixish环境的好方法,但遗憾的是它的性能和内存消耗让我不得不寻找另一种替代方案……而且我发现它在一个比我预期更近的地方。

1. Bash

第一步是下载并安装支持 bash 终端的git 。

2. ZSH

Bash 至少比 Windows 的 CMD 好用,但 ZSH 的效率提升更是锦上添花。所以我们打算在 Bash 上安装 ZSH。

下载 ZSH

从MSYS2 软件包仓库下载最新的 MSYS2 zsh 软件包。该文件的名称类似于zsh-#.#-#-x86_64.pkg.tar.zst

zst文件可以用PeaZip解压缩

安装

将存档内容(应包含etcusr文件夹)解压到 Git Bash 安装目录中。该目录可能位于 下C:\Program Files\Git。如果系统提示,请合并 文件夹的内容(不应覆盖任何文件)。

设置

打开Git Bash终端并运行zsh命令,然后验证安装的版本。



zsh --version
zsh 5.9 (x86_64-pc-msys)


Enter fullscreen mode Exit fullscreen mode

通过将以下内容附加到文件来配置zsh为默认 shell ~/.bashrc



if [ -t 1 ]; then
  exec zsh
fi


Enter fullscreen mode Exit fullscreen mode

Windows 可能会损坏某些 UTF-8 编码的文本,导致终端中显示意外的字符。要修复此问题,请将以下内容添加到~/.bashrc文件中,最好将其添加到将 shell 设置为 zsh 的代码之前:



/c/Windows/System32/chcp.com 65001 > /dev/null 2>&1


Enter fullscreen mode Exit fullscreen mode

关闭并重新打开终端,以更新这些更改。第一次会要求创建一些文件,请选择:



Quit and do nothing.  The function will be run again next time.


Enter fullscreen mode Exit fullscreen mode

3. 哦,我的 zsh!

安装

运行此命令来zsh安装Oh my zsh!并添加超能力。



sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"


Enter fullscreen mode Exit fullscreen mode

字体

手动下载并安装Meslo Nerd 字体,以包含 Powerlevel10k 可能需要的所有字形和符号

主题

有很多主题,但我最喜欢的是Powerlevel10k,因为它易于设置和使用。



git clone https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k


Enter fullscreen mode Exit fullscreen mode

~/.zshrc文件上添加此附加配置



ZSH_THEME="powerlevel10k/powerlevel10k"
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(history)
POWERLEVEL9K_SHORTEN_DIR_LENGTH=1

# User configuration

export LS_COLORS="rs=0:no=00:mi=00:mh=00:ln=01;36:or=01;31:di=01;34:ow=04;01;34:st=34:tw=04;34:pi=01;33:so=01;33:do=01;33:bd=01;33:cd=01;33:su=01;35:sg=01;35:ca=01;35:ex=01;32:"


Enter fullscreen mode Exit fullscreen mode

重新启动终端并输入p10k configure

巫师

⚠ 如果您看不到图标符号并且始终显示▯,请取消该过程,并在继续之前参考步骤 4。

插件

我的天哪!zsh很多插件可用。建议你仔细研究一下,选择最适合你需求的。

我已经安装了很多与软件开发相关的程序,以及其他一些用于添加更多功能的程序。运行以下命令:



git clone https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone https://github.com/Pilaton/OhMyZsh-full-autoupdate.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/ohmyzsh-full-autoupdate


Enter fullscreen mode Exit fullscreen mode

现在编辑~/.zshrc文件并将其添加到plugins属性中(不要使用逗号作为分隔符)



plugins=(
    adb
    command-not-found
    extract
    deno
    docker
    git
    github
    gitignore
    history-substring-search
    node
    npm
    nvm
    yarn
    volta
    vscode
    sudo
    web-search
    z
    zsh-autosuggestions
    zsh-syntax-highlighting
    ohmyzsh-full-autoupdate
)

# User configuration

ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern cursor root line)
ZSH_HIGHLIGHT_PATTERNS=('rm -rf *' 'fg=white,bold,bg=red')


Enter fullscreen mode Exit fullscreen mode

如果您使用 NVM,请注意遵循此配置以避免减慢 zsh 启动速度,并遵循此配置以加快 compinit

⚠ 如果您在之前的 ZSH 安装中完成过包配置、别名定义或其他操作,~/.basrc则需要将它们手动移动到~/.zshrc

4. 终端

要在 VSCode 和 Windows Terminal 中使用相同的终端,请遵循以下配置。

VS 代码

将这些属性添加到用户setttings.json



{ 
    ...
+   "terminal.integrated.fontFamily": "MesloLGS NF",
+   "terminal.integrated.fontSize": 12,
+   "terminal.integrated.shellIntegration.enabled": true,
+   "terminal.integrated.defaultProfile.windows": "Git Bash",
    ...
}


Enter fullscreen mode Exit fullscreen mode

微软终端

在Git Bash终端上添加这些配置。



{
    "profiles": {
        "defaults": {},
        "list": [
            {
+               "font": {
+                   "face": "MesloLGS NF",
+                   "size": 12
+               },
                "guid": "{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b}",
                "hidden": false,
                "name": "Git Bash",
                "source": "Git",
+               "startingDirectory": "D:\\Developer"
            },
        ]
    }
}


Enter fullscreen mode Exit fullscreen mode

缺点

  • Git Bash是一个模拟层,所以它没有我预期的那么快,但比 WSL 快得多(并且也不用处理他的内存和性能问题)
  • 第一次加载需要一些时间(但比 WSL 少)。
  • Windows 上的 Volta 需要额外的权限,也许你需要回到 NVM
  • 在 VS Code 终端内写入时出现奇怪的行为
  • 无法使用已保存的路径%VARIABLE%,您需要将其转换为cygpath $LOCALAPPDATA。要更改为路径,请使用类似这样的命令$(cygpath $LOCALAPPDATA)/Volta/tools/image/node/

如果 Git Bash 不是您所期望的,您也可以回到 WSL 并使用 ZSH + Oh My ZSH!


来源


好了,各位!祝您
编程愉快
🖖

啤酒

鏂囩珷鏉ユ簮锛�https://dev.to/equiman/zsh-on-windows-without-wsl-4ah9
PREV
面向 NodeJS 开发人员的 Golang Rest API - 简介
NEXT
在 Windows 上使用 WSL 进行 ZSH + Oh My ZSH!