Tian Jiale's Blog

Windows 中使用 Oh My Zsh 的各种方式

方式介绍

首先需要明确的是 zsh 不存在 Windows 版本的命令工具。所以在实现上都需要我们有一个 Linux 系统。在 Windows 中既有 WSL2 这种 Windows 支持的 Linux 子系统,也可以使用 cygwin 和 MSYS2 这类对 Linux 系统的模拟软件,所以我们得解决方案有以下三种,不排除有其他方法。

  1. WSL2
  2. MSYS2
  3. cygwin

WSL2 安装 Oh My Zsh

在 WSL2 中安装 Oh My Zsh 的方法和 Linux 基本没有太大差别,WSL2 如何安装请自行搜索相关资料,先安装 zsh。

apt-get install zsh

然后需要安装 oh my zsh,在官网中提供了如下几种安装方式,如果你所在的地区可以正常访问 Github 则请用这种方法,如果不行请见下文。

Method Command
curl sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
wget sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
fetch sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

不能访问 Github 可以下载这个压缩包 ohmyzsh ,将其中的 .oh-my-zsh.zip 解压后放到 WSL2 用户根目录下,并在用户根目录下执行 omz.sh 文件,其中修改了需要克隆的文件资源过程。执行中可选择将 zsh 设置为默认 bash。

之后修改 .zshrc 即可配置 oh my zsh,这里给出常用的配置修改。

# 修改主题
ZSH_THEME="ys"

# 增加插件
plugins=(
  git
  zsh-syntax-highlighting
  zsh-autosuggestions
)

默认是不带 zsh-syntax-highlighting 和 zsh-autosuggestions 的可以使用如下命令获取,如果添加的是我提供的文件夹,那么这两个已经附带了。

# zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM}/plugins/zsh-syntax-highlighting

# zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM}/plugins/zsh-autosuggestions

到现在我们的 oh my zsh 已经可用了。对于需要使用 Linux 子系统开发的人来说还需要在 Linux 子系统中安装环境支持,这里不再赘述。

通过 MSYS2 使用 Oh My Zsh

MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software.

MSYS2 提供了了本地工具集,我们可以使用它来安装 Oh My Zsh,从 MSYS2 下载并安装 MSYS2,可以选择安装目录,这里如果是选择Windows的用户目录效果更好,如此便不需要修改用户目录;首次运行需要执行 pacman -Syu 更新包数据库和基本包。之后从启动菜单开启,运行 pacman-Su 来更新其他包。

使用 pacman -S zsh 来安装 zsh,之后采用和上一节一样的方法安装即可。

如果选择安装目录不是用户根目录,则最好将目录改为 Windows 用户根目录,先生成 passwd 文件,这一步需要在 MSYS2 中运行。

/usr/bin/mkpasswd.exe > /etc/passwd

生成文件之后,修改文件中指定用户的根目录即可,一般修改为 /c/Users/用户名,然后将 MSYS2 用户根目录中的 .zshrc 复制到 Windows 用户根目录中即可。

需要注意的是,把 MSYS2 中 /usr/bin 目录添加到 Windows 环境变量中就能使用各种支持的工具,另外,这种方法中使用 zsh 可以同时使用 Windows 的各种工具和 Linux 系列的工具,不需要额外配置开发工具了。

注意:两天后因卡顿已放弃,既然因为颜值而折腾为什么就不能美化 power shell 呢?参见 PowerShell 美化之 ys 主题。

通过 cygwin 使用 Oh My Zsh

a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows. a DLL (cygwin1.dll) which provides substantial POSIX API functionality.

我没用这个东西,但使用上应该和 MSYS2 差不太多,可以参照官网使用或者自行搜索相关方法。

问题集合

WSL2 中 Windows 文件夹背景色问题

在 WSL2 中切换到用户根目录,执行如下指令。

dircolors -p > .dircolors

在文件 .dircolors 中找到 OTHER_WRITABLE 字段,修改其后的第二个数字为 49,保存。

在 .zshrc 中添加如下内容。

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

重新 source 之后即可消除背景色。

zsh 中中文乱码问题

在 .zshrc 中添加如下内容。

export LC_ALL=en_US.UTF-8  
export LANG=en_US.UTF-8

重新 source 之后即可正常显示中文。

terminal 推荐

在 Windows 中推荐使用 Windows terminal,其可以支持各种 bash 和 shell 工具,在设置中可以将 MSYS2 安装的 zsh 添加进去并设置为默认。

terminal 配色可以从 iTerm2-Color-Schemes 和 Windows Terminal Themes 获取。